Google Sites Listitem

151 Views Asked by At

I am working with the google sites list item.

The classes are Here and Here

I have been able to iterate through the columns and put all of the column headers in to one array with the following code.

//Global
 var page = getPageByUrl(enter URL here)
 var name = page.getName();
 function getInfo() {
 var columns = page.getColumns();
   //Get Column Names
 for (var j in columns) {
    var cName =columns[j].getName();
    columnList.push(cName);
  }
}

Now I want to be able to get each row of the listitem and put it in its own array.

I can add the variable

function getInfo() {
 var columns = page.getColumns();
 var listItems = page.getListItems();//new variable
   //Get Column Names
 for (var j in columns) {
    var cName =columns[j].getName();
    columnList.push(cName);
  }
}

Now that I have the variable the output is [ListItem, ListItem, ListItem, ListItem] So I can use a .length and get a return of 4. So now I know I have 4 rows of data so based on my wants I need 4 arrays.

Small interjection here, Not a coder by trade but code as a precursor to wants becoming needs.

A buddy of mine who is a JS coder by trade showed me this code which does work. With the logger added by me.

for (var i in listItems) { 
  if (listItems.hasOwnProperty(i)) {
      item = listItems[i];
        for (var x = 0; x < columnList.length; x++) {
          attrib = item.getValueByName(columnList[x]);
          Logger.log("Logging value of get list page get value by name = " + columnList[x] + " " + attrib);
      }
     }
     }

Which brings the total code to

var name = page.getName();
var listItems = page.getListItems();
var listCount = listItems.length
var listList = [];
var columns = page.getColumns();
var name = columns[0].getName();
var item, attrib = 0;
var columnList = [];
Logger.log(listItems);
Logger.log(name + " was last updated " + page.getLastUpdated());
Logger.log(name + " was last edited " + page.getLastEdited());
var listCount = 0;

//Get Column Names
  for (var j in columns) {
      var cName =columns[j].getName();
      columnList.push(cName);
  }
  Logger.log(columnList);
   // Get index of Due Date   
  var dueDateValue = columnList.indexOf("Due Date");
  Logger.log("The index of due date is " + dueDateValue);

for (var i in listItems) { 
if (listItems.hasOwnProperty(i)) {
    item = listItems[i];
    for (var x = 0; x < columnList.length; x++) {
        attrib = item.getValueByName(columnList[x]);
        Logger.log("Logging value of get list page get value by name = " + columnList[x] + " " + attrib);
      }
     }
     }



}`

Forgive the above code as it has been a bit of a sketch pad trying to work this out. I am a bit behind on understanding what is happening here

for (var i in items) { // This is for each item in the items array
    if (items.hasOwnProperty(i)) {   

if items is an array, how can we use has own property? Doesn't that belong to an object? Does an array become an object?

My questions are two category fold.

Category # 1 What is happening with the hasOwnProperty? -Does the array become an object and thus can be passed to .hasOwnProperty value

Category # 2 Is this the only way to take the values from the listitem and populate an array - If it is, is there some way to delimit so I can pass each row into it's own array - If it isn't , why does it work with the hasOwnProperty and why doesn't it work without it in the example below

for (var i in listItems) {
    for (var y = 0; y < columnList.length; y++) {
     item = listItems[i];
     listList = item.getValueByName(columnList[x]);
     Logger.log("Logging my version of list naming " + listList);
      }

In which I get a "Invalid argument: name (line 41" response. Highlighting the

listList = item.getValueByName(columnList[x]);

Not looking for a handout but I am looking to understand the hasOwnPropertyValue further.

My current understanding is that hasOwnValue has to do with prototyping ( vague understanding ) which doesn't seem to be the case in this instance and it has to depend on a object which I described by confusion earlier.

To clarify my want: I would like to have each row of listitems in its own array so I can compare an index value and sort by date as my current column headers are

["Project", "Start Date" , "End Date"]

Any and all help is much appreciated for this JS beginner of 2 weeks.

1

There are 1 best solutions below

1
On BEST ANSWER

An array can be inside of an object as the value of a member:

 {"myFirstArray":"[one,two,blue]"}

The above object has one member, a name/value pair, where the value of the member is an array.

Here is a link to a website that explains JSON.

Link To JSON.org JSON explained by Mozilla There are websites that will test the validity of an object:

Link to JSONLint.com

An array has elements, and elements in an array can be other arrays. So, there can be arrays inside of arrays.

.hasOwnProperty returns either true or false.

Documentation hasOwnProperty

Interestingly, I can use the hasOwnProperty method in Apps Script on an array, without an error being produced:

function testHasProp() {
  var anArrayTest = [];
  anArrayTest = ['one', 'two', 'blue'];
  Logger.log(anArrayTest);

  var whatIsTheResult = anArrayTest.hasOwnProperty('one');
  Logger.log(whatIsTheResult);

  Logger.log(anArrayTest);
}

The result will always be false. Using the hasOwnProperty method on an array doesn't change the array to an object, and it's an incorrect way of using Javascript which is returning false.

You could put your list values an object instead of an array. An advantage to an object is being able to reference a value by it's property name regardless of where the property is indexed. With an array, you need to know what the index number is to retrieve a specific element.

Here is a post that deals with adding properties to an object in JavaScript:

StackOverflow Link

You can either use dot notation:

objName.newProperty = 'newvalue';

or brackets

objName["newProperty"] = 'newvalue';

To add a new name/value pair (property) to an object.