What is causing the error message "Cannot schedule an event in the past" in my Google Apps Script?

I'm looking for a way to set up a one-time trigger for a function within a Google Apps Script. Essentially, I want to schedule an event to occur at a specific date and time in the future, without it being a recurring event.

However, every time I try to do this, I keep encountering an error message that says: "Exception: You cannot schedule an event in the past." Any insights or assistance on how to resolve this issue would be greatly appreciated.

function trigger(){

var rawdate = "2020-07-29T11:30";

var time = rawdate.substr(11).split(':');
var durationMilliseconds = time[0] * 3600000 + time[1] * 60000;
var year = rawdate.substr(0,4);
var month = rawdate.substr(5,2);
var date = rawdate.substr(8,2);

ScriptApp.newTrigger("myFunction")
  .timeBased()
  .atDate(year, month, date)
  .after(durationMilliseconds)
  .inTimezone("America/New_York")
  .create();
}

Answer №1

You are on the right track. To complete the process, all you need to do is find the hour difference between the scheduled trigger time and the current time. Once you have that, simply add the hours difference to the .after() method:

function trigger(){

// Specify the date for scheduling the one-time trigger
var rawdate = "2020-07-29T21:30";
var today_D = new Date(new Date().toLocaleString("en-US", {timeZone: "America/New_York"}));
var scheduled_D = new Date(rawdate);
var time_af = Math.abs(scheduled_D - today_D) / 36e5;

ScriptApp.newTrigger("myFunction")
.timeBased()
.after(time_af * 60 *60 * 1000)
.create();
}

No longer necessary:

  • .inTimezone("America/New_York")
    as the hour difference already considers the correct timezone,
  • .atDate() because the script will automatically adjust the number of hours after it runs based on the scheduled date.

Lastly, you can pass rawdate directly to the new Date() object:

new Date(rawdate)

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

"The printing function of the "Print page" button appears to be malfunctioning

I am having some trouble with my JavaScript code for a "print page" button in an HTML document. The button appears and is clickable, but it doesn't actually print the page as intended. I keep receiving 3 errors related to the `document` object being u ...

Error in Bootstrap table implementation leading to truncation of tbody height due to data-detail-view property

I'm currently working on integrating Bootstrap-Table's detailView feature into my project. However, when I enable data-detail-view="true" for my table, the height of the table element shrinks to 0. Strangely, clicking on any column with ...

Utilizing react.js and passing props as parameters in recursive functions

When using recursion, I encountered an issue where I am unable to pass the props parameter: class App extends React.Component { constructor(props) { super(props); this.state = { visible: this.props.root, a:this.props.a }; } toggle(){ thi ...

Error in parsing JSON: An unexpected token < was encountered at the beginning of the JSON input, causing a SyntaxError at position 0 when parsing with

I need to transfer an array from a PHP file to JavaScript and save it in a JavaScript array. Below is the snippet of JavaScript code: xmlhttp = new XMLHttpRequest(); xmlhttp.onreadystatechange=function(){ if (xmlhttp.readyState==4 ...

Loading JSON and HTML div in the correct order: Reloading vs Refreshing

Currently, I am working on developing a code snippet to retrieve and showcase stock data for my Smart Mirror Project. Although the script successfully displays all of the necessary information, I am encountering difficulties when attempting to update the s ...

Using Node.js to read from a file and write the character 'Ł' to another file

I am facing an issue with an ANSI file that contains the character 'Ł'. My goal is to extract this character and save it to another file while preserving the same encoding to retain the 'Ł' character. const fs = require('fs&apos ...

Issues arise when encountering duplicated items during the JSON import process in Google BigQuery

I've been attempting to manually upload the JSON data into BigQuery, but I keep encountering the following error message. Error occurred while reading data, error message: Parsing error in JSON at row starting from position 0: Repeated field must be i ...

Leveraging AJAX to call upon a designated php file

I have a selection of menu items on my webpage, each with different options: option1, option2, and option3. Additionally, I have corresponding PHP files on my server for each of these menu items: option1.php, option2.php, and option3.php. For simplicity, ...

Choose the currently active md-tab within the md-dialog's md-tab-group

I need to create a dynamic md-dialog with an md-tab-group that has two tabs. The md-dialog should open based on the button clicked, displaying the corresponding tab content. The initial template where the md-dialog is triggered: <button md-button class ...

Guide on setting up the installation process of Gulp jshint with npm?

Having trouble with the installation of JSHint. Can anyone point out what I might be doing incorrectly? This is the command I am using: npm install --save-dev gulp-jshint gulp-jscs jshint-stylish Getting the following error message: "[email protect ...

Simple method to modify the text size of the entire HTML page

Looking for a way to adjust the font size of an entire HTML document? I'd like to have two buttons - one to increase the font size and the other to decrease it. Each button will trigger a JavaScript function; function increaseFontSize(){ //Increase ...

Removing an item with Ajax upon page reload

After creating a small Ajax script to remove items from my database and view, I encountered an issue where the view was only updated after a reload when the item was removed. I want to avoid reloading the entire page by using window.location.reload() in m ...

Determining if there is a common element in two arrays to yield a true outcome

In my Ionic app built with Angular 1.x, I am dealing with two arrays containing numbers: var arr1 = [1,32,423,43,23,64,232,5,67,54]; var arr2 = [11,32,1423,143,123,64,2232,35,467,594]; The common numbers in these arrays are 32 and 64. I need a JavaScript ...

The webpage appears to be experiencing issues with the append() function

I'm having trouble appending a form and it's not working properly. Can anyone help me with a solution? Whenever I click the button below: <div class="col-md-4"> <span href="" class="btn btn-info btn-lg dashboard-icon append">Assig ...

Can someone explain the meaning and syntax of this code in a clear way

I recently encountered this syntax in a tutorial and I'm unsure if it's ES6. It was part of a reduce function. Can someone provide a clear explanation of what is happening inside these parentheses? {...curr, ...acc} Here is the full code snippe ...

Top method for transforming an array into an object

What is the optimal method for transforming the following array using JavaScript: const items = [ { name: "Leon", url: "../poeple" }, { name: "Bmw", url: "../car" } ]; into this object structure: const result = ...

A guide on importing JSON files and rendering them in three.js

Greetings fellow developers! I have written some code to import a JSON file and render it using three.js. The JSON file was exported from the three.js editor. Strangely, there are no errors appearing in the console. window.onload = function() ...

Library for JavaScript that enables incremental testing using regular expressions

Searching for a unique JavaScript library (preferably a node.js package) that has the ability to gradually check if a string meets a regular expression, character by character, and provide uncertain results. For instance, let's consider the following ...

Collaborating an Ember JS application with a React host through microfrontend integration

While attempting to integrate various frontend technologies within a React host using microfrontends, I successfully connected React, Vue, and VanillaJS applications to the host. However, I've hit a roadblock when it comes to incorporating Ember JS. U ...

What is the process of incorporating external links into an angular application?

Attempting to embed an external URL within my Angular app using an iframe has resulted in the following issue: https://i.sstatic.net/liSmX.png The error message reads as follows: https://i.sstatic.net/u9GWw.png Below is the template where I am trying t ...