简体   繁体   中英

programmatically disable / enable Google Apps Script script trigger

I have created a script to run as a time based trigger.

  • I want to disable that trigger during weekends off and holidays.
  • I ideally want this to be part of a function I already have that deletes previous form submissions meaning that the user only has one button to press (deleting previous submissions AND disabling the time trigger) at the start of each holiday.
  • It would then be brilliant if the trigger was re-enabled the next time a pupil submitted the form (indicating holidays were over).

Not sure how to use script to disable / enable triggers in this way - grateful for any advice!

   //Menu allowing user to choose to delete previous form submissions
    function onOpen() {
      var ui = SpreadsheetApp.getUi();
      // Or DocumentApp or FormApp.
      ui.createMenu('Clear Out Responses')
      .addItem('Clear Form & Responses', 'showAlert')
          .addToUi();
    }


//Alert box deleting previous form submissions on yes, doing nothing if not
function showAlert() {
  var ui = SpreadsheetApp.getUi(); // Same variations.

  var result = ui.alert(
     'This will clear all previously registered data',
     'Are you sure you want to continue?',
      ui.ButtonSet.YES_NO);

  // Process the user's response.
  if (result == ui.Button.YES) {
    // User clicked "Yes".
 var ui = SpreadsheetApp.getUi();
  var source = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Form Responses 1");
  var lastRow = source.getLastRow()
  source.deleteRows(2, lastRow)}
  var form, urlForm = SpreadsheetApp.getActiveSpreadsheet().getFormUrl();
  if (urlForm) {}
    form = FormApp.openByUrl(urlForm);
    if (form) form.deleteAllResponses();
  }

//Trigger running  record-keeping function each night 
function dailyTrigger() {
  ScriptApp.newTrigger('dailyRecord')
  .timeBased()
  .atHour(22)
  .everyDays(1)
  .create();
}


//Record-keeping function, copy/value-pasting record into next clear column
function dailyRecord() {
    var ss = SpreadsheetApp.getActive()
        .getSheetByName('History'),
        lastColumn = ss.getLastColumn(),
        colE = ss.getRange("C:C")
            .getValues();
    ss.getRange(1, lastColumn + 1, colE.length, 1)
        .setValues(colE);
}

Have you looked at getUserTriggers() and deleteTrigger ?

  • With getUserTriggers() you can identify whether the trigger to call dailyRecord is already in existence. It returns a list of triggers, and you can check the function being called by each trigger by using getHandlerFunction() .
  • With deleteTrigger() you can delete a given trigger. eg if your trigger to call dailyRecord is installed (and returned in the list from getUserTriggers() , you can then delete it using this function.
  • To enable the trigger again, having checked that it doesn't already exist, you can use the trigger builder as per the code you already have, to recreate the trigger.

Armed with these methods, you should be able to achieve the effect you are after.

An existing trigger can not be retrieved and get re-defined. For example, a time based trigger that has already run, can not be retrieved and given a new time to run using code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM