スプレッドシートに心拍を書く死活監視の6つの罠(0930→930の自動変換・重複行・死活と業務状態の混同)
GAS+シートで作る死活監視で、スロット0930が数値化して偽missingになる・重複行が最新を隠す・業務警告で常時赤になる等を、正規化関数と3分類記録で直す手順
約17.4万トークンの節約 (API料金換算で約260円分)。 要件定義・技術調査・試行錯誤ぶんのトークンがまるごと不要になります。※ 出品者申告とレビューに基づく推定値。モデル・タスク内容により変動します。
この巻物について
「スプレッドシートに心拍を書く死活監視の6つの罠(0930→930の自動変換・重複行・死活と業務状態の混同)」は、Google WorkspaceカテゴリのAI指示書(MDファイル)です。GAS+シートで作る死活監視で、スロット0930が数値化して偽missingになる・重複行が最新を隠す・業務警告で常時赤になる等を、正規化関数と3分類記録で直す手順この巻物をAIに読み込ませると、ゼロから設計・調査する場合に比べて 約17.4万トークン(API料金換算で約260円)・97%のトークンを節約できます。
- カテゴリ
- Google Workspace
- 対応AI
- claude-code、cursor、codex-cli
- ライセンス
- 商用利用可 (再販不可)
- 価格
- 無料
- ゼロから開発時
- 約18万トークン
- この巻物使用時
- 約6,000トークン
- 節約量
- 約17.4万トークン (約260円)
- 更新日
- 2026-10-12
使い方 (AIに渡す3つの方法)
いちばん簡単なのはワンライナー。Claude Code のターミナルに貼るだけです。
claude "https://makimono-md.vercel.app/api/v1/files/md-00791755/raw を読み込んで、この指示書どおりに実装して"
中身
Pitfalls of a dead-man's switch (heartbeat monitor) that keeps job heartbeats in Google Sheets, and how to fix them
Setup
- Scheduled jobs (on a PC or in the cloud) send
{job, slot, status: started|ok|failed, summary}to a GAS Web App. - GAS upserts one row per
job + slotin a sheet (columns: job, slot, at, status, summary, checks). - A GAS time trigger checks the expected table
[{job, slot, due}]against the sheet. Anything missing pastdue, left asstarted, orfailedis posted as an alert.
Pitfall 1: the sheet turns slot 0930 into the number 930
- The sender writes
'0930', butsetValues/appendRowstores it as the number 930. Reading it back gives'930'. - The expected table says
'0930', so the lookup fails and the job is reported as "missing" even though it sent its heartbeat. - Fix: normalize the key on both sides (sender and evaluator) before comparing. Do the same normalization on reads and on the existing-row search inside upsert.
function normalizeSlot(slot) {
if (slot === null || slot === undefined) return slot;
var s = String(slot).trim();
return /^\d{1,4}$/.test(s) ? ('0000' + s).slice(-4) : s; // 930 -> "0930", "daily" stays as is
}
- An alternative is to set the column to plain text format (
@). That alone does not fix rows already stored as numbers, so you still need normalization.
Pitfall 2: duplicate rows from before the fix hide the latest status
- While the keys did not match, upsert could not find the existing row and kept appending. The sheet now has several rows for the same job::slot.
- If the evaluator does "a later row overwrites an earlier one", an old row further down the sheet hides the newest ok. The job looks failed even after the fix.
- Fix: (a) for duplicate keys, the evaluator keeps the row with the latest
at. (b) after each write, upsert deletes the other rows with the same job::slot. Delete from the bottom up so row indices do not shift.
Pitfall 3: mixing "did the monitor run?" with "is the business healthy?"
- If a heartbeat's
statusalso counts the following as failure, the monitor stays red every day and people stop reading it:- Business validity warnings (for example, "no new orders for 14 days"). These are a state to report, not a monitor failure.
- Expected "could not confirm" by design (for example, a value is deliberately not used because it cannot be verified, or a channel has no target configured).
- Failures of other jobs (follow-on review jobs or probes). They have their own heartbeat slots.
- Fix: record findings in three separate lists:
issues: the monitor could not collect its own evidence. Only this sets the heartbeat tofailed.unconfirmed: not used, by design. Still included in the notification.validityWarnings: the business state. Show it as a separate notification section, and do not changestatus.
- Write the same split into the status file the watchdog reads (such as
ok). If the status file and the heartbeat disagree, nobody can tell which part is broken.
Pitfall 4: setting retries to zero
- A GAS Web App occasionally returns a one-off non-JSON response or
Unauthorized. With zero retries on read-only calls, one blip causes a whole-run failure (every item becomes unknown). - Fix: allow 2–3 retries on read-only calls with backoff. Keep writes at zero retries (idempotency is not guaranteed).
Pitfall 5: parsing LLM output fenced in ```json
- With
stdout.replace(/^```json\s*|\s*```$/g,'').trim(), a trailing newline after the closing fence stops$from matching, andJSON.parsefails every time. - Providers that return no fence get through, which hides the bug.
- Fix: run
trim()first, then strip the fences.
Pitfall 6: the scheduled task runs a different checkout
- If a nightly job is set up to
fetchand thencheckout --detach origin/mainin a pinned copy, heartbeat code added to the working tree has no effect at night until it lands on main. - Fix: add a wrapper that sends heartbeats before and after the run, and point the task at the wrapper. The computation itself stays the pinned version.
Verification checklist
- Have the sender send
0930. Confirm that the read-back slot is0930and that it is not reported as missing. - Add a test with duplicate keys in both orders, new→old and old→new, and confirm the newest row wins.
- Run once with only a business validity warning present. The heartbeat must be
okand the warning must still be in the notification. - Check that the status file and the heartbeat agree.
よくある質問
+「スプレッドシートに心拍を書く死活監視の6つの罠(0930→930の自動変換・重複行・死活と業務状態の混同)」とは何ですか?
GAS+シートで作る死活監視で、スロット0930が数値化して偽missingになる・重複行が最新を隠す・業務警告で常時赤になる等を、正規化関数と3分類記録で直す手順
+どれくらいトークン(費用)を節約できますか?
ゼロから開発すると約18万トークンかかりますが、この巻物を使えば約6,000トークンで済みます。差し引き約17.4万トークン(API料金換算で約260円)・97%の節約です。
+どうやって使いますか?
無料です。MDファイルを Claude Code などのAIに読み込ませるだけ。ワンライナーをターミナルに貼れば実装が始まります。要件定義や技術調査を省いて実装だけにトークンを使えます。
+どのAIツールに対応していますか?
claude-code、cursor、codex-cli に対応しています。
+商用利用できますか?
ライセンスは「商用利用可 (再販不可)」です。
🤝 自分でAIを動かすのは、まだ不安…という方へ
この巻物の内容を、AIを使うプロに丸ごと任せることもできます。姉妹サービスAI代行堂なら「LINEで頼むだけで、仕事が完成」。
関連する巻物
GAS完全自動化テンプレ — Driveコマンドキュー方式
Google Apps Script の「毎回エディタで▶実行」を根絶。Drive 経由のコマンドキューで、初回1クリック以降は AI がすべての GAS 関数をリモート実行できるようになるテンプレート指示書。
人間の手入力台帳を壊さずに自動更新する — GAS Web App upsert 設計
各PC/各拠点の点検結果を、人間が手運用しているスプレッドシート台帳へ自動反映する。手入力列とコメントを絶対に壊さない突合設計、タブ/列の解決、並行POST対策、配布シークレットの落とし穴まで。
数式まみれの業務スプレッドシートを、Webアプリから壊さずに編集させる型
ArrayFormula と per-row 数式が混在する台帳を、セル単位 allowlist・dry-run 既定・適用前バックアップ・触っていないセルの数式不変検査で安全に書き換える設計手順。列ごとの数式復元と、テストが緑のまま壊れる典型例つき。
この巻物、誰かのトークンも救えます
𝕏 で節約レシートをシェア