GAS SAMPLE

月次の集計とメール通知を、
スプレッドシートだけで自動化

Googleスプレッドシートに付いている「Apps Script(GAS)」で動く見本です。追加の費用はかからず、毎月決まった日に、売上を月別にまとめて報告用のシートを作り、担当者にメールで知らせます。

この見本でできること
/**
 * 「売上」シート(A:日付 B:商品名 C:金額)を月別に集計し、
 * 「月次報告」シートに書き出して、担当者にメールで知らせる。
 */
const CONFIG = {
  SOURCE: '売上',
  REPORT: '月次報告',
  MAIL_TO: 'tantou@example.com', // 通知先(見本)
};

function monthlyReport() {
  const ss = SpreadsheetApp.getActive();
  const rows = ss.getSheetByName(CONFIG.SOURCE).getDataRange().getValues().slice(1);

  const byMonth = {};
  rows.forEach(([date, item, amount]) => {
    if (!(date instanceof Date) || isNaN(amount)) return; // 読めない行は飛ばす
    const ym = Utilities.formatDate(date, 'Asia/Tokyo', 'yyyy-MM');
    byMonth[ym] = (byMonth[ym] || 0) + Number(amount);
  });

  const months = Object.keys(byMonth).sort();
  if (!months.length) return; // データがなければ何もしない
  const table = [['月', '売上', '前月比']];
  months.forEach((m, i) => {
    const prev = i ? byMonth[months[i - 1]] : null;
    table.push([m, byMonth[m], prev ? (byMonth[m] - prev) / prev : '']);
  });

  const sh = ss.getSheetByName(CONFIG.REPORT) || ss.insertSheet(CONFIG.REPORT);
  sh.clear();
  sh.getRange(1, 1, table.length, 3).setValues(table);
  sh.getRange(2, 2, table.length - 1, 1).setNumberFormat('#,##0"円"');
  sh.getRange(2, 3, table.length - 1, 1).setNumberFormat('+0.0%;-0.0%');

  const last = months[months.length - 1];
  MailApp.sendEmail(CONFIG.MAIL_TO, `【月次報告】${last} の売上`,
    `${last} の売上は ${byMonth[last].toLocaleString()} 円でした。\n${ss.getUrl()}`);
}

// 毎月1日の朝8時に動かす設定(最初に1回だけ実行)
function setTrigger() {
  ScriptApp.newTrigger('monthlyReport').timeBased().onMonthDay(1).atHour(8).create();
}
ご依頼のときに作り替えられること