Created
August 29, 2023 18:18
-
-
Save so298/9d4aa609c036ad1d92033b56f3603ae7 to your computer and use it in GitHub Desktop.
カウントダウンbot by Google App Script
This file contains 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 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); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
使い方
main.gs
をGASのエディタにコピーし、sendNotification
関数に対して適当なトリガを設定するWEBHOOK_URL
にwebhookのURLを追加する