Google脚本,OneDit功能突然无法正常工作,如何修复



我在电子表格中具有一个OnEdit函数,我需要复制到另一个电子表格,其中另一个电子表格是一个相同的副本,具有相同的表格名称。我上次检查时正常工作,但这是几天前,现在已经停止了。

代码:

function onEdit(e){
//  Logger.log("working so far 1");
//  mainfile();
  Logger.log("working so far 4");
//  var ss=SpreadsheetApp.openById(mainssid);
  var ss=SpreadsheetApp.openById("Sheet ID");
  var sh=ss.getSheetByName(e.range.getSheet().getName());
  var rg=sh.getRange(e.range.rowStart,e.range.columnStart);
  rg.setValue(e.value);
} 

function mainfile(){
  Logger.log("working so far 2");
  var SSID = SpreadsheetApp.getActiveSpreadsheet().getId();
  var folder = DriveApp.getFileById(SSID).getParents().next().getName();
  var files = DriveApp.getFoldersByName(folder).next().getFiles();
  var array = [];
  while (files.hasNext()) {
    var file = files.next();
    array.unshift(file.getName());
  }
  array.sort();
  var mainss = array[0];
  var mainssid = DriveApp.getFilesByName(mainss).next().getId();
  Logger.log(mainssid);
  Logger.log("working so far 3");
}

我将要获得特定文件的ID,而评论的部分正是我试图将其实现到ONEDIT函数中的位置。因此,预期的结果是我要在一个电子表格上进行编辑,并且要在另一个电子表格上进行相同的更改,并且日志会说到目前为止,数字为2,3,4,但什么都没出现。

当我运行MainFile功能时,它可以完美地工作。我还知道这可能是这里的重新发布,但由于他们实际上并没有得到答案,因此它本身就是我的,所以我可能没有资格。

这不是其他帖子的复制品,因为我不是要发送电子邮件。我已经查看了简单的触发指南,我无法弄清楚此代码有什么问题,因为它不要求正常运行该功能,所以我认为我不需要授权来运行它,我知道它可以修改其他文件,因为它曾经工作,并且我今天工作起来,它的运行时间不超过30秒,而且我没有超过配额。其他似乎都不适用。请您向我解释我做错了什么,因为我不明白。

我还用表ID替换了表ID。ONEDIT()代码的所有信用cooper。

提前感谢,对不起。

简单触发器无法执行需要授权的事情。

Restrictions
Because simple triggers fire automatically, without asking the user for authorization, they are subject to several restrictions:
The script must be bound to a Google Sheets, Slides, Docs, or Forms file, or else be an add-on that extends one of those applications.
They do not run if a file is opened in read-only (view or comment) mode.
Script executions and API requests do not cause triggers to run. For example, calling Range.setValue() to edit a cell does not cause the spreadsheet's onEdit trigger to run.
They cannot access services that require authorization. For example, a simple trigger cannot send an email because the Gmail service requires authorization, but a simple trigger can translate a phrase with the Language service, which is anonymous.
They can modify the file they are bound to, but cannot access other files because that would require authorization.
They may or may not be able to determine the identity of the current user, depending on a complex set of security restrictions.
They cannot run for longer than 30 seconds.
In certain circumstances, editor add-ons run their onOpen(e) and onEdit(e) simple triggers in a no-authorization mode that presents some additional complications. For more information, see the guide to the add-on authorization lifecycle.
Simple triggers are subject to Apps Script trigger quota limits.
These restrictions do not apply to doGet(e) or doPost(e).

最新更新