Last active
October 30, 2015 14:03
-
-
Save mikesorae/3b120b5391999c5d11c6 to your computer and use it in GitHub Desktop.
For Google Apps Script Hands-on
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 1. 初めてのGAS | |
// メッセージボックスにHello, Worldを表示します。 | |
function myFirstGAS() { | |
Browser.msgBox('Hello World!'); | |
Logger.log('Hello World!'); | |
} | |
// シートから値を取得して表示します。 | |
function getValueFromSheet() { | |
var book = SpreadsheetApp.getActive(); | |
var sheet = book.getActiveSheet(); | |
var value = sheet.getRange(1, 1).getValue(); | |
Browser.msgBox('cellの値は"' + value + '"です。'); | |
} | |
// シートに値をセットします。 | |
function setValueIntoSheet() { | |
var book = SpreadsheetApp.getActive(); | |
var sheet = book.getActiveSheet(); | |
var value = 12345; | |
sheet.getRange("A1").setValue(value); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function onOpen(){ | |
var sheet = SpreadsheetApp.getActiveSpreadsheet(); | |
var items = [ | |
{name: 'メール送信', functionName: 'sendMail'}, | |
{name: '定型メール送信', functionName: 'sendTemplatedMail'} | |
]; | |
sheet.addMenu('メール送信', items); | |
} | |
function sendMail() { | |
var sendTo = 'your_email_address'; // 送り先のメールアドレス | |
var subject = 'test mail 2'; | |
var body = 'メール送信テストです。'; | |
MailApp.sendEmail(sendTo, subject, body); | |
} | |
function sendTemplatedMail() { | |
// シートを取得 | |
var sheet = SpreadsheetApp.getActiveSheet(); | |
// 本日の日付を取得し、文字列に変換 | |
var date = new Date(); | |
var dateString = (date.getMonth() + 1) + '月' + date.getDate() + '日'; | |
Logger.log(dateString); | |
Logger.log(date.getFullYear()); | |
// 日付に対応するセルを取得 | |
var cell = findDateInRange(date, sheet.getRange('A:A')); | |
Logger.log(cell); | |
var todaysTaskCell = cell.offset(0, 1); | |
var nextTaskCell = cell.offset(0, 2); | |
var notesCell = cell.offset(0, 3); | |
// メールの宛先、タイトル、本文を準備 | |
var sendTo = 'your_email_address'; // 送り先のメールアドレス | |
var subject = '[作業日報] ' + dateString + ' まいく'; | |
var body = 'お疲れ様です。まいくです。\n' + | |
dateString + 'の作業日報を送ります。\n' + | |
'\n' + | |
'[本日の作業]\n' + | |
todaysTaskCell.getValue() + '\n' + | |
'\n' + | |
'[次回の作業]\n' + | |
nextTaskCell.getValue() + '\n' + | |
'\n' + | |
'[共有事項]\n' + | |
notesCell.getValue() + '\n' + | |
'\n' + | |
'以上、よろしくお願いします。'; | |
// メールAPIに宛先、タイトル、本文を渡してメールを送信する | |
MailApp.sendEmail(sendTo, subject, body); | |
} | |
/** | |
* 指定されセルの範囲から値を検索する | |
* @param value 検索する値 | |
* @param range 検索範囲 | |
* @return 最初に見つかったセル | |
*/ | |
function findValueInRange(value, range) { | |
var data = range.getValues(); | |
for(var i = 0; i < data.length; i++) { | |
for(var j = 0; j < data[i].length; j++) { | |
if(data[i][j] == value) { | |
return range.getCell(i + 1, j + 1); | |
} | |
} | |
} | |
return null; | |
} | |
/** | |
* 指定されセルの範囲から日付を検索する | |
* @param date 検索する日付 | |
* @param range 検索範囲 | |
* @return 最初に見つかったセル | |
*/ | |
function findDateInRange(date, range) { | |
var data = range.getValues(); | |
for(var i = 0; i < data.length; i++) { | |
for(var j = 0; j < data[i].length; j++) { | |
target = data[i][j]; | |
if(!validateType('Date', target)) { | |
continue; | |
} | |
if(date.getFullYear() == target.getFullYear() | |
&& date.getMonth() == target.getMonth() | |
&& date.getDate() == target.getDate()) { | |
return range.getCell(i + 1, j + 1); | |
} | |
} | |
} | |
return null; | |
} | |
/** | |
* オブジェクトが指定した型かどうか判別する。 | |
* @param type 目的の型 | |
* @param obj 型をチェックしたいオブジェクト | |
* @return チェック結果。型が一致すればtrue | |
*/ | |
function validateType(type, obj) { | |
var clas = Object.prototype.toString.call(obj).slice(8, -1); | |
return obj !== undefined && obj !== null && clas === type; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function createEvent() { | |
// 自分のカレンダーIDを指定する | |
var calendarId = 'your_calendar_id'; | |
var calendar = CalendarApp.getCalendarById(calendarId); | |
var title = 'D3勉強会'; | |
var startTime = new Date('2015/10/5 20:00'); | |
var endTime = new Date('2015/10/5 21:00'); | |
calendar.createEvent(title, startTime, endTime); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Getアクセスした時に呼ばれるイベントハンドラ | |
function doGet(e) { | |
var content = sampleContent(); | |
// var content = usersContent(); | |
return ContentService.createTextOutput(content).setMimeType(ContentService.MimeType.JSON); | |
} | |
// サンプルAPI用Json文字列作成 | |
function sampleContent() { | |
var json = {body : 'Hello World!'}; // 簡単なJsonを作成 | |
return JSON.stringify(json); // 文字列に変換して返す | |
} | |
// ユーザ情報一覧Json文字列作成 | |
function usersContent(){ | |
var book = SpreadsheetApp.openById('yourSheetId'); // 自分のスプレッドシートのID | |
var sheet = book.getSheetByName('users'); | |
var json = []; | |
// 見出し行を取得 | |
var titles = sheet.getRange(1, 1, 1, 4).getValues()[0]; | |
// 最終行を取得 | |
var lastRow = sheet.getLastRow(); | |
// シートの一番最後の行までデータを取得してjsonオブジェクトに格納 | |
for(i = 2; i <= lastRow; i ++) { | |
var userJson = {}; | |
var rowData = sheet.getRange(i, 1, 1, 4).getValues()[0]; | |
for(j = 0; j < 4; j++) { | |
userJson[titles[j]] = rowData[j]; | |
} | |
json.push(userJson); | |
} | |
return JSON.stringify(json); | |
} | |
// フォーム編集時に呼ばれるイベントハンドラ | |
// トリガーも設定する必要があります。 | |
function onFormSubmit(e) { | |
// イベントのパラメータから新規に追加された行のrangeを取得 | |
var range = e.range; | |
var values = range.getValues()[0]; | |
// シートを取得 | |
var book = SpreadsheetApp.openById('yourSheetId'); // 自分のスプレッドシートのID | |
var sheet = book.getSheetByName('users'); | |
// シートの最終行を取得 | |
var newRowNumber = sheet.getLastRow() + 1; | |
var newRow = sheet.getRange(newRowNumber, 1, 1, 4); | |
// ラジオボタンで選択した値を区分値に変換 | |
var gender = ''; | |
if(values[3] == '男性') { gender = 'male'; } | |
if(values[3] == '女性') { gender = 'female'; } | |
// フォームに投稿された内容をusersシートに追加 | |
newRow.getCell(1, 1).setValue(newRowNumber); | |
newRow.getCell(1, 2).setValue(values[1]); | |
newRow.getCell(1, 3).setValue(values[2]); | |
newRow.getCell(1, 4).setValue(gender); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var ROOM_ID = "target_room_id"; // chatworkのroomidを入力 | |
var API_KEY = "your_api_key"; // chatworkのapi keyを入力 | |
function test() { | |
sendMessage("このメッセージがChatworkの指定したroomに送信されます。"); | |
} | |
function sendMessage(message) { | |
var url = "https://api.chatwork.com/v1/rooms/" + ROOM_ID + "/messages"; | |
var method = "post"; | |
var headers = { | |
"X-ChatWorkToken": API_KEY | |
}; | |
var payload = { | |
"body": message | |
}; | |
var options = { | |
"method": method, | |
"headers": headers, | |
"payload": payload | |
}; | |
UrlFetchApp.fetch(url, options); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<base target="_top"> | |
</head> | |
<body> | |
<style> | |
table, td, th { | |
border: solid 1px gray; | |
} | |
</style> | |
<h1>Google Apps Script ハンズオン!</h1> | |
<? | |
var book = SpreadsheetApp.openById("1QZ1RgRac6Pj9MZvyHaC02XL10reINGI3pBC0kwLiMuc"); | |
var sheet = book.getSheetByName("FAQ"); | |
var lastRow = sheet.getLastRow(); | |
var range = sheet.getRange(2, 1, lastRow, 3); | |
var rows = range.getValues(); | |
?> | |
<table style='table,td,th{ border: 2px #2b2b2b solid; }'> | |
<? | |
for(i = 0; i < lastRow - 1; i ++) { | |
output.append("<tr>"); | |
var row = rows[i]; | |
for(j = 0; j < 3; j ++) { | |
output.append("<td>"); | |
output.append(row[j]); | |
output.append("</td>"); | |
} | |
output.append("</tr>"); | |
} | |
?> | |
</table> | |
</body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// このスクリプトを実行する前に下記を実施してください。 | |
// 1) 6_index.htmlをcopyしてプロジェクトにindex.htmlを作成 | |
// 2) "FAQ"シートを作成しNo, 問い合わせ, 内容のデータを作成 | |
// 3) メニュー > 公開 > ウェブアプリケーションとして導入をクリック | |
// 4) そのままの設定で公開 | |
// 5) 表示されたURLへアクセス | |
function doGet(){ | |
// index.htmlをテンプレートとして読み込み | |
var html = HtmlService.createTemplateFromFile('index'); | |
// templateファイルを評価してhtmlを出力 | |
return html.evaluate(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment