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?
If
site.getChildByName()
not working, check that: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":
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.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:I got the error when using the example code you referenced:
That listItem had just three values. I had success, shown in the screenshot, by changing it to this:
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.
Note that it assumes the page is at the top level - adapt as necessary.