簡體   English   中英

如何確定(New Apps)是否正在執行(Google Apps)腳本?

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

我已經為Google表格編寫了Google Apps腳本,但是它需要運行不同的代碼,具體取決於它是在“新”表格上運行還是在“舊表格”格式中運行。

它是在“舊表”上編寫的,但是當Google將所有表更新為“新表”格式時,我想保持兼容性。 (這特別適用於向用戶顯示警報框: https : //developers.google.com/apps-script/guides/dialogs#alert_dialogs

感覺有點像個大障礙,最終可能會停止工作,但是您可以嘗試調用新工作表不支持的功能之一。 例如,通過newTrigger以編程方式創建觸發器。

此代碼包含在新的工作表中:

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()');
}

刷新工作表后,將其輸出到日志中:

[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

我認為您可以通過檢查文檔的創建日期來相當可靠地做到這一點。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM