Time-driven triggers in Google Apps Script allow you to schedule scripts to run at specific time intervals or specific times of day. They provide a convenient way to automate tasks and perform actions on a regular basis.
Here are five different ways you can use time-driven triggers:
Data backup: You can use time-driven triggers to automatically create backups of your data at regular intervals. Here’s an example that creates a backup of a Google Sheets file every day:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
function createBackup() { var spreadsheetId = "your-spreadsheet-id"; var backupFolderId = "your-backup-folder-id"; var currentDate = Utilities.formatDate(new Date(), "GMT", "yyyyMMdd_HHmmss"); var backupFileName = "Backup_" + currentDate; DriveApp.getFileById(spreadsheetId).makeCopy(backupFileName, DriveApp.getFolderById(backupFolderId)); } // Set up a daily time-driven trigger to call the createBackup function function setUpBackupTrigger() { ScriptApp.newTrigger("createBackup") .timeBased() .everyDays(1) .create(); } |
Data processing: Time-driven triggers can be used to perform regular data processing tasks. For example, you can set up a trigger to run a script that fetches data from an external API and updates a Google Sheets file every hour:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
function fetchDataAndProcess() { // Fetch data from API var data = // Code to fetch data from API // Process the data and update the Google Sheets file var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet(); // Code to process data and update sheet } // Set up an hourly time-driven trigger to call the fetchDataAndProcess function function setUpDataProcessingTrigger() { ScriptApp.newTrigger("fetchDataAndProcess") .timeBased() .everyHours(1) .create(); } |
Report generation: You can use time-driven triggers to automatically generate and send reports at specific times. Here’s an example that generates a weekly report and sends it as an email:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
function generateAndSendReport() { // Generate report var report = // Code to generate report // Send report as email var recipientEmail = "recipient@example.com"; var subject = "Weekly Report"; var body = "Please find the attached report."; var attachment = Utilities.newBlob(report).setName("report.pdf"); MailApp.sendEmail(recipientEmail, subject, body, { attachments: [attachment] }); } // Set up a weekly time-driven trigger to call the generateAndSendReport function function setUpReportTrigger() { ScriptApp.newTrigger("generateAndSendReport") .timeBased() .onWeekDay(ScriptApp.WeekDay.MONDAY) .atHour(8) .create(); } |
Data cleanup: Time-driven triggers can be used for regular data cleanup tasks. For instance, you can schedule a trigger to delete outdated data from a Google Sheets file every month:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
function deleteOutdatedData() { var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet(); // Code to identify and delete outdated data } // Set up a monthly time-driven trigger to call the deleteOutdatedData function function setUpDataCleanupTrigger() { ScriptApp.newTrigger("deleteOutdatedData") .timeBased() .onMonthDay(1) .atHour(0) .create(); } |
Reminder notifications: You can use time-driven triggers to send reminder notifications to yourself or others at specific times. Here’s an example that sends a reminder email every weekday at a specific time:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
function sendReminderEmail() { var recipientEmail = "recipient@example.com"; var subject = "Reminder: Complete the Task"; var body = "This is a reminder to complete the task."; MailApp.sendEmail(recipientEmail, subject, body); } // Set up a weekday time-driven trigger to call the sendReminderEmail function function setUpReminderTrigger() { ScriptApp.newTrigger("sendReminderEmail") .timeBased() .onWeekDay(ScriptApp.WeekDay.MONDAY) .atHour(9) .create(); } |
These examples demonstrate various use cases for time-driven triggers in Google Apps Script. You can customize the triggers based on your specific needs and scheduling requirements.