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とGmailへリマインド通知する方法

0
Posted at

概要
Googleスプレッドシートに記録された契約・サブスクリプションの更新期日を、Google Apps Script(GAS)の日付トリガーで毎日自動チェックし、指定期日(30日前、14日前、7日前、当日など)を迎えた項目を担当者へSlack通知およびGmail自動送信する設計を解説します。

システムの特長と設計思想
二重通知の防止: 同日に何回トリガーが走っても連動しないよう、実行成功時に「最終通知日」カラムへ当日日付を書き込み、次回判定時にスキップします。

マルチチャネル通知: チーム全体共有用のSlack Incoming Webhookと、担当者個別の GmailApp.sendEmail() を同時発火させます。

完全ソースコード

const CONFIG = {
SHEET_NAME: '契約・サブスク管理',
SLACK_WEBHOOK_URL: 'YOUR_SLACK_WEBHOOK_URL'
};

function checkContractExpirations() {
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 todayStr = Utilities.formatDate(today, Session.getScriptTimeZone(), 'yyyy/MM/dd');

for (let i = 1; i < data.length; i++) {
const [name, vendor, email, dateRaw, cost, status, lastNotified] = data[i];
if (!name || !dateRaw || status === '解約済み' || status === '更新完了') continue;

const renewalDate = new Date(dateRaw);
renewalDate.setHours(0, 0, 0, 0);

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

let label = '';
if (diffDays === 30) label = '30日前';
else if (diffDays === 7) label = '7日前';
else if (diffDays === 0) label = '本日更新期限';

const lastNotifiedStr = lastNotified instanceof Date 
  ? Utilities.formatDate(lastNotified, Session.getScriptTimeZone(), 'yyyy/MM/dd')
  : String(lastNotified);

if (label && lastNotifiedStr !== todayStr) {
  // Slack通知処理
  if (CONFIG.SLACK_WEBHOOK_URL) {
    UrlFetchApp.fetch(CONFIG.SLACK_WEBHOOK_URL, {
      method: 'post',
      contentType: 'application/json',
      payload: JSON.stringify({
        text: `【契約更新アラート: ${label}】${name} (${vendor}) - 期日: ${Utilities.formatDate(renewalDate, 'JST', 'yyyy/MM/dd')}`
      })
    });
  }

  // Gmail個別送信処理
  if (email && email.includes('@')) {
    GmailApp.sendEmail(email, `【要確認】契約更新通知: ${name}`, `${email} 様\n\n${name} の更新期限(${label})が近づいています。`);
  }

  // 最終通知日の記録
  sheet.getRange(i + 1, 7).setValue(todayStr);
}

}
}

ポイント
時刻の正規化: setHours(0, 0, 0, 0) で時刻を完全にカットすることで、日付単位の計算精度を確実に保ちます。

無駄なコストカット: サードパーティのSaaSやデータベースを挟まず、Google Workspace標準機能(スプレッドシート+GAS+Gmail)のみで安全かつ無料で運用可能です。

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?