Zac
Zac

Reputation: 3

Google Apps Script trigger to run every 2 months is not working

I have created a script for my enterprise Google Apps account (script.google.com) to require all users to change their passwords. I've gone into the Google Script settings under Resources > Current Project's Triggers and set the script to run as a time driven event using the Month timer set to "2". The script instead runs every 30 days, not every 60 days. Does anyone have any experience with the project triggers within script.google.com? I tried to call Google to find out why their built in triggers are not functioning as intended and they just tell me that they don't support script.google.com. I need this script to execute every 60 days, and it would seem that setting the project trigger to run every 2 months should work. It doesn't.

Any help is appreciated.

Thanks!

Upvotes: 0

Views: 1903

Answers (3)

HTTP ERROR 500
HTTP ERROR 500

Reputation: 67

Use ScriptApp.newTrigger("myFunction").timeBased().everyDays(60).create();

This will run your function "myFunction" every 60 days, instead of every 30 days.

Upvotes: 1

liamtiernan
liamtiernan

Reputation: 91

To get a bi-monthly trigger you can set the trigger to monthly and then wrap your code in a conditional like such.

The trigger will fire every month on the specified day but the code inside the conditional will only run once every 2 months.

const date = new Date;   
if (date.getMonth() % 2 == 0) {
  // code to execute   
};

Upvotes: 1

HDCerberus
HDCerberus

Reputation: 2143

I believe I see what the issue is here. The 'Month' trigger on Google Apps script actually refers to run monthly on [day of the month] not run every X months.

You can see the the 'Monthly' trigger gives you up to 31 days to choose a trigger from, indicating that if you choose '15' it would trigger on the 15th of every month. I suppose this could be misinterpreted as 'Run every 15 months'.

A possible solution here is to have it run every month on the 31st of each month, as it should skip months where the number if days is less then 31. The roman calendar is split in such a way that generally this means it would run every two months, with some exceptions (July and August are back to back and both have 31 days, as do December and January).

Upvotes: 0

Related Questions