This seems like it should be very straight forward, but doesn't seem to be. Something like sheet.getRange(B2:2).getValues().filter(notBlank) but what do I put in place of notBlank?
1 Answer
In your situation, how about the following modification?
Modified script:
const res1 = sheet.getRange("B2:2").getDisplayValues()[0].filter(String).length;
console.log(res1)
// or
const res2 = sheet.getRange(2, 2, 1, sheet.getLastColumn() - 1).getDisplayValues()[0].filter(String).length;
console.log(res2)
- In this case, I think that both
getDisplayValuesandgetValuescan be used. - By this, the number of cells that the cell value is not empty in "B2:2" is obtained.
2 Comments
jonnybolton16
Ahh brill thankyou. I tried this initially but it didn't work because I didn't use
[0] before filter. I think where I went wrong was I was assuming the result of getValues was a 1D array, but it's 2D, so the [0] was required to pull out the 1D row arrayTanaike
@jonnybolton16 Thank you for replying. Yes. In the case of "B2:B", when the value retrieved by
getValues is like [["b2","c2","d2",,,]]. So, I used [0]. By this, it can be used as a 1-dimensional array.