GAS SAMPLE
月次の集計とメール通知を、
スプレッドシートだけで自動化
Googleスプレッドシートに付いている「Apps Script(GAS)」で動く見本です。追加の費用はかからず、毎月決まった日に、売上を月別にまとめて報告用のシートを作り、担当者にメールで知らせます。
この見本でできること
- 「売上」シートの明細(日付・商品名・金額)を、月別・商品別に集計
- 「月次報告」シートに、前月比つきの表を書き出す(毎回作り直すので、手で直す必要なし)
- 毎月1日の朝に自動で動かし、集計が終わったらメールで通知
/** * 「売上」シート(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(); }
ご依頼のときに作り替えられること
- 楽天・Amazon・自社ECなど、販路ごとのCSVの取り込み
- 広告費や商品別の利益の集計、グラフの自動作成
- チェック作業の自動化(価格・ポイント・期間の設定漏れの検出など)
- LINEやSlackへの通知