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

Script stopping, may be due to onEdit() SpreadsheetApp call and then DocumentApp call

此脚本恰好在最后一个函数的入口处停止: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
}  

有什么想法吗?

感谢阅读, 埃里克:-)

OnEdit 手动 运行 时,它 运行 具有一组不同的权限,但触发器本身有特定的限制,如前所述 here。从那个页面看到..

They can modify the file they are bound to, but cannot access other files because that would require authorization.

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

我认为适合您的解决方案是将简单的触发器转换为可安装的触发器。 Here 详细介绍了如何为电子表格安装触发器。 onEdit() 功能不会有任何变化,您只需 运行 安装代码片段一次即可创建可安装的触发器。

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

here是关于权限和其他细节的细节。在这里它清楚地提到您可以访问其他服务..

For example, the installable open trigger for Google Sheets activates whenever the spreadsheet is opened by any user who has edit access, just like the simple onOpen() trigger. However, the installable version can call services that require authorization. The installable version runs with the authorization of the user who created the trigger, even if another user with edit access opens the spreadsheet