Google Apps Script - Custom Triggers - Just Month + Day - No Year - google-apps-script

In short, I have code that I would like to trigger on the first day of a specific month. So, for example, I want to run X-script on Jan 1, but not Feb 1st, March 1st etc. Based on my testing, it seems the atDate function requires the year variable and I'd like the trigger to fire every year on Jan 1, rather than just x year.
This is the version of the timed trigger function I've been working with.
function createTrigger() {
ScriptApp.newTrigger("moveNumbersDecember")
.timeBased()
.atDate(xxxx, 1, 1)
.create();
}
Is there any other function I could use, or workaround for atDate()? My other attempts have been with making the A1 cell on a sheet a clock using today(), then trying to make an if function where if A1 = January 1, then run X script. But I'm very new to coding and haven't been able to make it work.
Any ideas/help extremely appreciated.

GAS do not have yearly trigger, but try this workaround. Create a trigger that runs every month. This runs every month BUT only call the function you want to do if January 1 comes via checking the month of current date.
function createMonthlyTrigger() {
// every month, the trigger created will run checkIfJan1()
ScriptApp.newTrigger("checkIfJan1")
.timeBased()
.onMonthDay(1)
.atHour(0)
.create();
}
function checkIfJan1() {
var date = new Date();
// if jan 1, then run moveNumbersDecember()
if (date.getMonth() === 0) {
moveNumbersDecember();
}
}

Related

Trying to sort out Google Yearly Time-driven Trigger but failed

As the Google Apps Script doesn't support yearly triggers, I am trying to run a monthly trigger that runs on a certain date of every month, and if the month is matching my criteria, run the actual function. Here is the code written:
function createYearlyTrigger() {
const sheet=SpreadsheetApp.getActive().getSheetByName("mobile") //<- insert Sheet name
const subjectLine = "Message sent to be Yearly | {{Property}}"; // <-- Insert email draft subject line
sheet.getRange("AH2:AH").setValue("")
ScriptApp.newTrigger("shouldTriggerRun")
.timeBased().onMonthDay(17).atHour(19).create();
}
function shouldTriggerRun() {
var date = new Date();
if (date.getMonth() === 3) {
incrementCell();
}
sendEmails(subjectLine, sheet);
}
Now my target is to run the trigger on 17th day of April. I am also using the following trigger:
But it is not working properly.
Please let me know where I am doing wrong.

how to make correct timing trigger in google apps script, please check my code line

I am very new to GAS. The already created script (paragraph 3) I need it to be run everyday, every 3 hours, at a specific time at 0:30 -> 3:30 -> 6:30 -> 9:30 -> 12:30-> 15:30-> 18:30-> 21:30
This is upper part of the code. Cannot "combine" below 3 codes into one
function createOpenTrigger() {
ScriptApp.newTrigger("#what should I input here or leave blank????")
.forSpreadsheet(SpreadsheetApp.getActive())
.onOpen()
.create();
}
function createTimeTriggerAtSpecificHourMinute() {
ScriptApp.newTrigger("what should I input here or leave blank????")
.timeBased(00:00)
.everyHour(3)
.nearMinute(30)
.everyDays(1)
.inTimezone("Asia/Tokyo")
.create();
}
function takeSnapShot() {
const ss = SpreadsheetApp.getActiveSpreadsheet();
const mainSheet = ss.getSheetByName('SOURCE');
const snap = mainSheet.getRange("A1:Q26").getDisplayValues();
....
If I understand your question correctly, you want to schedule your takeSnapShot() function to run every three hours beginning at 00:30.
Using the sample code you have supplied you can replace your createOpenTrigger() and createTimeTriggerAtSpecificHourMinute() functions with something like this.
function setFirstCall(){
ScriptApp.newTrigger('setRecurrence')
.timeBased()
.atHour(0)
.nearMinute(30)
.inTimezone('Asia/Tokyo')
.create();
}
This will trigger a one-off call to happen near 00:30 the next day. . After running, the trigger will delete itself but will schedule a function setRecurrence() to run. So next you need to create the recurring function.
function setRecurrence(){
ScriptApp.newTrigger('takeSnapShot')
.timeBased()
.everyHours(3)
.create();
}
GAS doesn't allow scheduling for an 'exact' minute (there's a +/- 15min accuracy) however the recurrence should be for the same minute, i.e. if the first trigger runs in the 27th minute then every iteration after should be in the 27th minute.
Hopefully that helps.
References:
TriggerBuilder.timeBased()
ClockTriggerBuilder

How to create a yearly time-driven trigger?

I'm attempting to create a time-based trigger to execute my incrementCell function once a year on a specified date at 1 AM in perpetuity. When attempting to run below
ScriptApp.newTrigger("incrementCell").timeBased().atDate(2018, 1, 4).atHour(1).everyWeeks(52).create();
I received an "Already chosen a specific date time with at() or atDate()." error.
Interestingly, the line immediately below does not error out:
ScriptApp.newTrigger("incrementCell").timeBased().atDate(2018, 1, 4).create();
Google Apps Script doesn't support yearly triggers but you can use a workaround. Create a monthly trigger that runs on the 1st of every month, and if the month is January, run the actual function.
function createYearlyTrigger() {
ScriptApp.newTrigger("shouldTriggerRun")
.timeBased().onMonthDay(1).atHour(1).create();
}
function shouldTriggerRun() {
var date = new Date();
if (date.getMonth() === 0) {
incrementCell();
}
}

Google Script Trigger Every 30 Days

I'm interested in having my functions be triggered every 30 days. The closest I can find within Google is the once a month option, but that'll vary away from 30 days due to February and the months with 31 days.
Does anyone have any advice on how I could get this to work?
Thanks!
Trigger this every day. It will run the first time then eventually run 30 days later etc.
// trigger this function every day
function myFunction() {
var lastRunDate = new Date(PropertiesService.getScriptProperties().getProperty("lastRunDate"));
var thirtyDaysAgo = new Date(new Date().setDate(new Date().getDate()-30));
if (lastRunDate > thirtyDaysAgo) {
return; //exit because it's not yet been 30 days since last run
} else {
PropertiesService.getScriptProperties().setProperty("lastRunDate", new Date());
}
// do you work here
}

Open Google Form every Thursday

I am trying to figure out how to check if it the current time is Thursday 9:00 AM. I don't care about date in any other way except that it is a Thursday and that it is past 9:00 AM.
I've never written a single google script before and the API here doesn't seem to offer something like what I am looking for: https://developers.google.com/apps-script/reference/contacts/date-field
Here is how far I've gotten:
FORM_OPEN_DATE = "2016-03-09 12:20";
FORM_CLOSE_DATE = "2016-03-09 12:30";
function Initialize() {
if ((FORM_OPEN_DATE !== "") &&
((new Date()).getTime() < parseDate_(FORM_OPEN_DATE).getTime())) {
closeForm();
ScriptApp.newTrigger("openForm")
.timeBased()
.at(parseDate_(FORM_OPEN_DATE))
.create();
}
if (FORM_CLOSE_DATE !== "") {
ScriptApp.newTrigger("closeForm")
.timeBased()
.at(parseDate_(FORM_CLOSE_DATE))
.create();
}
}
My script above works as intended, it will open and close the form between the time frames, however I am trying to figure out how to check based on the day of the week as oppose to exact date. Reason being is that I wish for the form to open itself every thursday at 9:00 AM and close at 8:30 PM.
Is this achievable?
The JavaScript method getDay() returns the day of the week as a number. Sunday is 0, Monday is 1, and so on. So, Thursday would be 4.
if (new Date().getDay() === 4) {//true block};