title: Google Script with Spreadsheet - Function to Count Column Values
body: <p>I'm building a Google spreadsheet where I'll enter a bunch of data into that spreadsheet, then on another sheet, I want to pull statistics from it. I'm having a hard time counting the values correctly.</p>
  
  <p><strong>What I'm attempting to accomplish</strong>
  - Build a function that I can add to any cell to get the statistics of a certain column by counting repeating values based on the target/argument in the function.</p>
  
  <p>Example:</p>
  
  <blockquote>
    <p>Sheet 1 has the data I need to calculate in column E (holds 1 or 2 letters to indicate a status.
     Sheet 2 is where I will pull all statistical data.
     I add a function like so:   =getData("A")</p>
    
    <p>The function itself would already point to sheet1 and column E. What the function is saying here is, "I want you to pull all of the data in column E and tell me how many cells are marked as A" and then give me the count.</p>
    
    <p>I have about 5 different values I would independantly search for here. We'll say A, B, C, D and E. So if I go to search for C, I would pull the stats into another cell on my statistics sheet using: =getData("C")</p>
  </blockquote>
  
  <p><strong>What I've gotten so far (that doesn't work)</strong>
  - I'm pretty sketchy at coding this stuff, so I'm still learning a lot. Here's what I was attempting so far:</p>
  
  <pre><code>function getData(target) {
    var ss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("SHEET1");
    var lrow = ss.getLastRow();
    var r = ss.getRange(2, 5, lrow, 1);
    var data = r.getValues();
    var count;
  
    for (var i = 0; i &lt; lrow ; i++) {
      var count = 1;
      for (var j = 0; j &lt; i; i++) {
        if (data[j][0].value(target) == data[i][0].value(target)) {
          count++;
        }
      }
    return (count);
    }
  }
  </code></pre>
  
  <p>I also tried:
      if (data.value() == target) { count++; }</p>
  
  <p>But that didn't seem to help either. What am I doing wrong? Either I'm not counting correctly and/or not pulling the data to count it correctly. I've searched quite a few posts and can't seem to pull together what I need.</p>
  
  <p>I appreciate any help anyone could offer. Thanks.</p>
  
  <hr>
  
  <p><strong>Resolved Code ( Thanks @ScampMichael )</strong> - This slight variation actually allows me to search for multiple values using the format =getData("Example1","Example2") and counting the total:</p>
  
  <pre><code>function getData(target1, target2) {
    var ss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("SHEET1");
    var lrow = ss.getLastRow();
    var r = ss.getRange(2, 5, lrow-1, 1); // lrow-1 to not count header
    var data = r.getValues();
    var count = 0; // 0 must be declared here or you'll get error: Overflow on the spreadsheet
  
    for (var i=0; i&lt;data.length; i++) {
      if (data[i][0] == target1) { // can repeat this part depending how many values you want to search for
        count++;
      }
      if (data[i][0] == target2) {
        count++;
      }
    }
    return (count);
  }
  </code></pre>