試してみたブログ

AI関連・iPhone/Pixelなどのガジェット・音声入力・サーマルプリンタなど興味をある事をどんどん試してみた際の記録

SaaS課金解約し忘れを通知するGASを作成してみた

背景

tameshitemita.blog

  • 昨日のエントリーでメール通知でなんとかする方向性を打ち立てた
  • 実際に作っていた

試してみた

B列・・・サービス名

G列・・・金額

N列・・・次回課金日(YYYY/MM/DD or 毎月)

function sendSaaSNotification() {
  const sheet = SpreadsheetApp.getActiveSheet();
  const lastRow = sheet.getLastRow();

  const startRow = 3; // 3行目から
  const numRows = lastRow - startRow + 1;
  if (numRows <= 0) return;

  const COL_SERVICE = 2;   // ★サービス名:B列
  const COL_AMOUNT  = 7;   // 金額:G列
  const COL_NEXT    = 14;  // 次回課金:N列

  const range = sheet.getRange(startRow, 1, numRows, sheet.getLastColumn());
  const values = range.getValues();

  const today = new Date();
  const dayMs = 24 * 60 * 60 * 1000;

  let lines = [];
  let total = 0;

  values.forEach(function(row) {
    const service = row[COL_SERVICE - 1];
    const amount  = row[COL_AMOUNT - 1];
    const nextVal = row[COL_NEXT - 1];

    // デバッグ用(不要になったら消してOK)
    Logger.log('service=' + service + ', nextVal=' + nextVal + ' (' + (typeof nextVal) + ')');

    let isTarget = false;

    // 「毎月」の文字列
    if (typeof nextVal === 'string' && nextVal.indexOf('毎月') !== -1) {
      isTarget = true;
    }

    // 日付で 実行日〜14日以内
    if (nextVal instanceof Date) {
      const diffDays = Math.floor((nextVal.getTime() - today.getTime()) / dayMs);
      if (diffDays >= 0 && diffDays <= 14) {
        isTarget = true;
      }
    }

    if (isTarget && service) {
      const amt = Number(amount) || 0;
      lines.push(service + ' : ' + amt.toLocaleString() + '円');
      total += amt;
    }
  });

  if (lines.length === 0) {
    return; // 対象がなければ何もしない
  }

  const sheetUrl = 'https://docs.google.com/spreadsheets/d/1PmlVGFofA-ZNhaUPlro1HO0Xr6r_n0_Xcn-11-Kk1ws/edit?gid=0#gid=0';
  const body =
    lines.join('\n') +
    '\n\n合計: ' + total.toLocaleString() + '円' +
    '\n\nシートへのリンク:\n' + sheetUrl;

  const to = 'm1104m@gmail.com';
  const subject = 'SaaSまとめ通知';

  GmailApp.sendEmail(to, subject, body); // MailAppでも可[web:21][web:29]
}

トリガー

まとめ

いったんこの運用で解約が進むかを試してみる。毎月思ったよりもデカイ負担だったので、早急に解約を進める。