/** * Copyright Google LLC * * Licensed under the Apache License, Version 2.0 (the 'License'); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * https://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an 'AS IS' BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ // [START apps_script_triggers_onopen] /** * The event handler triggered when opening the spreadsheet. * @param {Event} e The onOpen event. * @see https://developers.google.com/apps-script/guides/triggers#onopene */ function onOpen(e) { // Add a custom menu to the spreadsheet. SpreadsheetApp.getUi() // Or DocumentApp, SlidesApp, or FormApp. .createMenu('Custom Menu') .addItem('First item', 'menuItem1') .addToUi(); } // [END apps_script_triggers_onopen] // [START apps_script_triggers_onedit] /** * The event handler triggered when editing the spreadsheet. * @param {Event} e The onEdit event. * @see https://developers.google.com/apps-script/guides/triggers#onedite */ function onEdit(e) { // Set a comment on the edited cell to indicate when it was changed. const range = e.range; range.setNote('Last modified: ' + new Date()); } // [END apps_script_triggers_onedit] // [START apps_script_triggers_onselectionchange] /** * The event handler triggered when the selection changes in the spreadsheet. * @param {Event} e The onSelectionChange event. * @see https://developers.google.com/apps-script/guides/triggers#onselectionchangee */ function onSelectionChange(e) { // Set background to red if a single empty cell is selected. const range = e.range; if (range.getNumRows() === 1 && range.getNumColumns() === 1 && range.getCell(1, 1).getValue() === '') { range.setBackground('red'); } } // [END apps_script_triggers_onselectionchange] // [START apps_script_triggers_oninstall] /** * The event handler triggered when installing the add-on. * @param {Event} e The onInstall event. * @see https://developers.google.com/apps-script/guides/triggers#oninstalle */ function onInstall(e) { onOpen(e); } // [END apps_script_triggers_oninstall] // [START apps_script_triggers_time] /** * Creates two time-driven triggers. * @see https://developers.google.com/apps-script/guides/triggers/installable#time-driven_triggers */ function createTimeDrivenTriggers() { // Trigger every 6 hours. ScriptApp.newTrigger('myFunction') .timeBased() .everyHours(6) .create(); // Trigger every Monday at 09:00. ScriptApp.newTrigger('myFunction') .timeBased() .onWeekDay(ScriptApp.WeekDay.MONDAY) .atHour(9) .create(); } // [END apps_script_triggers_time] // [START apps_script_triggers_open] /** * Creates a trigger for when a spreadsheet opens. * @see https://developers.google.com/apps-script/guides/triggers/installable */ function createSpreadsheetOpenTrigger() { const ss = SpreadsheetApp.getActive(); ScriptApp.newTrigger('myFunction') .forSpreadsheet(ss) .onOpen() .create(); } // [END apps_script_triggers_open] // [START apps_script_triggers_delete] /** * Deletes a trigger. * @param {string} triggerId The Trigger ID. * @see https://developers.google.com/apps-script/guides/triggers/installable */ function deleteTrigger(triggerId) { // Loop over all triggers. const allTriggers = ScriptApp.getProjectTriggers(); for (let index = 0; index < allTriggers.length; index++) { // If the current trigger is the correct one, delete it. if (allTriggers[index].getUniqueId() === triggerId) { ScriptApp.deleteTrigger(allTriggers[index]); break; } } } // [END apps_script_triggers_delete]