Zapier 更新工作表时运行 onEdit 触发器 [英] Running onEdit trigger when sheet is updated by Zapier

查看:30
本文介绍了Zapier 更新工作表时运行 onEdit 触发器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Google 电子表格,我想在每次 Zapier 自动化添加新行时自动更新一个字段.

I have a Google spreadsheet where I would like to automatically update one field every time a new row is added by a Zapier automation.

每次我通过添加一行或对现有行进行编辑来手动测试脚本时,脚本都会按预期触发.但是,当 Zapier 向电子表格添加新行时,绝对没有任何反应.

Every time I test the script manually by adding a row or making edits on existing rows, the script fires as expected. However, when Zapier adds a new row to the spreadsheet absolutely nothing happens.

知道为什么以及如何解决这个问题吗?

Any idea why and how I can remedy this problem?

这是我尝试使用的脚本:

This the script which I am trying to use:

function onEdit(e) {
    var row = e.range.getRow();
    SpreadsheetApp.getActiveSheet().
        getRange(row, 10).setValue('My_New_Value');
}

推荐答案

我能够复制您在脚本中看到的行为.我相信当您在活动数据范围之外添加一行时,它不会触发编辑事件.为了捕捉这些事件,您还需要捕捉 chagne 事件.

I was able to replicate the behavior you are seeing in your script. I believe that when you add a row outside of the active data range, it does not trigger an edit event. In order to catch those events, you will also need to catch chagne events.

https://developers.google.com/apps-script/reference/script/spreadsheet-trigger-builder#onchange

function onOpen ()
{ 
  var sheet = SpreadsheetApp.getActive();
  ScriptApp.newTrigger("myFunctionx")
   .forSpreadsheet(sheet)
   .onChange()
   .create();
 }

查看更改事件的文档,可以检测到插入了一行,但未返回行号.

Looking at the documentation for a change event, you can detect that a row was inserted, but the row number is not returned.

https://developers.google.com/apps-script/guides/triggers/events#change

function myFunctionx(e) {
    Logger.log("Change3");
    Logger.log(e.changeType);
}

您也许可以通过查看活动范围中的最后一行来找到插入的行.

You might be able to find the row that was inserted by looking at the last row in the active range.

function myFunctionx(e) {
    Logger.log("Change3");
    Logger.log(e.changeType);
    Logger.log(SpreadsheetApp.getActiveRange().getLastRow());
}

如果您复制并粘贴到多个单元格中,则更改可能会影响多行.您应该遍历范围内的所有行.

It is possible for a change to affect more than one row if you copy and paste into multiple cells. You should loop through all of the rows in your range.

function onEdit(e) {
    for(var i=e.range.getRow(); i<= e.range.getLastRow(); i++) {
        SpreadsheetApp.getActiveSheet().
            getRange(i, 10).setValue(e.range.getA1Notation());
    }
}

这篇关于Zapier 更新工作表时运行 onEdit 触发器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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