I am trying to use a Google script to set a cell in the last row only of the 2nd column in a Google sheet to green color if it is:
1. <0
, and
2. not equal to #N/A
Partial preferred approach
I have the following if
statement (without using a loop):
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');
}
However, this is not working. It is simply not assigning the color green to the font.
Not the preferred approach
I could do this using a loop, based on this answer, and loop over all rows in the column one at a time:
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');
}
}
However, the disadvantage is that this approach is formatting all the rows in the column. I only need to format the last row.
Is there a way to avoid looping over all rows and just check if the last row meets 1. and 2. from above?