ドットインストールでGASの勉強をしています。
自分用にコードをメモ。
Googleフォームと連携する
function sendTask(e) {
MailApp.sendEmail('hoge@gmail.com', 'タスクが追加されました', e.namedValues['Task']);
}
上記とは別に、トリガーを設定する。
- 実行する関数 →sendTask
- イベントのソース →スプレッドシート
- イベントの種類 →フォーム送信時
とすると、タスクに自動追加されメールが届く!
Web上で公開
function doGet() {
var template = HtmlService.createHtmlOutputFromFile('index');
return template.evaluate()
}
TaskをWEB上で公開
<!DOCTYPE html>
<html>
<head>
<base target="_top">
</head>
<body>
<h1><?= title ?></h1>
<ul>
<? for (var i = 0; i < tasks.length; i++) { ?>
<li><?= tasks[i] ?></li>
<? } ?>
</ul>
</body>
</html>
function sendTask(e) {
MailApp.sendEmail('hoge@gmail.com', 'タスクが追加されました', e.namedValues['Task']);
}
function doGet() {
var template = HtmlService.createTemplateFromFile('index');
template.title = 'MyTaskApp';
template.tasks = getTasts();
return template.evaluate()
}
function getTasts() {
var sheet = SpreadsheetApp.getActiveSheet();
return sheet.getRange(2, 2, sheet.getLastRow() - 1, 1).getValues();
}
Task追加フォームへのリンクを付ける
<p>→ <a href="https://docs.google.com/hoge/viewform">タスク追加用フォームへのリンク</a></p>
Taskアプリの完成!
function sendTask(e) {
MailApp.sendEmail('hoge@gmail.com', 'タスクが追加されました', e.namedValues['Task']);
}
function doGet() {
var template = HtmlService.createTemplateFromFile('index');
template.title = 'MyTaskApp';
template.tasks = getTasts();
return template.evaluate()
}
function getTasks() {
var sheet = SpreadsheetApp.getActiveSheet();
return sheet.getRange(2, 2, sheet.getLastRow() - 1, 1).getValues();
}
function sendReport() {
var to = 'hoge@gmail.com';
var subject = 'タスク一覧';
var url = 'https://script.google.com/macros/s/hoge/exec';
var body = getTasks().join('\n') + '\n\n' + url;
MailApp.sendEmail(to, subject, body);
}