本文介绍了Google App 脚本 onEdit?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

真的觉得我在电子表格对象脚本中遗漏了一些东西.

really feel like I'm missing something with the Spreadsheet object scripts.

我正在尝试自动向 onEdit 的协作者发送电子邮件.我在测试中显式运行脚本时成功发送电子邮件,但 onEdit 事件似乎从未被触发(甚至看不到日志消息).脚本看起来很简单.

I'm trying to automatically email collaborators onEdit. I successfully emailed when explicitly runnign the script in test, but the onEdit event never seems to get fired (not seeing log messages even). Script seems pretty straightforward.

function onEdit(e) {
  var sheet = e.source;
  var viewers = sheet.getViewers();
  var ct = viewers.length;
  var recipients = [];
  for(var i=0;i<ct;i++){
    recipients.push(viewers[i].getEmail());
  };
  var subject = 'Update to '+sheet.getName();
  var body = sheet.getName() + ' has been updated.  Visit ' + sheet.getUrl() + ' to view the changes ' + e.range;

  Logger.log('Running onedit');

  MailApp.sendEmail(recipients, subject, body);
};

推荐答案

简单的 onEdit 函数的可能操作集非常有限,因为它在未经潜在用户授权的情况下运行.您必须创建另一个函数并在此函数上设置特定触发器.(可安装触发器)

the simple onEdit function has a very limited set of possible actions since it runs without the authorization of the potential user. You have to create another function and set a specific trigger on this function.(installable trigger)

这篇文章 为例.它显示了简单的 onEdit 和可安装的编辑示例(用于电子邮件发送)这在此处的文档中有解释.

See this post as an example. It shows examples of simple onEdit and installable edit (for email send)This is explained in the documentation here.

这是您的代码工作:

function sendAlert() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getActiveSheet();
  var cell = ss.getActiveCell().getA1Notation()
  var viewers = ss.getViewers();
  var ct = viewers.length;
  var recipients = [];
  for(var i=0;i<ct;i++){
    recipients.push(viewers[i].getEmail());
  };
  var subject = 'Update to '+sheet.getName();
  var body = sheet.getName() + ' has been updated.  Visit ' + ss.getUrl() + ' to view the changes on cell ' + cell;
  MailApp.sendEmail(recipients, subject, body);
};

这篇关于Google App 脚本 onEdit?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-17 00:07