Google脚本,onEdit函数突然不起作用,如何解决?

时间:2019-03-19 19:18:06

标签: google-apps-script google-sheets synchronization

我在电子表格中具有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");
}

mainfile函数将为我提供特定文件的ID,而注释掉的部分正是我试图将其实现到onEdit函数中的地方。因此,预期的结果是我可以在一个电子表格上进行编辑,而在另一电子表格上进行相同的更改,日志显示到目前为止,对于数字2,3,4,Woriking仍然显示,但是什么都没有出现。

当我运行mainfile函数时,它运行良好。我也知道这可能是here的转贴,但由于他们实际上没有得到答案,而且它只是固定了它,我认为它可能没有资格。

它不是其他帖子的副本,因为我没有尝试发送电子邮件。我已经看过simple triggers guide了,但我不知道这段代码有什么问题,因为它不要求正常运行该函数的权限,所以我认为我不需要进行身份验证即可运行它,我知道它可以修改其他文件,因为它曾经可以工作,并且我今天可以工作,它运行的时间不超过30秒,而且我还没有超出配额。其他似乎都不适用。请您向我解释一下我在做什么错,因为我听不懂。

我还用工作表ID替换了工作表ID。所有onEdit()代码的功劳都归库珀所有。

在此先感谢您。”

1 个答案:

答案 0 :(得分:0)

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

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).