Annuaire Vin


Pour découvrir les vins suisses et apprécier les vins du monde

Mes Favoris

 

Les Etiquettes du Vin


Wine's Links


Chasselas


Fendant - Vin du Valais


Obrist Artisans du vin


Maurice Gay - la haute couture des vins du Valais


Henri Badoux - Les Murailles


Bolle - Les spécialistes en vin de Domaines



Using Google Apps Script with Google Sites - Updating a Page


Google Apps Script is now available within Google Sites. We can launch the script using a link or using a time trigger.



Updating a Page


As example, we can update a Google Sites Page with a feed coming from
Google Reader. It is a better way then using a Feed Reader. We can open the link in a new window, and using some html, it's easy to create an unordered list which is directly incorporated into the Page.



The Updated Page



Feeds From Google Reader
is a Google Sites Page which is fully updated every 24 hours with a Google Apps Script.


Reading the html of the Page



We get the content of the page, using the following snipset :




// get Sites and Page content (html)

 ...

var site = SitesApp.getSiteByUrl("https://sites.google.com/site/annuairevin/");
var name = "feeds-google-reader";
var page = site.getChildByName(name);
if (page == null) { return; }
else { process; }
 ...
 


Parsing the Feed


We get an XmlElement and we create an unordered list with each entry of the feed.




...
var rep=UrlFetchApp.fetch(url);                           // get HTTPRESPONSE from the url of the feed
var doc=Xml.parse(rep.getContentText(),true);     // get XmlDocument
var root=doc.getElement();                                // feed element

// create a table (entries) with all the entry of the feed

var entries=root.getElements("entry");

// create the ul list

var ul="<br/><ul>";

for (var i=0; i<entries.length; i++){
    var e = entries[i];
    var title= e.getElement("title").getText();
    var link = e.getElement("link").getAttribute("href").getValue();
    ul += "<li><a href='" + link + "' target='_blank' title='" + title + "'>" + title + "</a>;
    ul += "<br/><br/>" + "</li>\n";
 }

// end the ul li list

ul += "</ul><br/>";
return ul;
...



Inserting some html within an existing page


As we have now the unordered list, we have to insert the html fragment within the original page. The tips is to create, within the targeted page, a named div tag which define the html bloc to be updated.


<div name="somename">Any content</div>



To replace Any content we use the following function :



function InsertToPage(html,s,target) {
//
//-- in an html string, assume we have a div name='target'
//-- replace this div content with s
//

var i = html.indexOf("name='" + target + "'",0);
i=html.indexOf('>',i);
var j = html.indexOf('</div>',i+1);
return html.substring(0,i+1) + s + html.substring(j);
}
 





More informations


Google Apps Script Guide






 

Find a Wine with Google CSE