0
votes

I have 2 scripts in the same project but it seems that only one is working, not specific but depending on the time I add the script, it is like the last script Im adding disabling the other.

these are the 2 scripts (I copied from google forum and updated according to my needs):

  1. adding an auto time stamp to a cell when specified range is not empty:
 function onEdit(event) {
      var sp = SpreadsheetApp.getActiveSheet();
      var c = sp.getActiveCell();
      if (c.getColumn() < 9 && sp.getName()=='QUE') {
        var celladdresp ='I'+ c.getRowIndex() 
        sp.getRange(celladdresp).setValue(new Date()).setNumberFormat("dd/mm/yy hh:mm");
      }
    };
  1. copy a row to another sheet on the same file if checkbox is checked (=true):
   function onEdit(event) {
         var ss = SpreadsheetApp.getActiveSpreadsheet();
         var s = event.source.getActiveSheet();
         var r = event.source.getActiveRange();
         if(s.getName() == "QUE" && r.getColumn() == 12 && r.getValue() == true) { var row = r.getRow();
           var numColumns = s.getLastColumn();
           var targetSheet = ss.getSheetByName("QUE2");
           var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
           s.getRange(row, 1, 1, numColumns-2).copyTo(target);
         }   
   };

Your help understanding why they dont work together will be appreciated. *BTW I tried to Enable/Disable new apps scriptspowered by chrome v8 but did not helped

1
Every function has to have a different name you’ll have to combine the two functions in the one - Cooper

1 Answers

0
votes

Answer:

You can only have one onEdit(event) function per script. If you wish both functions to run on edit, you need to combine them.

More Information

It's programming 101 that all functions and variables have to have unique names - otherwise your code isn't going to know which function or variable you're referring to. The uniqueness is important as you're defining exactly what you want.

In this case, as you have two onEdit(event) functions, when the sheet is edited your script is only calling one of these functions. This makes sense - it has no reason to continue looking for more doGet()s if it's already found and executed one.

Merging Functions:

When merging, it's important not to replicate code that is unneccesary. Reducing the number of calls so you only do exactly what you need to is important for efficient-running code:

function onEdit(event) {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getActiveSheet()
  var r = event.source.getActiveRange();
  var c = sheet.getActiveCell();  

  if (c.getColumn() < 9 && sheet.getName() == 'QUE') {
    var celladdresp = 'I' + c.getRowIndex() 
    sheet.getRange(celladdresp).setValue(new Date()).setNumberFormat("dd/mm/yy hh:mm");
  }

  if(sheet.getName() == "QUE" && r.getColumn() == 12 && r.getValue() == true) { var row = r.getRow();
    var numColumns = sheet.getLastColumn();
    var targetSheet = ss.getSheetByName("QUE2");
    var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
    sheet.getRange(row, 1, 1, numColumns-2).copyTo(target);
  }
}

I hope this is helpful to you!