Last active
February 19, 2025 06:14
-
-
Save ttsukagoshi/5b60ff9a92e89cd39d1c445b23c4b936 to your computer and use it in GitHub Desktop.
Gmailの受信トレイにある未読メールの件数をGoogleチャットで通知する、Spreadsheet-boundのGoogle Apps Script。
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
/** | |
* Gmailの受信トレイにある未読メールの件数をGoogleチャットで通知する、Spreadsheet-boundのGoogle Apps Script。 | |
* 前提: | |
* スプレッドシートに以下のように2列が設定されていること: | |
* | ACCOUNT | WEBHOOK_URL | | |
* | -------------------- | --------------------------- | | |
* | [email protected] | (Googleチャットのwebhook URL) | | |
* スクリプトは、実行しているアカウントと[email protected]が一致すれば、 | |
* その受信トレイにある未読メールの件数をWebhookを設定したチャットに投稿する。 | |
* [email protected]は当該チャットに参加している必要がある。 | |
* Webhook URLの取得については https://developers.google.com/chat/how-tos/webhooks を参照のこと。 | |
* あとは、未読メールの件数チェックを行いたい頻度によってトリガーを設定すれば完了。 | |
*/ | |
const SHEET_NAME_CONFIG = '設定'; // ACCOUNTとWEBHOOK_URLが設定されたシート名 | |
const JAPANESE_HOLIDAY_CAL_ID = 'ja.japanese#[email protected]'; // Google Calendarの「日本の休日」 | |
/** | |
* スプレッドシートを開いた時に、手動実行用のメニューを設定する。 | |
*/ | |
function onOpen() { | |
SpreadsheetApp.getUi() | |
.createMenu('未読メール確認(手動)') | |
.addItem('未読メール確認', 'checkAndNotify') | |
.addItem('リマインダー送信', 'reminderCheckRunning') | |
.addToUi(); | |
} | |
/** | |
* 未読メールの件数チェック | |
*/ | |
function checkAndNotify() { | |
const now = new Date(); | |
if (!isWeekendOrHolidayJa_(now)) { | |
// 週末か祝休日でなければ(=平日ならば)未読メールチェックとチャットへの通知を行う | |
const ss = SpreadsheetApp.getActiveSpreadsheet(); | |
const myEmail = Session.getActiveUser().getEmail(); | |
const configArr = ss.getSheetByName(SHEET_NAME_CONFIG) | |
.getDataRange() | |
.getValues(); | |
const configHeader = configArr.shift(); | |
const config = configArr.map(row => { | |
return configHeader.reduce((o, k, i) => { | |
o[k] = row[i] | |
return o; | |
}, {}); | |
}); | |
config.forEach(row => { | |
if (row.ACCOUNT === myEmail) { | |
let inboxThreadCount = GmailApp.getInboxThreads().length; | |
let unreadMailCount = GmailApp.getInboxUnreadCount(); | |
let message = `【${myEmail}】${inboxThreadCount} 件のスレッド(うち未読メール ${unreadMailCount} 件)があります。`; | |
postToChat_(row.WEBHOOK_URL, message); | |
} | |
}); | |
} | |
} | |
/** | |
* チェックがトリガー実行されていることのリマインダー | |
*/ | |
function reminderCheckRunning() { | |
const ss = SpreadsheetApp.getActiveSpreadsheet(); | |
const myEmail = Session.getActiveUser().getEmail(); | |
const configArr = ss.getSheetByName(SHEET_NAME_CONFIG) | |
.getDataRange() | |
.getValues(); | |
const configHeader = configArr.shift(); | |
const config = configArr.map(row => { | |
return configHeader.reduce((o, k, i) => { | |
o[k] = row[i] | |
return o; | |
}, {}); | |
}); | |
config.forEach(row => { | |
if (row.ACCOUNT === myEmail) { | |
postToChat_(row.WEBHOOK_URL, `リマインダー:${myEmail}で未読メール件数チェックが設定されています。設定は${ss.getUrl()}にて確認・変更できます。`); | |
} | |
}); | |
} | |
/** | |
* 入力したDateオブジェクトが休日(土日または日本の祝日)かどうかを判定する。 | |
* スクリプトのタイムゾーンがJST (Asia/Tokyo)となっている前提。 | |
* @param {Date} dateObj | |
* @return {boolean} | |
*/ | |
function isWeekendOrHolidayJa_(dateObj) { | |
var weekday = dateObj.getDay(); // Assuming that the script's time zone is set to JST (Asia/Tokyo) | |
var holidayEventsJa = | |
CalendarApp.getCalendarById(JAPANESE_HOLIDAY_CAL_ID).getEventsForDay(dateObj); | |
return weekday === 0 || weekday === 6 || holidayEventsJa.length > 0; | |
} | |
/** | |
* Google Chatの指定したwebhook URLに対してメッセージを送信する。 | |
* @param {String} webhookUrl Google Chatで設定したwebhook URL。 https://developers.google.com/chat/how-tos/webhooks | |
* @param {String} message 送信するメッセージ | |
*/ | |
function postToChat_(webhookUrl, message) { | |
UrlFetchApp.fetch(webhookUrl, { | |
method: 'POST', | |
contentType: 'application/json; charset=UTF-8', | |
payload: JSON.stringify({ | |
text: message, | |
}), | |
}); | |
} |
onOpen
を定義- 未読メール確認で、受信トレイにある全スレッド数も記載するようメッセージを調整
checkAndNotify
のメッセージで、アカウント名(myEmail
)を表示するように追記(スマホのアプリ版だと、webhook名が「webhook」としか表示されず、複数メールアカウントでこのチェックを走らせている場合に、どのメールについての通知かわからなくなるため)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
定期的に(月次をイメージ)、未読メールの件数チェックが実行されていることをリマンドする通知
reminderCheckRunning
を追加