0

I get error that "cannot set property xx of undefined to xx" when try to save a value to a multidimensional array. I had this code working a few weeks ago and now it doesn't. I am not sure what changed but I believe the error is related t how the array is defined.

function iterateSheets() 
{
  var ss=SpreadsheetApp.getActive();
  var folder=DriveApp.getFolderById('1ezdV7AHBNyq6aZdqKdvhlOhKMmLPhQoW');//replace id with actual id of folder
  var files=folder.getFilesByType(MimeType.GOOGLE_SHEETS);
  var consol_sheet = SpreadsheetApp.openById('1FyuizUjU8W8idMonEp0qFthvTDjfwmnboZivAAozDXU').getActiveSheet();
  consol_sheet.clear()
  var x=0;
  var z=0;
  var final_values = [[]];

while(files.hasNext())
 {
    var file=files.next();
    var ts=SpreadsheetApp.openById(file.getId());
    var allShts=ts.getSheets();


       for(var i=0;i<1;i++)   
       {  
        // if (allShts[i] == "Aug"  || allShts[i] = "Sept")
         //{   
            var consol_values = allShts[i].getRange(8,1,allShts[i].getLastRow(),10).getValues(); 
            var headers = allShts[i].getRange(4,6,1,4).getValues();  
            var position= allShts[i].getRange("B1").getValue();
            var period = allShts[i].getRange("B2").getValue();
            var email = allShts[i].getRange("B3").getValue();    

             for (var z = 0;z<3;z++)//
             {   

               for (var y= 0;y<allShts[i].getLastRow();y++)               
               {          
                 if (consol_values[y][0] != "" && consol_values[y][z+5] !="")     


                 final_values[x] = [];       

                 final_values[x][0]  = consol_values[y][0]  //AI pack 
                 final_values[x][1]  = '1111'
                 final_values[x][2]  = consol_values[y][2]  ; //measure
                 final_values[x][3]  = email ;
                 final_values[x][4]  = position ;     
                 final_values[x][5]  = headers[0][z];//location
                 final_values[x][6]  = 1;
                 final_values[x][7]  = period;
                 final_values[x][8]  = consol_values[y][3]; //price
                 final_values[x][9]  = consol_values[y][z+5]; //fcst

                 if (consol_values[y][z+5] != "")
                 { 
                    final_values[x][10] =(consol_values[y][25]); //fcst value             
                 }                          

                 x = Number(x)+1  //row count for consolidation output

                }
              //}   

         }    
      }

 }


//consol_sheet.getRange(2,1,final_values.length,final_values[0].length).setValues(final_values);

}

Any help in what I am doing wrong is much appreciated.

4
  • 1
    Which line are you getting this error on? Commented Jul 16, 2019 at 14:29
  • final_values[x][0] = consol_values[y][0] Commented Jul 16, 2019 at 14:32
  • I think this var final_values = [[]]; should be this var final_values = []; Commented Jul 16, 2019 at 15:08
  • 1
    Instead of including the original code add a minimal reproducible example and the textual error message that you was able to reproduce. Include the values of the variables like x and y. Commented Jul 16, 2019 at 22:14

1 Answer 1

1

Edit: TheMaster has pointed out that you're already initialising your arrays with final_values[x] = []; and after reviewing my answer I agree that they are correct.

The problem is actually your conditional statement:

if (consol_values[y][0] != "" && consol_values[y][z+5] !="")

Apps script will continue to read a line until it sees a ;, and so as you haven't contained your if block within curly brackets {}, when final_values[x] = []; is being read it is reading the whole if block as:

if (consol_values[y][0] != "" && consol_values[y][z+5] !="") final_values[x] = []; 

This means that if consol_values[y][0] != "" && consol_values[y][z+5] !="" evaluates to False, the inner array will never be initialised and you'll get the cannot set property error on assigning

final_values[x][0]  = consol_values[y][0];
Sign up to request clarification or add additional context in comments.

4 Comments

Isn't that already present in his code? final_values[x] = [];
Yes, but JavaScript doesn't handle multi-dimensional arrays to be pushed dynamically so final_values[x][0] on its own will throw a can't set property error unless the inner array has been initialised first, which is what I'm doing with the conditional.
That's what I'm saying. Isn't the inner array already initialized in his code? His code already contains final_values[x] = [];
@TheMaster You're right! Thank you for pointing that out I overlooked that when testing out the multidimensional arrays. I've worked it out now though so I've edited my answer.

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.