简体   繁体   中英

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

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?

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'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM