a person
a person

Reputation: 1

onEdit trigger broke after trying to change to onChange

just need some quick oversight on why when I changed from onEdit to onChange it broke the script, ive tried for ~2 days to figure out the problem, still don't know what I did wrong so bear with me, I'm sure there is alot of headache on a public forum and I apologize in advance for being one of them; I know that the Simple and Installable triggers are different I just don't know how to convert to the new 'ideal' format

It's supposed to look for change in the Sheet "RAW" and then create a row on top of Sheet "Raw" and "Examples" with some functions applied to

it also creates a new row when an edit is found in Examples, I'm trying to figure out onChange range notation as well

Here's a link to the sheet, please check the script and see what I did wrong; https://docs.google.com/spreadsheets/d/1GKrS-5e6ArW8g8CXgFE_Tzow6PjDtYoEjzLCBg6cNrg/edit?usp=sharing

function createSpreadsheetOpenTrigger(e) {
    if (!e) {
        throw new Error('Script now running');
    }
    if (e.range.getA1Notation() !== 'A2') {
        return;
    }
    var ss = SpreadsheetApp.getActive();
    ScriptApp.newTrigger('myFunction')
        .forSpreadsheet(ss)
        .onOpen()
        .create();
}
sh.insertRowAfter(1);
sh.setRowHeight(2, 21);
var freeze = sh.getRange("A2:C2");
freeze.copyTo(freeze, {
    contentsOnly: false
});
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sh = ss.getSheetByName("Examples");
sh.insertRowAfter(1);
sh.setRowHeight(2, 21);
sh.getRange("A2").setFormula('=RAW!A3');

What it looked like before;

function onEdit(e) {
  if (!e) {
    throw new Error('Script now running');
  }
  if (e.range.getA1Notation() !== 'A2') {
    return;
  }
  var ss = SpreadsheetApp.getActiveSpreadsheet(); 
  var sh = ss.getSheetByName("RAW"); 
  sh.insertRowAfter(1); 
  sh.setRowHeight(2,21);
  var freeze = sh.getRange("A2:C2"); 
  freeze.copyTo(freeze,{contentsOnly:false});   
  var ss = SpreadsheetApp.getActiveSpreadsheet(); 
  var sh = ss.getSheetByName("Examples"); 
  sh.insertRowAfter(1); 
  sh.setRowHeight(2,21);
  sh.getRange("A2").setFormula('=RAW!A2');
  var freeze = sh.getRange("A2:C2"); 
  freeze.copyTo(freeze,{contentsOnly:false});   
}

Upvotes: 0

Views: 119

Answers (1)

ale13
ale13

Reputation: 6072

The onEdit and onChange triggers work differently, hence the event objects they use have different methods.

According to the event objects documentation, the onChange trigger uses the following:

  • authMode
  • changeType
  • triggerUid
  • user

So essentially, the main issues when switching from the onEdit trigger is the fact that you are still using the range.

Reference

Upvotes: 1

Related Questions