Search code examples
google-sheetsgoogle-apps-script

In Google app script, how do I count the number of cells in a row that aren't blank?


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?


Solution

  • 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 getDisplayValues and getValues can be used.
    • By this, the number of cells that the cell value is not empty in "B2:2" is obtained.