首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Google脚本,onEdit函数突然不工作了,怎么修复?

Google脚本,onEdit函数突然不工作了,怎么修复?
EN

Stack Overflow用户
提问于 2019-03-20 03:18:06
回答 1查看 2.9K关注 0票数 2

我在一个电子表格中有一个onEdit函数,我需要将它复制到另一个电子表格中,其中另一个电子表格是相同的副本,具有相同的表名。我最后一次检查时,它工作得很好,但那是几天前的事了,现在它已经停止了。

代码:

代码语言:javascript
复制
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函数中的地方。因此,预期的结果是我在一个电子表格上进行了编辑,并在另一个电子表格上进行了相同的更改,日志将显示Woriking for numbers 2,3,4,但没有显示任何内容。

当我运行mainfile函数时,它工作得很好。我也意识到这可能是here的转载,但他们实际上并没有得到答案,它只是修复了自己,我认为它可能不符合条件。

这不是另一个帖子的复制品,因为我不想发送电子邮件。我已经看过simple triggers guide了,我不知道这段代码有什么问题,因为它没有请求正常运行函数的许可,所以我不认为我需要授权才能运行它,我知道它可以修改其他文件,因为它过去是工作的,今天我让它工作了,它运行的时间不超过30秒,我没有超过配额。其他的似乎都不适用。你能给我解释一下我做错了什么吗,因为我不明白。

我还将工作表ID替换为工作表ID。所有用于onEdit()代码的信用都转到库珀。

提前谢谢你,很抱歉对你大喊大叫。

EN

回答 1

Stack Overflow用户

发布于 2019-03-20 03:49:41

简单的触发器不能做需要授权的事情。

代码语言:javascript
复制
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).
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55248536

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档