Zdubya
Zdubya

Reputation: 101

Simple Counting Function in Google Scripts

Just started learning how to write functions in Google Script Editor. Please help me fix my error. I'm also unable to get the Logger.log(var) function to produce anything in the log.

Goal of the function: count attendance based on whether an 'X' was listed next to a name. Currently the range being iterated through is hard coded. Is it possible to pass through a custom range when the function is called?

Thank you.

function attendance() {

  var values = SpreadsheetApp.getActiveSheet().getDataRange().getValues();
  var count = 0
  for (n=6; n<12;++n){
    var cell = values[n][2];
    if (cell == "X"){
      count  = count +1;
    }
  }
  return (count);
}

Upvotes: 1

Views: 40

Answers (1)

Eric Svitok
Eric Svitok

Reputation: 842

Yes, you can use the length of the values array to make it more dynamic.

n < values.length

Upvotes: 1

Related Questions