Skip to content

Instantly share code, notes, and snippets.

@so298
Created August 29, 2023 18:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save so298/9d4aa609c036ad1d92033b56f3603ae7 to your computer and use it in GitHub Desktop.
Save so298/9d4aa609c036ad1d92033b56f3603ae7 to your computer and use it in GitHub Desktop.
カウントダウンbot by Google App Script
function sendNotification() {
const webhookUrl = PropertiesService.getScriptProperties().getProperty('WEBHOOK_URL');
const spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
const sheet = spreadsheet.getSheetByName("日程表");
const lastRow = sheet.getLastRow();
const values = sheet.getRange(`A2:B${lastRow}`)
.getValues();
function convertToMessage(values) {
const today = new Date()
const text = values.map(([examDateStr, examName]) => { // 残り日数の計算
const examDate = new Date(examDateStr);
const remainDays = Math.ceil((examDate.getTime() - today.getTime()) / (1000 * 60 * 60 * 24));
return {
examName,
remainDays,
examDate
};
})
.filter(({ // すでに終了した試験を排除
remainDays,
}) => remainDays >= 0)
.sort((a, b) => a.examDate - b.examDate) // 日付で昇順にソート
.map(({
examName,
remainDays,
examDate
}) => {
const textSingle = `• ${examName} (${examDate.getMonth() + 1}/${examDate.getDate()}) まで残り *${remainDays}日* です。`
return textSingle;
}
)
.join('\n');
return `*${today.getFullYear()}/${today.getMonth() + 1}/${today.getDate()}*\n` + text;
}
const message = convertToMessage(values);
const payload = {
"text": message,
"icon_emoji": ":calendar:",
"username": "カウントダウンbot"
};
const options = {
"method": "post",
"contentType": "application/json",
"payload": JSON.stringify(payload)
};
UrlFetchApp.fetch(webhookUrl, options);
}
@so298
Copy link
Author

so298 commented Aug 29, 2023

使い方

  1. 適当なGoogle Spread Sheetを用意し、「日程表」という名前のシートを作成する
  2. 「日程表」シートのA列を日付、B列を予定名として、2行目以降に予定を記入する
  3. main.gsをGASのエディタにコピーし、sendNotification関数に対して適当なトリガを設定する
  4. Slackにwebhookのアプリを追加して、スクリプトのプロパティのWEBHOOK_URLにwebhookのURLを追加する

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