Linked Questions

Popular Questions

Google app scripts: Assign zero for empty cells

Asked by At

I am new to Google Apps Script, trying to set values to a column based on its current value and a flag.

  1. If Flag = Y then floor the value in C1:

    C1 column value =23.9895
    Expected value=23

  2. If Flag = N then round the existing value in C1:

    C1 column value =23.9895
    Expected value=24

  3. If the flag is either Y or N then write 0:

    C1 column value=empty cell
    Expected Value=0

I already implemented the below code. It was working partially. The first two scenarios works fine but the third scenario fails.
When I try to set zero, I am getting #NUM! error instead of zero. Not sure how to do it.

...
do {
  sRange = "Q" + iCt;
  if ((gDecimalInPrice == "Y") && (!isNaN(sheet.getRange(sRange).getValue()))) {
    sheet.getRange(sRange).setValue(Math.abs(parseInt(sheet.getRange(sRange).getValue())));
  } else if ((gDecimalInPrice == "N") && (!isNaN(sheet.getRange(sRange).getValue()))) {
    sheet.getRange(sRange).setValue(Math.abs(Math.round(sheet.getRange(sRange).getValue())));
  } else {
    sheet.getRange(sRange).setValue(sheet.getRange(sRange).getValue());
  }
  iCt = iCt + 1;
} while (iCt <= gRowCt);

Related Questions