0

I am writing a simple custom function to iterate through a column, and if a cell has a certain value, to check the value of a different column in that same row. If the value in that cell has a certain value, increase a counter. For some reason, I am getting a TypeError when trying to read a specific column.

Here is the error message I am getting:

TypeError: Cannot read property '0' of undefined (line 22).

Line 22 being: if (data[row][0] === "example") {

Does anyone have any solutions? Thank you.

  
  var sheet = SpreadsheetApp.getActive().getSheetByName('Form Responses 1');
  var lastRow = sheet.getLastRow();
  var lastCol = sheet.getLastColumn();
  
  var range = sheet.getRange(2, 2, lastRow, lastCol);
  var data = range.getValues();
  
  var count = 0;

  data.forEach(function (row) {
    if (data[row][0] === email) {
      if(data[row][1] === meeting){
      count += 1;
    }
  }
  });
  return data;
}

1 Answer 1

1

Instead of

data.forEach(function (row) {
    if (data[row][0] === "[email protected]") {
      if(data[row][1] === "Leadership"){
      count += 1;
    }
  }
  });

try

data.forEach(function (row) {
    if (row[0] === "[email protected]") {
      if(row][1] === "Leadership"){
      count += 1;
    }
  }
  });

The above assumes that the first column might contain "[email protected]" and the second might contain "Leadership"

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.