0

I'm trying to create a script that will trigger whenever a new row is added to my sheet, and add a text to column f in the new row only. I have the following script, but it didn't work. Would love your help. Thanks so much!

    function myFunction(e) {
 var sh = SpreadsheetApp.getActiveSheet();
 if(e.changeType === 'INSERT_ROW') {
 var row = sh.getActiveRange().getRow();
 sh.getRange(row, 6).setValue('defaultValue')
  }
}

2 Answers 2

1

The code is excellent, I was able to make it work, however I notice that in order to make it work you need to make sure to add a trigger.

In this case "onChange". On App Script Make sure to go add a trigger for this function to work over the spreadsheet

Sign up to request clarification or add additional context in comments.

7 Comments

Once you do that, just add a new row over the Spreadsheet and it should show you the "defaultValue" over column F and the row it was added.
yes it does thanks, missed that part the trigger I put is on open not on change.
Awesome, let me know if it works for you.
another thing that seems to work if I am the one making the change, not by google forms for example, or anyone have the editing permision
Interesting, I would assume that this can also be modified to work with "onEdit", happy to help.
|
1

You can add triggers programmatically

var ss = SpreadsheetApp.getActive();

function createSpreadsheetChangeTrigger() {
  ScriptApp.newTrigger('onChange')
    .forSpreadsheet(ss)
    .onChange()
    .create();
}

function onChange(e) {
  if (e.changeType === 'INSERT_ROW') {
    var row = ss.getActiveRange().getRow();
    ss.getRange(row, 6).setValue('defaultValue')
  }
}

That's because the onChange trigger is installable and you can do it from the user interface or with the code above. The formSubmit and onEdit work in the same way.

Comments

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.