0

I have captured a data range in an array. My column 3 of the array remains constant and only my row needs to increment while assigning this value to a variable matchindex. While assigning the value to a variable I am getting an error stating

'Çannot read property 3 from undefined'.

I am getting this error on the line while assigning matchindex = data[i+2][3].

Can somebody please explain what is going wrong?

Below is my script

function testrange() {
 var spreadsheet = SpreadsheetApp.getActive();
  var i=0;
  var temp1 =2;
  var temp2 =2;
  var startrow;
  var endrow;
  var matchindex;
  var ssVillage = spreadsheet.setActiveSheet(spreadsheet.getSheetByName('VillageName'),true);  
  var range = ssVillage.getDataRange();
  var data = range.getValues();
  var lastcol = ssVillage.getLastColumn();


for (i=0;i<data.length;i++)
{
  matchindex = data[i+2][3];

  temp1 = matchindex;
  if(temp1 !== temp2) {   

   // spreadsheet.setNamedRange(ssVillage.getRange("B"+startrow).getValue(),ssVillage.getRange("A"+startrow +":A"+endrow).activate() );    
      spreadsheet.setNamedRange(data[i+2][startrow],ssVillage.getRange("A"+startrow +":A"+endrow).activate() );            
      if (i+2 == matchindex) {
       startrow = (i+2);
       endrow = i+2;
      }      
      else {
        endrow = i+2;
      }
    }

  else {
    if (i+2 == matchindex) {
       startrow = (i+2);
       endrow = i+2;
      }      
      else {
        endrow = i+2;
      }
  }
  temp2 = matchindex;

  }
}  
2
  • so basically that value doesn't exist in array. did you check your array ? Commented Sep 21, 2018 at 11:06
  • yes..Array shows the value when i checked. What i am trying to do is define namedranges for col A, based on col B values. Also i get an error where i define the namedrange. When i use logger.log to check outside the for loop the values are shown correctly Commented Sep 21, 2018 at 11:53

1 Answer 1

1

You are iterating in for loop from i=0 to the length of array ( say L). In last 2 iterations, the value of (i+2) will be L, L+1. And definitely there will not be an index equal to length of array or more than that. Since L and L+1 index does not exist, value of data[i+2] will be undefined hence getting the error.

'Çannot read property 3 from undefined'.

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

1 Comment

Thanks Waqar. This explains it. Do you have a suggestion to offer to tackle it?

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.