Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-apps-script/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
If statement Google脚本if语句,仅在最后一行使用条件格式_If Statement_Google Apps Script_Google Sheets_Gs Conditional Formatting - Fatal编程技术网

If statement Google脚本if语句,仅在最后一行使用条件格式

If statement Google脚本if语句,仅在最后一行使用条件格式,if-statement,google-apps-script,google-sheets,gs-conditional-formatting,If Statement,Google Apps Script,Google Sheets,Gs Conditional Formatting,我试图使用Google脚本将Google工作表中第二列最后一行的单元格设置为绿色,如果是: 1.0.00){ sheet.getRange(i,2,1,1).setFontColor('green'); } } 但是,缺点是这种方法会格式化列中的所有行。我只需要格式化最后一行 有没有办法避免在所有行上循环,只需检查最后一行是否符合上面的1.和2. 修改点: 在脚本中,sheet.getRange(lastrow,2,1,1)>0.00和sheet.getRange(lastrow,2,1,1

我试图使用Google脚本将Google工作表中第二列最后一行的单元格设置为绿色,如果是:

1.
0.00){
sheet.getRange(i,2,1,1).setFontColor('green');
}
}
但是,缺点是这种方法会格式化列中的所有行。我只需要格式化最后一行

有没有办法避免在所有行上循环,只需检查最后一行是否符合上面的1.2.

修改点:
  • 在脚本中,
    sheet.getRange(lastrow,2,1,1)>0.00
    sheet.getRange(lastrow,2,1,1)!='#不适用“
    表示与范围的比较。要比较单元格的值,可以使用
    sheet.getRange(lastrow,2,1,1).getValue()

  • 的状态谢谢!我完全缺少
    .getValue()
    部分。
    var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Sheet1');
    lastrow = sheet.getLastRow()
    if (sheet.getRange(lastrow, 2, 1, 1) >0.00 && sheet.getRange(lastrow, 2, 1, 1) !='#N/A') {
      sheet.getRange(lastrow, 2, 1, 1).setFontColor('green');
    }
    
    var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Sheet1');
    lastrow = sheet.getLastRow()
    var oValues = sheet.getRange(2, 2, lastrow, 1).getValues();
    
    for (var i = 0; i < oValues.length; i++) {
      if (oValues[i] >0.00) {
       sheet.getRange(i, 2, 1, 1).setFontColor('green');
      }
    }
    
    var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Sheet1');
    var range = sheet.getRange(sheet.getLastRow(), 2); // Last row of 2nd column
    var value = range.getValue(); // Value of last row of 2nd column
    if (value < 0 && value) { // <0 and not equal to #N/A
      // range.setBackground("green"); // This line give the background color of cell.
      range.setFontColor("green"); // This line give the font color of cell.
    }