简体   繁体   中英

Loop through a column and change value of another column based on cell values in Google Apps Script

The following script should change cell values in column K based on values in column D. However, the comparator is not working, it always returns "NO".

Is range.toString the correct syntax? I think I'm not comparing Apples to Apples.

function Test1() {

   var ss = SpreadsheetApp.getActiveSpreadsheet();
   var s = ss.getSheetByName("report");
   var numRows = SpreadsheetApp.getActiveSheet().getLastRow();
   var range;

  /* Loop through Column D and find cells equal to order payment
  and set Column K value based on it */

   for (var i = 1; i <= numRows; i++) {
      range = s.getRange('D' + i );
      if (range.toString() == "Order Payment") {
        range.offset(0, 6).setValue("YES"); 
      }
     else {
       range.offset(0,6).setValue("NO");
     }
   }
}

range.getValue() == "Order Payment"是正确的语法,而不是range.toString()

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