7

I want to count a number of specific responses from a form where the response can be left empty, zero or any other number 'd'.

The following code:

var data = sheet.getRange("B2:L").getValues();

var d = 0;
if (data[i][j] == d){
    count++;
}

assumes that an empty cell == 0 is true, thus counting the wrong number of cells that actually hold the value zero

var d = 0;
if (data[i][j] != '' && data[i][j] == d){
    count++;
}

or any variation with != null / != "" does not seem to work for me

EDIT : Link to document. I think you can see the script if you make a copy.

4
  • Can you share a doc with us? Jan 24, 2013 at 17:53
  • Added the complete code (at least the counting part) will be helpful as well. Can you edit your question? Jan 24, 2013 at 18:15
  • 1
    Added a link to my document.. hope you're able to see the script.
    – Raven
    Jan 24, 2013 at 19:42
  • Nicely styled sheet you have created !! I will take a look. Jan 24, 2013 at 20:13

3 Answers 3

3

When counting for the different d values, you need to check whether the objects in the array are string or number based:

for (var j=1; j<lastColumn-1; j++) {
  if (typeof data[i][j] !== 'string') {
    count[+data[i][j]][d]++;   
  }
}
1
  • Awesome, that worked perfect! Thanks a lot :)
    – Raven
    Jan 24, 2013 at 23:15
5

Have you tried isBlank() function?

5
  • OP is after a script solution. Nov 19, 2014 at 12:55
  • 1
    @JacobJanTuinstra, isBlank() is a Google Apps Script function (Check the link). Nov 20, 2014 at 7:26
  • @Punchlinern Didn't know that... Nov 20, 2014 at 9:27
  • 1
    You learn something everyday :) Nov 20, 2014 at 9:28
  • @phermous: Have you tried it? Nov 20, 2014 at 9:28
3

You can also use the following to determine empty, zero-value or 'else'.

 if (cell === "") // empty cell
     doEmptyCellStuff();
 else
 if (cell == 0)  // zero-value cell
     doZeroValueCellStuff();
 else
     dValueCellStuff();
2
  • 2
    The title doesn't correspond to the problem faced. I had to dig in deep to understand real problem. This isn't resolving the problem Sep 17, 2013 at 13:58
  • Why not? Seems like it will correctly skip empty cells. It's just that doEmptyCellStuff should be a no-op in the OP's case... and the last else can be left off.
    – DylanYoung
    Sep 9, 2017 at 19:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.