【问题标题】:Trigger when user insert row in google sheet and return the list of new created row numbers当用户在 google sheet 中插入行并返回新创建的行号列表时触发
【发布时间】:2021-10-03 19:40:42
【问题描述】:

当用户插入行(在谷歌表格中)并获取新创建的行号列表时,我使用谷歌脚本来捕获事件。

我试过 onChange :

function onInsertRow(e){
  if (e.changeType == "INSERT_ROW") { 
    SpreadsheetApp.getUi().alert("insert");
  }
}

触发器有效,但 onChange() 的事件对象没有范围属性。那么如何获取已插入的行号列表呢?

【问题讨论】:

    标签: google-apps-script google-sheets triggers event-handling


    【解决方案1】:

    下面是代码:

    1. 您必须设置一个新触发器。

    您可以使用 getRow 或 geRowIndex 找出哪一行发生了变化。

    希望这会有所帮助。

    function setUpTrigger(){
      ScriptApp.newTrigger('on_insert').forSpreadsheet('id').onChange().create();
    }
    
    function on_insert(e){
      const sheet = SpreadsheetApp.getActiveSheet(); 
      sheet.appendRow([e.changeType,e.source.getActiveSheet().getActiveRange().getRowIndex()])
    }
    

    【讨论】:

    • 我使用 onChange() 因为我尝试使用 onEdit() 并且在插入行时无法触发它。它仅在我编辑单元格时才有效。你是如何让它发挥作用的?
    • 我更新了上面的代码。您必须为此工作表设置触发器。然后,每次您在此工作表上进行更改时,都会触发触发器。
    • 很高兴为您提供帮助!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-13
    • 1970-01-01
    • 1970-01-01
    • 2017-12-10
    • 2023-01-22
    相关资源
    最近更新 更多