Search code examples
google-apps-scriptgoogle-sites

Updating html of a Google Site's page with multiple blocks using Google Apps Script


I've created a page using "Web Page" template on my Google Sites, and I want to update the page using Google Apps Script. My code looks like this:

var template = HtmlService.createTemplateFromFile('mytemplate'); // meaning 'mytemplate.html'
var content = template.evaluate().getContent();

var site = SitesApp.getSiteByUrl('https://sites.google.com/site/example/');
var page = site.getChildByName('home');
page.setHtmlContent(content);

This works pretty well if the page's layout is "One column (simple)". Now my question is, if I choose one of the other layouts, how can I update each individual blocks using setHtmlContent()? Actually, I tried using "Two column (simple)" layout which has 2 blocks. setHtmlContent() changed the left column's html code and cleared the other column.

EDIT: What I actually wanted to do is to create a page with a gadget and update the page using Google Apps Script. According to this issue tracker https://code.google.com/p/google-apps-script-issues/issues/detail?id=572, it looks like we may encounter some problems when updating such page with setHtmlContent(). So I came up with an idea to create a multiple-column page, place a gadget in a column, and only update the HTML code of the other column with the Script to avoid such issue.


Solution

  • You can also create a Stand Alone Web App with Apps Script, then put an Apps Script Gadget into a Sites page. You can have multiple Apps Script Gadgets. I don't know what is triggering the change of the HTML. For all practical purposes, a Stand Alone Apps Script HTML Service App, is basically the same thing as a web site. You don't have a nice, understandable URL, but that won't show up in the Apps Script Gadget. Google will display a message: "This content was not created by Google"

    I don't think you'll be able to inject HTML into multiple columns. You could create HTML that has multiple columns in it, and then add that HTML to the one column. So, you would be creating your own columns in HTML.