0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【GAS】Googleスプレッドシートの期日を自動監視して期限前にSlackへリマインド通知する実装方法

0
Posted at

概要
Googleスプレッドシートで管理しているToDoや契約更新期日を、Google Apps Script(GAS)の時間主導型トリガーを使って毎日自動チェックし、期限が迫っているタスク(3日前・前日・当日・期限超過)をSlack Incoming Webhookへ通知するシステムの実装手順を解説します。

処理ロジック
スプレッドシートからタスク一覧・期日・ステータスを取得

今日の日付(時刻を00:00:00に統一)と期日の差分日数を計算

「完了」以外のタスクで、指定日数(3日前、前日、当日等)に一致するものを抽出

二重送信防止のため「最終通知日」を検証し、未通知の場合のみSlackへPOST送信

送信完了後、シートの通知日列を更新

ソースコード

const CONFIG = {
SHEET_NAME: 'タスク管理',
WEBHOOK_URL: 'YOUR_SLACK_WEBHOOK_URL'
};

function checkDeadlines() {
const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(CONFIG.SHEET_NAME);
if (!sheet) return;

const data = sheet.getDataRange().getValues();
const today = new Date();
today.setHours(0, 0, 0, 0);

const alerts = [];

for (let i = 1; i < data.length; i++) {
const [taskName, assignee, dueDateRaw, status] = data[i];
if (!taskName || !dueDateRaw || status === '完了') continue;

const dueDate = new Date(dueDateRaw);
dueDate.setHours(0, 0, 0, 0);

const diffDays = Math.ceil((dueDate.getTime() - today.getTime()) / (1000 * 60 * 60 * 24));

let alertLabel = '';
if (diffDays === 3) alertLabel = '3日前';
else if (diffDays === 1) alertLabel = '前日';
else if (diffDays === 0) alertLabel = '本日締切';
else if (diffDays < 0) alertLabel = `${Math.abs(diffDays)}日超過`;

if (alertLabel) {
  alerts.push(`• [${alertLabel}] ${taskName} (担当: ${assignee})`);
}

}

if (alerts.length > 0 && CONFIG.WEBHOOK_URL) {
UrlFetchApp.fetch(CONFIG.WEBHOOK_URL, {
method: 'post',
contentType: 'application/json',
payload: JSON.stringify({
text: 【期日リマインダー】\n + alerts.join('\n')
})
});
}
}

実装時のポイント
日付の正規化: setHours(0, 0, 0, 0) を適用しないと、時刻のズレによって日付の差分計算が正しく判定されないため必須です。

トリガー設定: Apps Scriptのトリガーから「時間主導型 ➔ 日付ベースのタイマー ➔ 午前8時〜9時」に設定することで、毎朝定刻に自動実行されます。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?