Finance wants the month-end export on the last business day. Not the 30th, not the 31st: the last weekday that isn’t a company holiday. Apps Script’s monthly trigger only offers “day N of the month”, and day N is a Saturday a few times a year.
The fix is boring and dependable: a daily trigger, and a guard at the top of the job that asks “is today the day?” in the spreadsheet’s timezone. On every other day the job exits in a few milliseconds.
TL;DR
- Install one daily time-driven trigger (morning, not midnight)
- First line of the job:
isLastBusinessDayOfMonth(new Date(), tz, holidays), elsereturn tz= the spreadsheet’s timezone, not the script’s or the server’s- Holidays live on a Config tab as
YYYY-MM-DDtext so ops can edit them - A
DRY_RUNswitch plus a “pretend today is…” override lets you rehearse month end any day - Stamp the month you ran so a manual re-run can’t double-send
Using the Apps Script editor a lot? NitroGAS drops free themes & snippets right into script.google.com — optional Co-Pilot when you want a boost.
What not to do
- Don’t use
onMonthDay(30)(or 28 “to be safe”). That’s the wrong day most months - Don’t compare
new Date().getDate()on the server. Late-night runs land on the wrong calendar date in another zone - Don’t hardcode holidays in the script. Ops will need to add one in December and won’t open the editor
- Don’t install the trigger from inside the job. You’ll end up with one per run
- Don’t wait for the real month end to find out it works
The pattern
1. One daily trigger, installed once
function installMonthEndTrigger() {
var exists = ScriptApp.getProjectTriggers().some(function (t) {
return t.getHandlerFunction() === 'monthEndJob';
});
if (exists) return;
ScriptApp.newTrigger('monthEndJob').timeBased().everyDays(1).atHour(7).create();
}
Same shape as Setup your triggers once (no doppelgängers): check first, then create. atHour(7) uses the script project’s timezone, so set that to match the spreadsheet in Project Settings.
2. The date guard, in the sheet’s timezone
function monthEndJob() {
var ss = SpreadsheetApp.getActive();
var tz = ss.getSpreadsheetTimeZone();
var cfg = readMonthEndConfig_(ss);
var today = cfg.pretendDate || new Date();
if (!isLastBusinessDayOfMonth(today, tz, cfg.holidays)) return; // most days: exit here
var monthKey = Utilities.formatDate(today, tz, 'yyyy-MM');
var props = PropertiesService.getScriptProperties();
if (!cfg.dryRun && props.getProperty('MONTH_END_LAST_RUN') === monthKey) return; // already ran
runMonthEndExport_(ss, { dryRun: cfg.dryRun, monthKey: monthKey });
if (!cfg.dryRun) props.setProperty('MONTH_END_LAST_RUN', monthKey);
}
isLastBusinessDayOfMonth reads the calendar date with Utilities.formatDate(date, tz, 'yyyy-MM-dd'), then walks back from the month end skipping Saturdays, Sundays, and anything in holidays. If that day is today, you’re on. Why tz matters so much is covered in Timezone-safe dates in Sheets and Apps Script.
3. Holidays and switches on a Config tab
A small Config tab ops can edit:
| A | B |
|---|---|
DRY_RUN |
TRUE |
PRETEND_DATE |
(blank, or 2026-12-31 to rehearse) |
HOLIDAYS |
2026-12-24 |
2026-12-31 |
|
2027-05-31 |
function readMonthEndConfig_(ss) {
var sh = ss.getSheetByName('Config');
var rows = sh.getRange('A1:B30').getDisplayValues();
var cfg = { dryRun: true, pretendDate: null, holidays: [] }; // safe defaults
var inHolidays = false;
rows.forEach(function (r) {
var key = r[0].trim(), val = r[1].trim();
if (key) inHolidays = (key === 'HOLIDAYS');
if (key === 'DRY_RUN') cfg.dryRun = val.toUpperCase() !== 'FALSE';
if (key === 'PRETEND_DATE' && val) cfg.pretendDate = new Date(val + 'T12:00:00Z');
if (inHolidays && /^\d{4}-\d{2}-\d{2}$/.test(val)) cfg.holidays.push(val);
});
return cfg;
}
Format column B as plain text so 2026-12-31 stays a string. getDisplayValues() also helps: you read what ops see, not a Date object. Noon UTC for PRETEND_DATE keeps the date the same in any timezone near the Americas or Europe.
4. Dry run first
function runMonthEndExport_(ss, opts) {
var rows = buildMonthEndRows_(ss, opts.monthKey);
if (opts.dryRun) {
ss.getSheetByName('Log').appendRow([new Date(), 'DRY RUN month end ' + opts.monthKey, rows.length + ' rows']);
return;
}
writeMonthEndExport_(ss, rows, opts.monthKey);
}
Rehearsal: set PRETEND_DATE to the coming month end, keep DRY_RUN = TRUE, run monthEndJob by hand, check the Log. Then try a date that isn’t month end and confirm it exits silently. Clear PRETEND_DATE, flip DRY_RUN to FALSE the week before.
Skipping a month
If finance says “not this month”, don’t delete the trigger. Use the PAUSED Script Property from Pause a nightly job without deleting the trigger as the very first check in monthEndJob, ahead of the date guard:
if (PropertiesService.getScriptProperties().getProperty('PAUSED') === 'true') {
console.log('monthEndJob skipped: PAUSED=true');
return;
}
One flag flip, and nobody has to reinstall the trigger next month.
Soft Co-Pilot note
NitroGAS has isLastBusinessDayOfMonth in the snippet search. Paste your existing monthly job into Co-Pilot and ask for the daily-trigger installer, the date guard, and a Config-tab reader with DRY_RUN and HOLIDAYS, then rehearse with PRETEND_DATE. Free core; Co-Pilot is a pass when you want the draft.
Closing checklist
- Exactly one daily trigger for
monthEndJob(installer checks first) - Script project timezone matches the spreadsheet timezone
- Guard uses
getSpreadsheetTimeZone()and the Config holiday list - Holidays stored as plain-text
YYYY-MM-DD -
DRY_RUNdefaults to on; rehearsed withPRETEND_DATEon and off month end - Month stamp prevents a double run
- Pause switch checked before anything else
Happy Coding!