Home > Mobile >  Automatically adding default text to column when new row is added google sheets
Automatically adding default text to column when new row is added google sheets

Time:02-03

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')
  }
}

CodePudding user response:

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

CodePudding user response:

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.

  •  Tags:  
  • Related