Tag Archives: automatically
Google Apps Script : Process only on business day
Have you ever planed to execute Google Apps Script only on business day? Send mail only on business day, or process on the weekend except holiday, etc. I thought script to make it.
First, write core script and set trigger to work on weekdays.
Process except Holiday
It means to process only on business day. Get holiday information from Google Calendar.
Add the following code to the first of the process.
1 2 3 4 5 6 7 8 9 |
var currentDate = new Date(); var weekday = currentDate.getDay(); if (weekday == 0 || weekday == 6) { return; } var calendar = CalendarApp.getCalendarById('ja.japanese#holiday@group.v.calendar.google.com'); if (calendar.getEventsForDay(currentDate, {max: 1}).length > 0) { return; } |
It stop the process on Sunday and Saturday, and also on holiday in Japanese holiday calendar, prepared by Google. You can configure the day on which the script works, by setting trigger, too.
It is good to create function that returns bool value.
If you have other special holiday, create special holiday calendar and add some code to check it. Calendar ID which should be passed to getCalendarById
is saw in the page of its calendar.
How to send email automatically and periodically
How do you do, when you should send email periodically? With Google Apps Script, we can send email automatically and periodically.
First, wrote the code to send email like below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
function scheduleMail() { var dateString = Utilities.formatDate(new Date(),"GMT+09:00","yyyy/MM/dd"); var mailto = "to@mail.com"; var subject = dateString + " mail title"; var message = "Hello everyone!nnI am Kenji.n"; message += "I have something to tell you on this weekend.nn"; message += "- Donation -n"; message += "Feel free to donate to me.n"; message += "reference: http://test.com"; message += "nn"; message += "- Contribution -n"; message += "Feel free to contribute money to me.nn"; MailApp.sendEmail(mailto, subject, message); return 0; } |
You can add CC and BCC on the email.
And now, select “Resources” from menu, and click “Current project’s triggers”, and follow the instructions and create a trigger to execute sending method periodically. You can send email everyday, or on the specific weekdays, or beginning or end of month, etc. Notification email is available, which tells you the information when program fails.
In the above case, I use MailApp for sending email、 You can send email according to you or other’s calendar, using CalendarApp. Detail of MailApp or CalendarApp, Utilities is on the online document published by Google.