Search code examples
dategoogle-sheetsgoogle-apps-scripttriggers

Time trigger .atDate using a cell as a reference


I am trying to make a triger to fire a function on a specific date in the future. I am using the .atDate method, but I want to use a cell from the sheet as a reference.

My code is:

`function createTimeTrigger() {
  ScriptApp.newTrigger("AdditionalSlots")
  .timeBased()
  .atDate(2025, 02, 12)
  .create();
}`

... and it works fine. My problem is that I wand to set the date (2025, 02, 12), from a cell of the sheet and after that to run the code. Is there a solution to grab the date from the cell and used it in the code? Thank you!


Solution

  • You may try:

    function createTimeTrigger() {
      ScriptApp.newTrigger("AdditionalSlots")
        .timeBased()
        .at(SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getRange("A1").getValue())
        .create();
    }
    

    This gets the date in A1 and creates a trigger for that period.

    REFERENCE