onEdit(e)不在附加组件中工作

时间:2016-01-15 22:31:04

标签: google-apps-script google-sheets triggers google-apps-script-addon

我编写了一个在本机电子表格中使用时效果很好的脚本。我现在正尝试将其作为附加组件发布,并且当onEdit(e)onOpen(e)工作正常时,我发现onInstall(e)无效。

我查看了有关授权模式和安装/启用附加组件的文档,但我认为我可能缺少一些东西(希望直截了当)因为我是初学者。我应该以不同的方式调用函数吗?或onEdit的展示位置?任何帮助表示赞赏。谢谢!

function setup() {
  var ui = SpreadsheetApp.getUi();
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetByName('Send Auto Emails');

  try {ss.setActiveSheet(ss.getSheetByName('Send Auto Emails'));}
  catch (e) {ss.insertSheet('Send Auto Emails', 0);}

  sheet.getRange(1, 1).setValue('Recipient Email Address');   

  //etc...
}

function onEdit(e) {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = e.source.getActiveSheet();
  var range = e.source.getActiveRange();

  if (range.getA1Notation() == "C1" | range.getA1Notation() == "D1" |     range.getA1Notation() == "E1" && sheet.getName() == "Send Auto Emails") {
    Browser.msgBox(
      'Alert',
      'Feel free to change the title here to something more relevant to you.  But be aware, if you would like to use an optional item, please make sure you are referencing it in your email message exactly as it appears here, wrapped in < and >. Example: <Optional Item 1>.',
      Browser.Buttons.OK
    );
  }
  if (range.getA1Notation() == "J4" && sheet.getName() == "Send Auto     Emails") {
    Browser.msgBox(
      'Alert',
      'Only add the email message body.  "Hello, Recipient Name" and "Best, Your Name" will be automatically added.  If you would like to use Optional Items in this message, see the example text to make sure you are using them the right way.',
      Browser.Buttons.OK
    );
  }

  if (range.getA1Notation() == "A2") {
    ss.toast("Your data in column A must not be separated by any blank rows. Any data after a blank row will be ignored.", "Be aware", 90);
  }
}

function onOpen(e) {
  var ui = SpreadsheetApp.getUi();
  ui.createMenu('BulkEmail beta')
      .addItem('1. Sheet Setup', 'setup')
      .addItem('2. Send Emails', 'sendEmails')
      .addToUi();
  onEdit(e);
}

function onInstall(e) {
  onOpen(e);
}

编辑1

我尝试过创建一个可安装的触发器,而不是使用简单的onEdit,但仍无济于事。

function createonEdit() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  ScriptApp.newTrigger('installableonEdit')
      .forSpreadsheet(ss)
      .onEdit()
      .create();
}

function installableonEdit(e) {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = e.source.getActiveSheet();
  var range = e.source.getActiveRange();

  try {
    if (range.getA1Notation() == "C1" | range.getA1Notation() == "D1" | range.getA1Notation() == "E1" && sheet.getName() == "Send Auto Emails") {
      Browser.msgBox(
        'Alert',
        'Feel free to change the title here to something more relevant to you.  But be aware, if you would like to use an optional item, please make sure you are referencing it in your email message exactly as it appears here, wrapped in < and >. Example: <Optional Item 1>.',
        Browser.Buttons.OK
      );
    }
    if (range.getA1Notation() == "J4" && sheet.getName() == "Send Auto Emails") {
      Browser.msgBox(
        'Alert',
        'Only add the email message body.  "Hello, Recipient Name" and "Best, Your Name" will be automatically added.  If you would like to use Optional Items in this message, see the example text to make sure you are using them the right way.',
        Browser.Buttons.OK
      );
    }

    if (range.getA1Notation() == "A2") {
      ss.toast("Your data in column A must not be separated by any blank rows. Any data after a blank row will be ignored.", "Be aware", 90);
    }
  } catch(err) {
    var errMsg = 'There was an error: ' + err +
        + " \n \n" +
        'from the: onEdit function ' +
        + " \n \n" +
        'The call stack is: ' + err.stack;

    GmailApp.sendEmail('elisabeth@groupon.com', "error", errMsg);
  }
}

2 个答案:

答案 0 :(得分:0)

您的onOpen()触发器正在运行onEdit()触发器。安装并启用加载项时,opOpen()在AuthMode.LIMITED中运行。

this documentation中,它声明:

  

允许访问有限服务子集的模式(LIMITED)。当绑定到文档的附加组件或脚本执行onOpen(e)或onEdit(e)简单触发器时,会发生此(有限)模式,除非为NONE描述。

您正在运行onOpen()个简单触发器,它正在限定模式下运行,因为它位于附加组件中。

所以,那部分,我很确定。

我相信你能做的是创建一个可安装的编辑触发器,并以FULL模式运行。所以,这就是我要尝试的,摆脱简单的触发器,并使用ScriptApp安装触发器。

在文档中,它声明:

  

他们(一个简单的触发器)无法访问需要授权的服务。例如,简单的触发器无法发送电子邮件,因为Gmail服务需要授权,但简单的触发器可以使用匿名的语言服务翻译短语。

Google Documentation

因此, try / catch 包括发送电子邮件,这会阻止onEdit()简单触发器工作。

在您的代码中添加 try / catch ,如果发生错误,请发送电子邮件给您自己。

function onEdit(e) {try{
  //Code Here

} catch(err) {
  var errMsg = 'There was an error: ' + err +
      + " \n \n" +
      'from the: onEdit function ' +
      + " \n \n" +
      'The call stack is: ' + err.stack;

  GmailApp.sendEmail('yourEmail@gmail.com', "Subject", errMsg);
};

答案 1 :(得分:0)

一个可能的原因是您“作为附件测试”

https://developers.google.com/gsuite/add-ons/how-tos/testing-editor-addons#testing_details

  

测试时不支持可安装的触发器。的功能   取决于可安装的触发器是不可测试的。

但是有趣的是,它可以与绑定脚本一起使用。

这让我困惑了好一阵子。因此,解决方案是当您需要测试可安装的触发器时,只需坚持绑定文档即可。

如果可以,该插件的触发器也将起作用。

对于其他事情,您可以执行“作为附加组件测试”