Skip to content

Instantly share code, notes, and snippets.

@arukayies
Last active April 25, 2020 04:13
Show Gist options
  • Save arukayies/45f658bf5410b40fe36f81f668d7e2bd to your computer and use it in GitHub Desktop.
Save arukayies/45f658bf5410b40fe36f81f668d7e2bd to your computer and use it in GitHub Desktop.
GASを使ってスプレッドシートの内容をSlackに通知させる方法
/*
関数概要
スプレッドシートのステータスが「新規」に編集されたら、Slackに内容を通知する
引数
e イベントオブジェクト(起動時の情報が含まれています)
戻り値
なし
*/
function sheet_postContent(e) {
// ヘッダーの行番号
const hedaerRow = 2;
// 通知させる内容が書かれている列番号
const contentCol = 5;
// 編集されたシート
const sheet = e.source.getActiveSheet();
// 編集されたセル
const currentCell = e.source.getActiveCell();
// 編集された値
const currentValue = currentCell.getValue();
// 編集されたセルの行番号
const currentRow = currentCell.getRow();
// ログ
Logger.log("行番号:" + currentRow);
Logger.log("編集された値:" + currentValue);
// ヘッダー以降で、編集された値は「新規」の場合にSlack通知させる
if (hedaerRow < currentRow && currentValue == "新規") {
var content = sheet.getRange(currentRow, contentCol).getValue();
Logger.log("送信する内容:" + content);
slack_postMessage(content);
}
}
/*
関数概要
Slackに指定テキストを#sampleに送信する
引数
message Slackに送信したいテキスト
戻り値
なし
*/
function slack_postMessage(message) {
const token = "Slackで取得したトークン";
const apiUrl = "https://slack.com/api/chat.postMessage?token=" + token;
const payload = {
"channel": "sample",
"text": message
};
const options = {
"method": "post",
"payload": payload
};
UrlFetchApp.fetch(apiUrl, options);
}
@arukayies
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment