Aaron_H
Aaron_H

Reputation: 1683

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)

Upvotes: 0

Views: 105

Answers (2)

Andrew Roberts
Andrew Roberts

Reputation: 2808

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

Upvotes: 1

AshClarke
AshClarke

Reputation: 3078

I think you could fairly reliably do this by checking the creation date of the document.

Upvotes: 0

Related Questions