脚本停止,可能是由于onEdit()SpreadsheetApp调用然后是DocumentApp调用

时间:2017-10-03 15:29:42

标签: google-apps-script google-docs

此脚本仅在最后一个函数的入口处停止:addToDocument()。

在此之前一切正常。我认为由于onEdit()和DocumentApp调用问题?

请注意,我的addToDocument()单独运行。

function onEdit() {
// simple timestamp -- when a single "t" is entered in a cell, replace it with a timestamp
// see https://productforums.google.com/d/topic/docs/rC6MpQDC7n4/discussion 
var ss = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var cell = SpreadsheetApp.getActiveRange();
if (cell.getValue() == "t") {
cell.setValue(new Date()); 
}

formatDate() // Some Date formatting using        : 'SpreadsheetApp' call
mefCond()    // Some conditonnal formatting using : 'SpreadsheetApp' call
doCounts()   // Some numéricals opérations, using : 'SpreadsheetApp' call

//At this point the scripts enter in the following function,
//and stops on the first line. Nothing being executed.

addToDocument() // Time stamp on a document using : 'DocumentApp' call
}  

有什么想法吗?

感谢阅读, 埃里克: - )

1 个答案:

答案 0 :(得分:0)

当手动运行OnEdit时,它会使用不同的权限集运行,但触发器本身具有所提到的特定限制here。从该页面看.. ..

  

他们可以修改绑定的文件,但不能访问其他文件   文件,因为这需要授权。

请参阅this了解授权模式以及您可以在每种模式下执行的操作。可能是下面第2行正在影响你...

enter image description here

我认为解决方案是将简单触发器转换为可安装的触发器。 Here详细介绍了如何为电子表格安装触发器。对于onEdit()函数,任何内容都不会发生变化,您只需运行一次安装代码段即可创建可安装的触发器。

function createSpreadsheetEditTrigger() {
  var ss = SpreadsheetApp.getActive();
  ScriptApp.newTrigger('onEdit')
      .forSpreadsheet(ss)
      .onEdit()
      .create();
}

here是权限和其他详细信息的详细信息。在这里,它明确提到您可以访问其他服务..

  

例如,Google表格的可安装打开触发器会激活   只要具有编辑权限的任何用户打开电子表格,   就像简单的onOpen()触发器一样。但是,可安装   版本可以调用需要授权的服务。可安装   版本在创建的用户的授权下运行   触发,即使具有编辑权限的其他用户打开电子表格