0

I use google script to capture event when user insert rows (in google sheet) and fetch the list of row number that's newly created.

I tried onChange :

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

Trigger works but the event object of onChange() doesn't have range property. So how can i get the list of row numbers that has been inserted ?

1 Answer 1

3

Below is the code:

  1. you have to set up a new trigger.

you can use the getRow or geRowIndex to find out which row has changged.

Hope this will help.

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()])
}
Sign up to request clarification or add additional context in comments.

3 Comments

I use onChange() because I tried using onEdit() and it can't be triggered when i insert rows. It works only when i edit the cells. How do you make it work ?
I updated the code above. You have to set up a trigger for this sheet. Then each time you made changes on this sheet, trigger will be fired.
So happy to help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.