Home > Blockchain >  Why does comparing the string TRUE to the value from getValue fail?
Why does comparing the string TRUE to the value from getValue fail?

Time:07-07

I'm trying to move an entire row to another sheet based on a specific value. See code below:

function onEdit(e) {
  const src = e.source.getActiveSheet();
  const r = e.range; 
  if (src.getName() == "Sheet1" && r.columnStart == 4 && r.getValue() == "TRUE") {
  const dest = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet2");
  src.getRange(r.rowStart,1,1,19).moveTo(dest.getRange(dest.getLastRow() 1,1,1,19));
  src.deleteRow(r.rowStart);
}
}

The function works when I delete the getValue part, since the script then basically runs on the fact that something is edited. But I'd like it to only work if the value is TRUE, hence the getValue.

Any idea why this is not working?

CodePudding user response:

getValue() returns a Javascript object, whose type is according to the type of value in the spreadsheet(=TYPE(D4))1.

Boolean TRUE in sheet is converted to javascript true. Try

&& r.getValue() === true 

Or just

&& r.getValue()

On the other hand, e.value is always provided as a string.

&& e.value === 'TRUE'
  • Related