简体   繁体   中英

How do I determine whether a (Google Apps) script is being executed on New Sheets?

I've written a Google Apps Script for Google Sheets, but it needs to run different code depending on whether it's being run on the "New" Sheets or if it's being run on one in the "Old Sheets" formats.

It's been authored on an "Old Sheet", but I want to maintain compatibility when Google updates all sheets to the "New Sheets" format. (This specifically pertains to showing an alert box to the user: https://developers.google.com/apps-script/guides/dialogs#alert_dialogs )

This feels like a bit of a bodge and could stop working eventually, but you could try calling one of the functions that aren't supported by the new sheets. For example programmatically creating a trigger via newTrigger.

This code contained in a new sheet:

function onOpen() {

  try {

    var everySixHours = ScriptApp.newTrigger("runCommand")
        .timeBased()
        .everyHours(6)
        .create();

  } catch(e) {

      Logger.log(e);
      Logger.log('You are using a new Google sheet');
  }

  Logger.log(everySixHours);

}

function runCommand() {
  Logger.log('runCommand()');
}

outputs this in the log when the sheet is refreshed:

[14-08-10 22:15:07:767 BST] Exception: You do not have permission to call newTrigger
[14-08-10 22:15:07:767 BST] You are using a new Google sheet
[14-08-10 22:15:07:767 BST] undefined

我认为您可以通过检查文档的创建日期来相当可靠地做到这一点。

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