Jae Nulton
Jae Nulton

Reputation: 11

Google Sheets Script multiple onEdit changes

A link to a sample spreadsheet is here.

I have been able to do all 3 of the following things individually:

  1. When writing to the 4th column adding a timestamp into Column1.
  2. When checkbox=TRUE adding a timestamp into Column8.
  3. When checkbox=FALSE clear the contents of Column 8.

The problem is I can never get more than one function at a time using onEdit. Could someone please tell me what I am doing wrong? I really would like all 3 aspects of the functionality to work at the same time. An example of the code snippets I am trying to combine are below.

function onEdit(e) {
        if(e.value != "TRUE" ) return;
    e.source.getActiveSheet().getRange(e.range.rowStart,e.range.columnStart+2).setValue(new Date());
}
function onEdit(e)
{ 
  var sheet = e.source.getActiveSheet();
  if (sheet.getName() == "order data") //"order data" is the name of the sheet where you want to run this script.
  {
    var actRng = sheet.getActiveRange();
    var editColumn = actRng.getColumn();
    var rowIndex = actRng.getRowIndex();
    var headers = sheet.getRange(1, 1, 1, sheet.getLastColumn()).getValues();
    var dateCol = headers[0].indexOf("Date") + 1;
    var orderCol = headers[0].indexOf("Order") + 1;
    if (dateCol > 0 && rowIndex > 1 && editColumn == orderCol) 
    { 
      sheet.getRange(rowIndex, dateCol).setValue(Utilities.formatDate(new Date(), "UTC+8", "MM-dd-yyyy")); 
    }
  }
}```
if( r.getColumn() == 16 ) { 
    var nextCell = r.offset(0, 1);
    if (r.getValue() == 1) {
        if( nextCell.getValue() === '' ) 
            nextCell.setValue(new Date()).setNumberFormat("MM-dd-yyyy hh:mm");
    } else {
        nextCell.setValue('');
    }
}```

Thank you all for any advice that could help me get past this.

Upvotes: 0

Views: 810

Answers (1)

Mike Steelson
Mike Steelson

Reputation: 15318

Try

function onEdit(e){
  onEdit1(e)
  onEdit2(e)
  onEdit3(e)
}

and give onEdit1, onEdit2, onEdit3 to your individual functions

function onEdit(e){
  onEdit1(e)
  onEdit2(e)
  onEdit3(e)
}
function onEdit1(e){
  Browser.msgBox('first onEdit function')
}
function onEdit2(e){
  Browser.msgBox('second onEdit function')
}
function onEdit3(e){
  Browser.msgBox('third onEdit function')
}

assuming that all individual function works correctly

Upvotes: 1

Related Questions