1
votes

I am trying to use the Google Apps Script ScriptApp getUserTriggers(spreadsheet) method to create a report that displays all of my user-installed triggers across all of my projects.

This code loops through all of the files in my Google Drive and makes an array of file IDs for all of the files that are Google Sheets files, and then attempts to get the triggers for each file:

var spreadsheetIds = [];

  // get all spreadsheets in my google drive
  var files = DriveApp.getFiles();

  while (files.hasNext()) {

    var file = files.next();
    var type = file.getMimeType();

    if (type == 'application/vnd.google-apps.spreadsheet') spreadsheetIds.push(file.getId());

  }

  var data = [];

  try {

    for (var i = 0; i < spreadsheetIds.length; i++) {

      var ssOpen = SpreadsheetApp.openById(spreadsheetIds[i]);

      Utilities.sleep(100);

      var triggers = ScriptApp.getUserTriggers(ssOpen);

      var spreadsheetName = ssOpen.getName();

      Logger.log(spreadsheetName + ' triggers: ' + triggers.length);

      for (var j = 0; j < triggers.length; j++) {

        Logger.log(spreadsheetName + ' trigger ID: ' + triggers[j].getUniqueId());

        data.push([spreadsheetName, triggers[j].getEventType(), triggers[j].getHandlerFunction(), triggers[j].getTriggerSource(), triggers[j].getTriggerSourceId(), triggers[j].getUniqueId()]);

      }

    }

  } catch(e) {

    Logger.log(e);

  }

Many of my projects have user-installed triggers, but this just returns 0 for all of them (empty arrays are returned from ScriptApp.getUserTriggers()).

I am 100% sure that I am logged in to the same account when running this code as when I've set the triggers.

But just to be sure, when I add a test trigger to this project and run this code it correctly returns the number of triggers (which is 1):

function getCurrentProjectTriggersCount() {

  Logger.log('Current project has ' + ScriptApp.getProjectTriggers().length + ' triggers.');

}
1

1 Answers

0
votes

This has been previously reported as Issue 4562 - getProjectTriggers() always returns the triggers for the script it's contained in.

The usual advice is to visit and star the issue to increase its priority, and receive updates.