Write to Google Sites ListItem with GAS

332 Views Asked by At

I've been futzing around for a couple of hours trying to get either

addListItem(values)

or

setValueByIndex(index, value)

to work in my google apps script.

I have figured out that I need the page object to be defined. Google application is listed like the following

var site = SiteApp.getSite("domain","site name")
var page = site.getChildByName("pagename")

The first problem occurs here. My page keeps coming back Null. So I found a work around. I am using....

var site = SiteApp.getSite("domain","site name")
var matches = site.search("myName");

for(var i in matches) {
    Logger.log(matches[i].getName());
}
var listpage = matches[0];

It's a little messy but ,hey, I get my page object.

Now I am trying to add items to the list page and I am coming up with more problems.

listpage.addListItem([ "John", "Smith", "123 Main St"]);

taken straight from google example Here!

The error I am getting is "Invalid argument: values (line xxx, file "Functions")"

The goal is to pull values from one list ( easy and done ) and write it to another list.

Total code *** edited to show listpage confirmation

var site = SiteApp.getSite("domain","site name")
var page = site.getChildByName("pagename")
var isListPage = false

for(var i in matches) {
    Logger.log(matches[i].getName());
}
var listpage = matches[0];

if (listpage.getPageType() == SitesApp.PageType.LIST_PAGE) {
    isListPage = true;
    listpage.addListItem([ "John", "Smith", "123 Main St"]);
    Logger.log(isListPage);
  }

Can someone help me understand where I am going wrong here?

1

There are 1 best solutions below

4
On

If site.getChildByName() not working, check that:

  1. You've got the correct name from the page path, usually lower case with hyphens for spaces. For example, this page is named "My List Page":

    https://sites.google.com/a/example.com/experiment/my-list-page
                                 Domain      Site        Page
    
  2. That the page is a child of site, not another page.

    If the page is a under another page, not at the top level of your site, then you need to use Page.getChildByName() instead.

    var section = site.getChildByName( 'daily-status' );
    var page = section.getChildByName( 'my-list-page' );
    

Invalid argument error

I've been able to recreate the Invalid argument: values... error by trying to add a list item that has a different number of values than the list contains. For example, here's a list page, with four columns:

screenshot

I got the error when using the example code you referenced:

var site = SitesApp.getSite("example.com", "experiment");

var page = site.getChildByName("my-list-page");

page.addListItem([ "John", "Smith", "123 Main St"]);

That listItem had just three values. I had success, shown in the screenshot, by changing it to this:

page.addListItem([ "John", "Smith", "123 Main St", ""]);

Since the number of columns in a list is variable, and can be changed by editors of the page who may not even be aware of scripts being used to feed in data, hard-coding listItem values is unreliable.

addListItemToPage()

Here's a helper function to take care of the dirty work.

  addListItemToPage( "my-list-page", ["John", "Smith", "123 Main St"] );

Note that it assumes the page is at the top level - adapt as necessary.

// These globals should be customized for your site
var domain = 'example.com';
var sitename = 'experiment';

/**
 * Add a listItem to the given page. The listItem is truncated or padded as necessary
 * to match the list size on the page. Expects global variables `domain` and `sitename`.
 *
 * @param {String}   pageName      Name of the list page, assumed to be at top level
 * @param {String[]} listItem      Array of strings to be added to list
 */
function addListItemToPage(pageName, listItem) {
  // Get site from sitename & domain if applicable
  var site = (domain === '') 
           ? SitesApp.getSite(sitename)          // consumer
           : SitesApp.getSite(domain, sitename); // hosted apps
  var page = site.getChildByName(pageName);
  if (!site || !page) {
    throw new Error("Page not found: " + domain + "/" + sitename + "/" + pageName);
  }

  if (page.getPageType() == SitesApp.PageType.LIST_PAGE) {
    var numColumns = page.getColumns().length;
    // Ensure listItem matches size of list
    // Uses pad from http://www.jslab.dk/library/Array.pad
    var paddedItem = listItem.pad(numColumns, "").slice(0, numColumns);
    page.addListItem(paddedItem);
  } else {
    throw new Error("Not a list page: " + domain + "/" + sitename + "/" + pageName);
  }
}