簡體   English   中英

如何以編程方式為 Google Apps 腳本中的打開事件創建觸發器

[英]How to create a trigger programmatically for an open event in Google Apps Script

我正在嘗試使用 Google Apps 腳本並在嘗試以編程方式創建觸發器(未綁定到特定文件或任何內容)時遇到一個奇怪的錯誤。

我正在嘗試運行某個代碼 - 基本上是在打開 Google Docs 時編輯文檔,但我遇到了這個錯誤:

獲取在對象 ScriptApp.DocumentTriggerBuilder 上創建的方法或屬性時出現意外錯誤。 (第 9 行,文件“代碼”)DetailsDismiss

這是我正在使用的代碼:

 /**
 * Creates a trigger for when a spreadsheet opens.
 */
function createSpreadsheetOpenTrigger() {
  var d = DocumentApp.getActiveDocument();
  ScriptApp.newTrigger('myFunction')
        .forDocument(d)
        .onOpen()
        .create();
}


function myFunction() {
     // change the body or something...
}

如果腳本未綁定到文檔,則 getActiveDocument 將不起作用。 你想要 openById。

您為什么要嘗試在打開時創建觸發器? onOpen本身就是一個觸發器。

嘗試這個

function onOpen(e)
{
    myFunction();
}

你能找到解決方案嗎? 我也在尋找相同的答案,對打開的任何文檔執行任務。

暫無
暫無

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

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