Search code examples
javascriptarraysgoogle-apps-scriptgoogle-sites

Programmatically declaring variables based on indexed array ( working with google site list page )


imagine I have an excel like grid that consists of two parts.

A column and a list.

The column is the columns and the list is the entire collection of the all of the rows. I can get my columns list with a .length and a .length on the list gives me the number of rows.

So I have my x and my y now.

I want to create an object out of this that would work something like this:

var = ObjList {

    row0 = [],
    row1 = [],
    row2 = [],
    column = [],
};

The issue is that the list can be modified in length. The only way i see to approach this is to programmatically create variables. Like so ( psuedo code )

  //Variables
  var itemName

  //Get Column Names
  for (var j in columns) {                                 //columns is an object/array
      var cName =columns[j].getName();                     //I have to get the name otherwise cName just returns as ListItem
      columnList.push(cName);                              //Push cName to my own array so I have an array of strings
  }
  for (var i in listItems) {                              //Again working with an object array
      item  = listItems[i]                                //Creating an easier reference variable
      for (var j = 0; j < columnList.length - 1;j++){     //now I have to use the length of the Column array to find out how wide our list is
          itemName = item.getValueByName(columnList[j]);  //turning the listitem into a string
          row(i).push(itemName);                          //the is the programmatic variable. 
      }
  }

The problem is I'm not sure where I would declare the variable. Is there some other way for me to loop through the List array and match each index of the listItems array to the index of the column which I am doing here with the getValueByName(columnList[j]).

******************************Edit********************************

An example was requested and I can see how this would be confusing. This is dealing with google sites specifically.

I added comments to the above code as well.

We have a grid.This grid is actually two parts. The column is an object which is why I am creating another array and putting the names into the array. The list is another object. ( LI = lineItem )

 |Col1|Col2|Col3|Col4|<~~~ Column Object 
 |LI |LI |LI |LI |<~~~~~~~~~~~~~~~~~~~|
 |LI |LI |LI |LI |<~~~~~~~~~~~~~~~~~~~| List Object
 |LI |LI |LI |LI |<~~~~~~~~~~~~~~~~~~~|

WHat I want is to be able to iterate through a loop and break each row into its own array.

I have been able to do that successfully one at a time. But since I would like to have one array per row and the number of rows is dynamic ( because a user can add more rows ) I am confused as to how I would account for that.

I want the entire grid to exist as an object with row properties and column properties that are array. So row0 would be row0[LI1,LI2,LI3,LI4]

 Columns[]~~>|Col1|Col2|Col3|Col4|  ~~~~~~~|
 row0[]~~~~~>|LI1 |LI2 |LI3 |LI4 |  ~~~~~~~|
 row1[]~~~~~>|LI1 |LI2 |LI3 |LI4 |  ~~~~~~~|ObjList
 row2[]~~~~~>|LI1 |LI2 |LI3 |LI4 |  ~~~~~~~|

Again the width of the grid is

Column.length 

and the number of rows can be defined by

itemList.length 

So I guess the question is can I have an array of arrays?

So I could have

 //Variables
   var itemName;
   var masterArray = [];
   var midArray = [];
  //Get Column Names
  for (var j in columns) {                                
      var cName =columns[j].getName();                     
      columnList.push(cName);                              
  }
  for (var i in listItems) {                              
      item  = listItems[i]                                
      for (var j = 0; j < columnList.length - 1;j++){    
          itemName = item.getValueByName(columnList[j]);  
          midArray.push(itemName);
              if (midArray > columnLisst.length -1) {
                 masterArray.push(midArray);
                 midArray.length = 0;
              }                      
      }
  }

It feels clunky and wrong though.


Solution

  • Yes, you can have a 2 dimensional array in JavaScript. What you have now is an object with another level which is an array, so, in terms of dimensions, there really isn't any difference. Whether it's an object with arrays, or an object with other objects, or an array with other arrays; in all situations you have that second level.

    The only issue really is; do you want to be able to access values by name rather than by index? If you want to access values by name, then use an object. Have you considered having an object inside of an object?

    I don't see that you are programatically declaring variables. Maybe there is a misunderstanding of the terminology.

    In this situation, where there is a second level of data, it would be normal to have nested FOR loops, so even though it's more complex, I don't think it would be considered clunky or wrong.

    You'll need to run the code and look at the results to figure out what will work for you. You can use Logger.log("some text: " + aVariableName); statements to print information to the LOG, then VIEW the LOG. Also, view the EXECUTION TRANSCRIPT.

    You can also step through lines of code, one line at a time, line by line and view the results.

    If you have a specific problem that you can't figure out, provide the error message, and what line of code is producing the error message.