Apps Script example · 8 min read

Create a Daily Time-Driven Trigger in Apps Script: Copy-Paste Apps Script Pattern

Working create a daily time-driven trigger in apps script example in Apps Script—copy-paste code, common mistakes, and when to get it built professionally.

ScriptAppTriggersAutomation

Any workflow that should run without a human remembering to click a menu item, like closing out a daily queue or refreshing a report, needs a time-driven trigger instead of relying on someone to open the sheet and run a function manually.

This tutorial creates a trigger that calls a function named dailyJob once every day at approximately six in the morning, using ScriptApp's fluent trigger builder rather than the legacy trigger UI.

Because Apps Script allows the same handler function to be attached to multiple triggers if the setup code runs more than once, the example first deletes any existing dailyJob triggers before creating a fresh one, keeping the project's trigger list clean.

The dailyJob function itself processes a small Queue sheet, marking pending rows as done and logging a summary row, which gives the trigger something concrete to demonstrate beyond an empty scheduled function.

Need this built? Hire a Google Apps Script developer →

Sheet / project setup

PieceValuePurpose
HandlerdailyJobFunction run by trigger
Trigger buildereveryDays(1).atHour(6)ScriptApp.newTrigger
SheetDaily RunsLog of each execution

What it does

createDailyTrigger removes any prior triggers pointed at dailyJob and then registers a new time-based trigger configured to fire every day during the six o'clock hour in the script's timezone.

Prerequisites

ScriptApp.newTrigger takes the handler function's name as a string, and chaining timeBased(), everyDays(1), and atHour(6) builds a recurring schedule without needing to specify an exact minute.

Walkthrough

Run createDailyTrigger once from the Apps Script editor to register the schedule; Google will prompt for authorization the first time, and the trigger then persists independently of whether the editor stays open.

Edge cases

deleteExistingDailyTriggers loops over every project trigger and compares getHandlerFunction() against the string dailyJob, which is the safe way to avoid duplicate triggers accumulating each time the setup function runs.

Testing

If dailyJob throws an uncaught error during a scheduled run, Apps Script emails the trigger owner a failure notification by default, which is the main built-in signal that something needs attention since there is no live console to watch.

Hardening

Call dailyJob directly from the editor first to confirm it behaves correctly against the Queue sheet, and only create the trigger with createDailyTrigger once that manual run produces the expected Daily Log entry.

Variations

Google documents time-based triggers as firing within a window around the requested hour rather than at an exact minute, so any downstream process that depends on dailyJob's output should tolerate a delay of up to roughly fifteen minutes.

Full code: createDailyTrigger() and dailyJob()

Run createDailyTrigger once from the editor to register the schedule; dailyJob then runs automatically every morning.

function createDailyTrigger() {
  deleteExistingDailyTriggers();
  ScriptApp.newTrigger('dailyJob')
    .timeBased()
    .everyDays(1)
    .atHour(6)
    .create();
}

function deleteExistingDailyTriggers() {
  var triggers = ScriptApp.getProjectTriggers();
  triggers.forEach(function (trigger) {
    if (trigger.getHandlerFunction() === 'dailyJob') {
      ScriptApp.deleteTrigger(trigger);
    }
  });
}

function dailyJob() {
  var sheet = SpreadsheetApp.getActive().getSheetByName('Daily Log') || SpreadsheetApp.getActive().insertSheet('Daily Log');
  var pending = SpreadsheetApp.getActive().getSheetByName('Queue');
  var values = pending ? pending.getDataRange().getValues() : [];
  var processed = 0;

  values.forEach(function (row, index) {
    if (index === 0) return;
    if (row[1] === 'pending') {
      pending.getRange(index + 1, 2).setValue('done');
      processed++;
    }
  });

  sheet.appendRow([new Date(), 'dailyJob ran', processed + ' rows processed']);
}
  1. Line 2: Removes any prior dailyJob triggers before creating a new one.
  2. Line 3: Registers the handler function dailyJob by name.
  3. Line 6: Configures the trigger to fire once a day near six in the morning.
  4. Line 13: Matches triggers by handler function name to avoid duplicates.
  5. Line 22: Reads the Queue sheet once with getDataRange().getValues().
  6. Line 30: Logs a summary row to the Daily Log sheet after processing.

Deploy this example

  1. 01

    Open Apps Script

    In the bound spreadsheet: Extensions → Apps Script. For standalone projects, create one at script.google.com and link your Sheet by ID.

  2. 02

    Paste and save

    Add a .gs file, paste the code below, rename constants at the top (sheet names, column letters, API property keys), then save.

  3. 03

    Authorize once

    Run the main function from the editor. Accept OAuth scopes when prompted — triggers cannot run until authorization succeeds once.

  4. 04

    Add the trigger

    Triggers → Add trigger → choose the handler function and event (time-driven, on edit, or on form submit). Delete test triggers before production.

Before you run: create a daily trigger

  • 1dailyJob function tested manually before any trigger is created
  • 2createDailyTrigger run once from the editor to register the schedule
  • 3Existing dailyJob triggers removed before a new one is created
  • 4Script timezone confirmed to match the intended six o'clock run time
  • 5Queue and Daily Log sheets exist with the expected columns
  • 6Failure notification email address confirmed as the right owner
  • 7Downstream processes tolerant of the trigger's firing time window

Frequently asked questions

Running the setup function more than once without cleanup would attach multiple triggers to the same handler, causing dailyJob to run several times a day instead of once.

No, Apps Script time-based triggers fire within an approximately fifteen-minute window around the requested hour rather than at a precise minute, which is a platform limitation rather than a configuration option.

Apps Script automatically emails the project owner a failure notification with the error details, since there is no live session watching a scheduled execution the way there is for a manual run.

Replace everyDays(1) with a method like everyHours(n) if the job needs to run several times daily, keeping in mind the shortest supported interval is currently every hour, not every minute.

Yes, once created, a trigger is stored with the project and runs on Google's servers regardless of whether the editor or the spreadsheet is open.

Open the Executions view in the Apps Script editor, which lists every past run of dailyJob along with its status, duration, and any error details.

Related examples

Want this wired into your real workflow?

I adapt these patterns to your Sheet structure, APIs, and triggers — deployed in your Google account. Fixed-scope quotes from $500 · free 30-min consult · quote within 24 hours.