onChange 触发器未按预期工作 [英] onChange Trigger not working as expected

查看:27
本文介绍了onChange 触发器未按预期工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个谷歌表,它有一个基于定时触发器的自动刷新 IMPORTXML 功能,我正在尝试编写一个脚本,当它检索到具有帮助"消息类型的行时,将自动通过电子邮件发送通知警报.

I have a google sheet that has an auto refreshing IMPORTXML function on it based on a timed trigger and am trying to write a script that will auto email a notification alert when it retrieves a row with a "HELP" message type.

我根据我发现的一些例子编译了这个脚本.我已经对其进行了测试,并且它可以与使用 onEdit 可安装触发器手动输入的数据一起正常工作,但是从我的测试(并基于研究)来看,我需要使用 onChange 触发器来让 IMPORTXML 触发脚本.

I compiled this script based on some examples I found. I have tested it and it works fine with manually entered data with an onEdit installable trigger but from my testing (and based on research) I need to use the onChange trigger to have the IMPORTXML trigger the script.

但是,当我将脚本设置为 onChange 可安装触发器时,该脚本似乎既不会使用 IMPORTXML 加载数据自动执行,也不会由我在工作表上手动输入.

However, when I set the script to a onChange installable trigger, the script doesn't seem to execute either automatically with the IMPORTXML loading data or by me manually entering it on the sheet.

我是否遇到了 Good Apps Script 的一些限制?这是我第一次使用它(和 JavaScript)

Am I hitting some limitation of Good Apps Script? This is my first time using it (and JavaScript)

function helpAlertEmail(e) 
{
  if (e.range.columnStart !== 5 || e.value !== 'HELP' && e.value !== 'HELP-CANCEL') return;
  var ss = e.source.getActiveSheet()
  var details = ss.getRange(e.range.rowStart, 1, 1,11).getValues()[0];
  var headers = ss.getRange(1, 1, 1, 11).getValues()[0];
  var subject = "SPOT BEACON ALERT:  " + details[2] + " Sent a " + details[4] + " Message at " + details[9];
  var body = "SPOT Beacon " + details[2] + " (" + details[1] + ")  Sent a " + details[4] + " Message at " + details[9] +  "

";
  var email = "xxx@xxx.org";
  var cols = [0, 4, 5, 6, 9, 10];

  for (var i = 0; i < details.length; i++) 
  {
    if (cols.indexOf(i) === -1) continue;
    body += headers[i] + ": " + details[i] + "
"
  }

  body += "


 Please do not respond to this email as it is automatically generated by an account that is not checked.";
  MailApp.sendEmail(email, subject, body, {noReply:true});
}

推荐答案

根据你对用例的解释,我希望你知道这一点:

By what I understand from your explanation about the use case, I would like you to know this:

1) onEdit - 指定在编辑电子表格时触发的触发器.

1) onEdit - Specifies a trigger that will fire when the spreadsheet is edited.

2) onChange - 指定一个触发器,当电子表格的内容或结构发生更改时将触发该触发器.

2) onChange - Specifies a trigger that will fire when the spreadsheet's content or structure is changed.

这些是 Google 规定的条件.

These are the conditions stated by Google.

现在它没有告诉我们的是:

1) 必须手动编辑

现在你说你也手动尝试过,但触发器没有触发.一个可能的原因是您可能从某处复制了数据,然后将其粘贴到您想要的位置.因此,您不能指望在这种情况下触发触发器.

Now you said you tried manually as well but the trigger did not fire. A possible reason for that is that you might have copied the data from somewhere and then pasted it where you want it. So, you cannot expect the trigger to fire in that case.

2) 编辑不能是任何形式的自动化(不能使用电子表格公式或使用任何脚本来完成).在这种情况下,触发器也不会触发.

2) The edit cannot be any form of automation (it cannot be done using a spreadsheet formula or using any script). In this case as well, the trigger would not fire.

因此,总而言之,您所面临的情况不是 Google 声明的任何限制的后果.可悲的是,它就是这样.

So, in all, what you are facing is not a consequence of any of the limitations stated by Google. Sadly, It is what it is.

您需要做的是,想出另一种方式来实现您想要实现的目标,即使用除 onChangeonEdit 之外的其他内容> 触发器.

What you will have to do is, think of another way to go about what you are trying to achieve, which is, use something else except the onChange and the onEdit triggers.

这篇关于onChange 触发器未按预期工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆