1

I'm trying to trigger a spreadsheet in google sheets to reset itself every 14 days but I need it to start doing that on a certain date (it's for company time sheets not that that's relevant). Here's what I have that works, triggering every 14 days...

function test() {

ScriptApp.newTrigger("14day_trigger")
   .timeBased()
   .everyDays(14)

Browser.msgBox('Testing!');

I want to start the 14 day cycle 2015, 06, 12. I tried with .atDate ...

function test() {

ScriptApp.newTrigger("14day_trigger")
   .timeBased()
   .atDate(2015, 06, 12) 
   .everyDays(14)

Browser.msgBox('Testing!');

But this gives the error -

Already chosen a specific date time with at() or atDate().

Can anyone help me?

  • You can't create a single time-based trigger with conflicting time criteria. Instead, think of the problem in two parts. First, set up a trigger to fire on the initial date. Then, a trigger to handle recurrence - which could be done within the trigger function itself, to run 2 weeks in the future. (i.e. there would be no `.everydays()` condition) – Mogsdad May 31 '15 at 19:41

1 Answers1

1

Create three functions:

function triggerAtDate() {
  ScriptApp.newTrigger("atDateTrigger")
    .timeBased()
    .atDate(2015, 06, 12)
    .create();
};

function 14day_trigger() {
  ScriptApp.newTrigger('resetItself')
    .timeBased()
    .everyDays(14)
    .create();
};

function resetItself() {
  SpreadsheetApp.getActiveSpreadsheet().getSheets()[0].clearContents();
};

Then run the first function inside of the script editor. Once the one time trigger has run, it is no longer needed, you could delete it.

You could wait for the day that you want the trigger to run from, then manually create the 14 day trigger on that day.

Alan Wells
  • 30,746
  • 15
  • 104
  • 152