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();
}
}
Related
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.
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();
}
}
I am trying to automate certain parts of my workflow for scheduling clients with Google Calendar. I've successfully managed to capture new/edited/deleted events in Google Apps Script using a trigger which detects changes and Calendar.event.list to sync those changes with a spreadsheet.
I create a new row, or edit an existing one, in my spreadsheet of all the clients. What I desire to do is three days before the appointment with the client, automatically generate a custom email with all of their details, to send them as a reminder regarding the appointment.
My plan was every time a new row was created in the Spreadsheet (when a new Calendar event was created), was to make a new email trigger. That trigger would execute code to create an email, with all of the clients info.
function createEmailTrigger(event) {
var today = new Date();
today.setHours(0,0,0,0); // Don't care about time
// Create Email Trigger three days before
const sendDaysBefore = 3;
var daysBefore = new Date(event.start);
daysBefore.setDate(daysBefore.getDate() - sendDaysBefore);
var trigger = ScriptApp.newTrigger('sendEmail')
.timeBased()
.at(daysBefore)
.create();
associateEventWithTrigger(trigger.getUniqueId(), event);
return trigger.getUniqueId();
}
associateEventWithTrigger connects the trigger id with the Calendar event. sendEmail would then create a new email with all of the client's info, which came from the Calendar event. When the trigger is executed, it deletes the trigger since it won't be used again.
All of this was working fine, as I was testing one Calendar event at a time. However, once I decided to sync all of this year's Calendar events, the script very quickly threw this error:
Exception: This script has too many triggers. Triggers must be deleted from the script before more can be added.
Apparently you can only have 20 triggers per user/script. This is very inconvenient, as I was expecting to create hundreds of triggers.
Therefore, I need to rethink how to go about doing this. Any suggestions? I appreciate it.
Proposed workaround
This script is designed to be run on a time-driven trigger that runs daily.
function sendReminderEmails() {
let file = SpreadsheetApp.getActive();
let sheet = file.getSheetByName("Sheet1");
let range = sheet.getDataRange();
let values = range.getValues();
// removing headers
values.shift()
values.forEach(row => {
let name = row[0]
let email = row[1]
let date = row[2]
// Date object representing time now
let now = new Date();
// helper variables
let second = 1000;
let minute = second * 60;
let hour = minute * 60;
let day = hour * 24;
// gets time to appointment in milliseconds
let timeToApp = date.getTime() - now.getTime()
if (timeToApp > 2 * day && timeToApp < 3 * day) {
MailApp.sendEmail(
email,
"Remember Your Appointment",
"Hello " + name + ",\nYou have an appointment coming up soon."
)
}
})
}
This is based on a sample spreadsheet like this:
So you would need to adapt it to your particular format.
Script walkthrough
It is based on the date object.
If your dates are stored as formatted dates in your spreadsheet, when you getValues, Apps Script will automatically pass them as Date object. Alternatively you can cast them as Date objects in Apps Script if needed.
The script starts off by getting all the values in the target sheet.
It initialized a new Date object that represents the time now.
It then goes through each row of the target sheet and gets a value for how long until the appointment.
If the value is between 2 days and 3 days, then an email is sent.
I Think you could make an script to search events every day , events that are 3 days ahead , select then and send email. So it will be just one script that will be triggeres every day, using the date trigger mode.
I have a google script that I would like to run automatically every weekday at 8:11 am. I have set my time zones to make sure that everything is correct, but it never seems to work correctly. I am still weak at scripting. Does anyone see where I might have error in this script?
function myFunction(){
try {
var d = new Date();
if (d.getDay() == 6 || d.getDay() == 0) return;
if (d.getHours() != 08 && d.getMinutes() != 11) return; // This will stop the script from running unless it is 8:11am
} catch (e) {
MailApp.sendEmail("pthompson#ucc.on.ca", "Error report", e.message);
}
}
Thank you,
Paul
To run a function based on time, setup a project trigger (under Resources tab) with the Event "Time-driven". You can even setup a notification.
If you wanted to manage this trigger programmatically you could do something like this:
function createTimeTrigger() {
// Once a day at 8AM, near minute 11 for function 'writeSomething()'
var dailyHourNearMinute = ScriptApp.newTrigger("writeSomething")
.timeBased()
.everyDays(1)
.atHour(8)
.nearMinute(11)
.create();
}
You will notice that this creates a trigger for you that can be viewed under 'Current project's triggers'. It will say between 8AM-9AM. As far as google time events, they work well at getting it within the hour but not on the exact minute. The load in the system can also throw this off. So, the best you can really plan for is, between 8AM - 9AM.
I'm trying to create a trigger that activates on weekdays only and at a specific time, but I don't know what I'm doing wrong. Here is my code.
function createTriggers() {
var days = [ScriptApp.WeekDay.MONDAY, ScriptApp.WeekDay.TUESDAY,
ScriptApp.WeekDay.WEDNESDAY, ScriptApp.WeekDay.THURSDAY,
ScriptApp.WeekDay.FRIDAY];
var d = new Date();
var time = d.toLocaleTimeString();
if (time == '3:05:00 PM EDT') {
for (var i = 0; i < days.length; i++) {
ScriptApp.newTrigger(Lunch1)
.timeBased().onWeekDay(days[i])
.everyMinutes().create();
}
}
}
I'm guessing that you mean you want to make it so a function is triggered (runs) every weekday at a certain time of the day. Here's what I would do.
First, in the Script Editor:
Go to Resources - Current Project's Triggers
Click Add a new trigger
Select the function that you want to be triggered
Change the next box to "Time-driven"
Change the next box to "Hour timer"
Then change the last box to "every hour"
Then at the very beginning of your function, add this code:
var d = new Date();
if (d.getDay() == 6 || d.getDay() == 0) return;
// more info here: http://www.w3schools.com/jsref/jsref_getday.asp
That will stop the rest of the script from running if it's a Saturday or Sunday.
Then, say you want the script to only run at one specific time each weekday, you could set the time-driven trigger to "Every minute" and then add another if statement after the one above:
if (d.getHours() != 15 && d.getMinutes() != 5) return;
That will stop the rest of the script from running if it's not exactly 15:05 (3:05 PM).
This is definitely not the most efficient way to do this, but it works.
Also, this is worth mentioning: I'm not sure if there's anything that would prevent you from triggering the script every minute. Google does impose quotas for what your scripts can do, but I didn't see anything about how many times a script can be triggered in a day. Here's the chart: https://developers.google.com/apps-script/guides/services/quotas
You should do something like this:
function createTriggers(func, hour) {
var weekdays = [ScriptApp.WeekDay.MONDAY, ScriptApp.WeekDay.TUESDAY,
ScriptApp.WeekDay.WEDNESDAY, ScriptApp.WeekDay.THURSDAY,
ScriptApp.WeekDay.FRIDAY];
for (var dayIndex in weekdays) {
var day = weekdays[dayIndex];
ScriptApp.newTrigger(func).timeBased()
.onWeekDay(day).atHour(hour).create();
}
}
This code creates a trigger for weekdays in a specific time calling the func function.