











{"author":"Faiq Najib Al-Aziz","body":"\nA lot of Google Workspace tasks feel too small for a dedicated app, but doing them over and over gets old. Things like reminding clients about invoices, summarizing Google Form responses each weekend, or copying data between spreadsheets. **Google Apps Script** can automate them. It’s JavaScript that runs on Google’s servers and can access Sheets, Gmail, and Calendar. It’s free for standard accounts.\n\nFor me, writing JavaScript isn’t the most annoying part. It’s the back-and-forth debugging in the Apps Script editor: tiny logs, limited breakpoints, and slow execution. AI can help, but I still need to check its work.\n\nHere I build an invoice reminder for Google Sheets with AI assistance. Before opening Google’s editor, I test the logic on my laptop first.\n\n## The Example I Worked On\n\nThis example spreadsheet has five columns:\n\n```text\nA: Client Name     B: Amount      C: Due Date      D: Status   E: Last Reminded\nPT Maju Jaya       7,500,000      12/11/2026       UNPAID\nCV Berkah          1,200,000      11/11/2026       UNPAID\nPT Sejahtera       3,000,000      01/12/2026       UNPAID\n```\n\nThe script emails clients once a day when an invoice is due within ≤ 3 days, then records the reminder date so it doesn’t spam them. It skips paid invoices, avoids repeatedly emailing about overdue invoices, and doesn’t send another reminder today if the client got one yesterday.\n\n## I Keep the Logic Separate from Google Services\n\nIf I just ask AI to \"write an Apps Script for invoice reminders\", it might put everything in one block: read the sheet, calculate dates, format and send emails, then write back to rows. It’s hard to test the pieces without running them in Google. Each fix means copying the code into the editor again and guessing from the logs.\n\nSo I ask AI to split the code into two parts.\n\n**Part one: logic**, plain JavaScript with no Google service calls:\n\n```javascript\nfunction filterDueInvoices(rows, today, leadDays) {\n  // rows: [[client, amount, dueDate (Date), status, lastReminded (Date|null)], ...]\n  const soon = [];\n  for (const r of rows) {\n    const [client, amount, due, status, lastReminded] = r;\n    if (status !== \"UNPAID\") continue; // paid -\u003e skip\n    const daysLeft = Math.ceil((due - today) / 86400000);\n    if (daysLeft \u003c 0 || daysLeft \u003e leadDays) continue; // overdue or too far out -\u003e skip\n    if (lastReminded \u0026\u0026 (today - lastReminded) / 86400000 \u003c 3) continue; // 3-day dedup window\n    soon.push({ client, amount, due, daysLeft });\n  }\n  return soon;\n}\n\nfunction composeReminderEmail(inv) {\n  const dateStr = Utilities.formatDate(inv.due, \"Asia/Jakarta\", \"d MMMM yyyy\");\n  const formattedAmount = Number(inv.amount).toLocaleString(\"id-ID\");\n  const timing = inv.daysLeft === 0 ? \"today\" : `in ${inv.daysLeft} days`;\n  return {\n    subject: `Reminder: Invoice for ${inv.client} is due on ${dateStr}`,\n    body:\n      `Hello ${inv.client} team,\\n\\n` +\n      `Your invoice of Rp${formattedAmount} will be due ${timing} (${dateStr}).\\n` +\n      `Please let us know once payment has been processed.\\n\\n` +\n      `Thank you,\\nAkordium Lab`,\n  };\n}\n```\n\n**Part two: Google integration**, the bit that calls the services:\n\n```javascript\nfunction sendReminders() {\n  const sheet = SpreadsheetApp.openById(\"YOUR_SHEET_ID\")\n    .getSheetByName(\"Invoices\")\n    .getDataRange()\n    .getValues()\n    .slice(1); // discard header row\n\n  const today = new Date();\n  const due = filterDueInvoices(sheet, today, 3);\n\n  for (const inv of due) {\n    const mail = composeReminderEmail(inv);\n    GmailApp.sendEmail(\"finance@client.id\", mail.subject, mail.body);\n    // record reminder date in column E of the corresponding row\n    sheetRow(inv).setValue(today); // pseudo-code - see implementation notes\n  }\n}\n```\n\nThis way, **I can test the first part on my laptop** using Node.js and a few small mocks:\n\n```javascript\n// mock Google services - local test harness only\nconst sentEmails = [];\nglobal.Utilities = {\n  formatDate: (d, tz, fmt) =\u003e {\n    /* ...date formatting implementation... */\n  },\n};\nglobal.GmailApp = {\n  sendEmail: (to, subject, body) =\u003e sentEmails.push({ to, subject, body }),\n};\n```\n\nI set up six rows to cover those cases, then checked the results. That’s how I put this script together. The local test found things to fix right away.\n\n## Two Bugs I Found\n\nWhile testing locally, I found two bugs that were easy to miss by just reading the code:\n\n**First, timezone handling on dates.** The initial implementation calculated `new Date('2026-11-12')`, which is parsed as midnight **UTC**. Compared with `today` in WIB (UTC+7), the date arithmetic shifted: an invoice due in 2 days appeared due in 3 days. The mismatch showed up in the asserted day count. In Apps Script, make sure the project's `timeZone` in `appsscript.json` is configured (`\"timeZone\": \"Asia/Jakarta\"`). Otherwise, `new Date()` follows the project timezone, not your local one.\n\n**Second, a service method name.** The AI initially wrote `Utilities_formatDate(...)`, an underscore-style function name that does not exist in Apps Script. The actual method is `Utilities.formatDate(...)`. Both AI and people can make this kind of mistake when writing from memory without running the code.\n\nThe tests let two of the six rows pass the filter, as expected. The email showed `Rp7.500.000` and `12 November 2026`, and all the assertions passed. At least it didn’t just look right.\n\n## Setting Up the Trigger\n\nFrom the spreadsheet, open the Apps Script editor (Extensions -\u003e Apps Script) and paste in both parts. Then set up a trigger to run `sendReminders` every morning.\n\n```javascript\n// run once manually to set up a daily 07:00 AM trigger\nfunction setupTrigger() {\n  ScriptApp.newTrigger(\"sendReminders\")\n    .timeBased()\n    .atHour(7)\n    .everyDays(1)\n    .create();\n}\n```\n\nBefore using it, I check a few things:\n\n- **Email quota limits.** Free Gmail accounts are capped at ~100 emails/day via `GmailApp` (Workspace accounts have higher allowances). These reminders go out in small numbers each day, so the quota should be fine. Do not use this setup for blast campaigns to hundreds of recipients.\n- **Row write-backs.** To record the reminder timestamp in the right row, map the array index carefully. `getValues()` yields a zero-indexed array. When writing with `sheet.getRange(row, 5).setValue(today)`, use the actual sheet row number. Add `+1` for the header and `+1` because Sheets uses 1-based indexing.\n- **Failures must be observable.** Wrap `GmailApp.sendEmail` in a `try/catch` and log to `console.error`. A silent trigger failure is worse than doing the work manually: you think reminders were sent when they were not.\n\n## Notes for Asking AI for Help\n\nHere are a few things I put in the prompt:\n\n1. **Provide a real data schema** (column names and three sample rows), not a vague description. Concrete data shapes help the AI produce more accurate code.\n2. **Explicitly request two layers**: \"separate pure business logic from Google service invocations so it can be tested with Node.js\". Without this instruction, the code may end up bundled together.\n3. **List the edge cases**: already paid, overdue, and a 3-day dedup window. The AI will not guess your deduplication rules; spell out the business policy.\n4. **Test first, paste later.** After the logic passes local tests, copy it into Google's script editor. The remaining live checks are the integration details, such as sheet IDs, permissions, and trigger timing.\n\nI wrote about this in an [earlier article](/en/writing/2026/workflow-ai-assisted-development/), too: AI can help generate the code, but we still have to check it. This example uses Google Sheets, though the same workflow could work with Forms, Calendar, or Drive.\n\nIf you try this workflow or spot something I should fix, [Contact](/en/contact/). You can follow future posts via the [RSS feed](/en/writing/index.xml).\n","body_clean":"A lot of Google Workspace tasks feel too small for a dedicated app, but doing them over and over gets old. Things like reminding clients about invoices, summarizing Google Form responses each weekend, or copying data between spreadsheets. Google Apps Script can automate them. It’s JavaScript that runs on Google’s servers and can access Sheets, Gmail, and Calendar. It’s free for standard accounts.\nFor me, writing JavaScript isn’t the most annoying part. It’s the back-and-forth debugging in the Apps Script editor: tiny logs, limited breakpoints, and slow execution. AI can help, but I still need to check its work.\nHere I build an invoice reminder for Google Sheets with AI assistance. Before opening Google’s editor, I test the logic on my laptop first.\nThe Example I Worked On #This example spreadsheet has five columns:\nA: Client Name B: Amount C: Due Date D: Status E: Last Reminded PT Maju Jaya 7,500,000 12/11/2026 UNPAID CV Berkah 1,200,000 11/11/2026 UNPAID PT Sejahtera 3,000,000 01/12/2026 UNPAID The script emails clients once a day when an invoice is due within ≤ 3 days, then records the reminder date so it doesn’t spam them. It skips paid invoices, avoids repeatedly emailing about overdue invoices, and doesn’t send another reminder today if the client got one yesterday.\nI Keep the Logic Separate from Google Services #If I just ask AI to \u0026ldquo;write an Apps Script for invoice reminders\u0026rdquo;, it might put everything in one block: read the sheet, calculate dates, format and send emails, then write back to rows. It’s hard to test the pieces without running them in Google. Each fix means copying the code into the editor again and guessing from the logs.\nSo I ask AI to split the code into two parts.\nPart one: logic, plain JavaScript with no Google service calls:\nfunction filterDueInvoices(rows, today, leadDays) { // rows: [[client, amount, dueDate (Date), status, lastReminded (Date|null)], ...] const soon = []; for (const r of rows) { const [client, amount, due, status, lastReminded] = r; if (status !== \u0026#34;UNPAID\u0026#34;) continue; // paid -\u0026gt; skip const daysLeft = Math.ceil((due - today) / 86400000); if (daysLeft \u0026lt; 0 || daysLeft \u0026gt; leadDays) continue; // overdue or too far out -\u0026gt; skip if (lastReminded \u0026amp;\u0026amp; (today - lastReminded) / 86400000 \u0026lt; 3) continue; // 3-day dedup window soon.push({ client, amount, due, daysLeft }); } return soon; } function composeReminderEmail(inv) { const dateStr = Utilities.formatDate(inv.due, \u0026#34;Asia/Jakarta\u0026#34;, \u0026#34;d MMMM yyyy\u0026#34;); const formattedAmount = Number(inv.amount).toLocaleString(\u0026#34;id-ID\u0026#34;); const timing = inv.daysLeft === 0 ? \u0026#34;today\u0026#34; : `in ${inv.daysLeft} days`; return { subject: `Reminder: Invoice for ${inv.client} is due on ${dateStr}`, body: `Hello ${inv.client} team,\\n\\n` + `Your invoice of Rp${formattedAmount} will be due ${timing} (${dateStr}).\\n` + `Please let us know once payment has been processed.\\n\\n` + `Thank you,\\nAkordium Lab`, }; } Part two: Google integration, the bit that calls the services:\nfunction sendReminders() { const sheet = SpreadsheetApp.openById(\u0026#34;YOUR_SHEET_ID\u0026#34;) .getSheetByName(\u0026#34;Invoices\u0026#34;) .getDataRange() .getValues() .slice(1); // discard header row const today = new Date(); const due = filterDueInvoices(sheet, today, 3); for (const inv of due) { const mail = composeReminderEmail(inv); GmailApp.sendEmail(\u0026#34;finance@client.id\u0026#34;, mail.subject, mail.body); // record reminder date in column E of the corresponding row sheetRow(inv).setValue(today); // pseudo-code - see implementation notes } } This way, I can test the first part on my laptop using Node.js and a few small mocks:\n// mock Google services - local test harness only const sentEmails = []; global.Utilities = { formatDate: (d, tz, fmt) =\u0026gt; { /* ...date formatting implementation... */ }, }; global.GmailApp = { sendEmail: (to, subject, body) =\u0026gt; sentEmails.push({ to, subject, body }), }; I set up six rows to cover those cases, then checked the results. That’s how I put this script together. The local test found things to fix right away.\nTwo Bugs I Found #While testing locally, I found two bugs that were easy to miss by just reading the code:\nFirst, timezone handling on dates. The initial implementation calculated new Date('2026-11-12'), which is parsed as midnight UTC. Compared with today in WIB (UTC+7), the date arithmetic shifted: an invoice due in 2 days appeared due in 3 days. The mismatch showed up in the asserted day count. In Apps Script, make sure the project\u0026rsquo;s timeZone in appsscript.json is configured (\u0026quot;timeZone\u0026quot;: \u0026quot;Asia/Jakarta\u0026quot;). Otherwise, new Date() follows the project timezone, not your local one.\nSecond, a service method name. The AI initially wrote Utilities_formatDate(...), an underscore-style function name that does not exist in Apps Script. The actual method is Utilities.formatDate(...). Both AI and people can make this kind of mistake when writing from memory without running the code.\nThe tests let two of the six rows pass the filter, as expected. The email showed Rp7.500.000 and 12 November 2026, and all the assertions passed. At least it didn’t just look right.\nSetting Up the Trigger #From the spreadsheet, open the Apps Script editor (Extensions -\u0026gt; Apps Script) and paste in both parts. Then set up a trigger to run sendReminders every morning.\n// run once manually to set up a daily 07:00 AM trigger function setupTrigger() { ScriptApp.newTrigger(\u0026#34;sendReminders\u0026#34;) .timeBased() .atHour(7) .everyDays(1) .create(); } Before using it, I check a few things:\nEmail quota limits. Free Gmail accounts are capped at ~100 emails/day via GmailApp (Workspace accounts have higher allowances). These reminders go out in small numbers each day, so the quota should be fine. Do not use this setup for blast campaigns to hundreds of recipients. Row write-backs. To record the reminder timestamp in the right row, map the array index carefully. getValues() yields a zero-indexed array. When writing with sheet.getRange(row, 5).setValue(today), use the actual sheet row number. Add +1 for the header and +1 because Sheets uses 1-based indexing. Failures must be observable. Wrap GmailApp.sendEmail in a try/catch and log to console.error. A silent trigger failure is worse than doing the work manually: you think reminders were sent when they were not. Notes for Asking AI for Help #Here are a few things I put in the prompt:\nProvide a real data schema (column names and three sample rows), not a vague description. Concrete data shapes help the AI produce more accurate code. Explicitly request two layers: \u0026ldquo;separate pure business logic from Google service invocations so it can be tested with Node.js\u0026rdquo;. Without this instruction, the code may end up bundled together. List the edge cases: already paid, overdue, and a 3-day dedup window. The AI will not guess your deduplication rules; spell out the business policy. Test first, paste later. After the logic passes local tests, copy it into Google\u0026rsquo;s script editor. The remaining live checks are the integration details, such as sheet IDs, permissions, and trigger timing. I wrote about this in an earlier article, too: AI can help generate the code, but we still have to check it. This example uses Google Sheets, though the same workflow could work with Forms, Calendar, or Drive.\nIf you try this workflow or spot something I should fix, Contact. You can follow future posts via the RSS feed.\n","canonical":"https://najib.id/en/writing/2026/otomasi-google-apps-script-ai/","description":"An invoice reminder example with Apps Script. I asked AI to help write it, then tested the logic on my laptop before copying it into Google’s editor.","language":"en","lastmod":"2026-12-02T21:36:50Z","publishedAt":"2026-11-10T00:00:00Z","readingTime":6,"section":"writing","series":null,"tags":["ai","automation","productivity","workflow"],"title":"Automating Google Apps Script with AI Assistance","type":"writing","url":"https://najib.id/en/writing/2026/otomasi-google-apps-script-ai/","wordCount":1157}
