Home > Mobile >  Adding additional criteria in if statement
Adding additional criteria in if statement

Time:08-24

Struggling with these & feel like I've put together all the possible combinations, just not fully understanding.

I have this amazing script that records the timestamp when a checkbox turns true in the same row (just a different column).

I am trying to in essence, add another level to it, where I can set the status for a DIFFERENT column, and then record a timestamp for that as well.

In other words, this is the working script (one if):

    function onEdit(e) {
      const sheet = e.range.getSheet();
    
    
    
      if (sheet.getName() !== 'TEST SHEET'
        || sheet.getRange('P'   e.range.rowStart).getValue() !== 'In Progress') {
        return;
      }
      sheet.getRange('AK'   e.range.rowStart)
        .setValue(new Date())
        .setNumberFormat('MM/dd/yyyy');
}

I'm trying to make it something like:

function onEdit(e) {
  const sheet = e.range.getSheet();



  if (sheet.getName() !== 'TEST SHEET'
    || sheet.getRange('P'   e.range.rowStart).getValue() !== 'In Progress') {
    return;
  }
  sheet.getRange('AK'   e.range.rowStart)
    .setValue(new Date())
    .setNumberFormat('MM/dd/yyyy');

    
  if (sheet.getName() !== 'TEST SHEET'
    || sheet.getRange('P'   e.range.rowStart).getValue() !== 'Completed') {
    return;
  }
  sheet.getRange('AL'   e.range.rowStart)
    .setValue(new Date())
    .setNumberFormat('MM/dd/yyyy');
    
}

Doing some research, seems like nothing would work after "return".. I tried adding in the fields before, and did some testing moving everything around but still now luck.

Is there something simple I'm missing here?

CodePudding user response:

I'd add a parameter for each value that may change (the range to set, and the value to compare against), then call that function twice.

function tryInsertDate(e, rangeToSet, val) {
    const sheet = e.range.getSheet();
    if (
        sheet.getName() !== 'TEST SHEET'
        || sheet.getRange('P'   e.range.rowStart).getValue() !== val
    ) {
        return;
    }
    sheet.getRange(rangeToSet   e.range.rowStart)
        .setValue(new Date())
        .setNumberFormat('MM/dd/yyyy');
}
function onEdit(e) {
    tryInsertDate(e, 'AK', 'In Progress');
    tryInsertDate(e, 'AL', 'Completed');
}

If those are states, you could name the parameter state to be more precise.

  • Related