Search code examples
google-apps-scriptgoogle-sites

How can you add a link to a google site via a Google Apps Script?


I have been scripting a few ways to make my Google site easier to manage by dynamically creating pages. I want to be able to create a directory with links to those pages.

So i was thinking something like...

var page = site.getChildByName("NEW URL");
var link = [page.getUrl()];
page = site.getChildByName("URL TO DIRECTORY").addListItem(link);

...using google apps scripts. Haven't tested this too thoroughly as of yet but it haven't been able to get the links to work in the list. I was hoping to just keep the page template as a normal webpage.

If I went that route would editing the html be the only option? Hopefully there is an easier way.

Any ideas would be great. Thanks!


Solution

  • here is a small test that shows a list of all the pages in your site

    function doGet() {
      var app=UiApp.createApplication().setTitle("Directory of this site")
      var mainPanel = app.createAbsolutePanel().setStyleAttributes({background:'beige',padding:'20px'});
      var scroll = app.createScrollPanel().setPixelSize(450,300);
      var site = SitesApp.getSiteByUrl('https://sites.google.com/site/appsscriptexperiments/')
      var pages = site.getAllDescendants()
      var grid = app.createGrid(pages.length,2).setWidth(400)
        for(n=0;n<pages.length;++n){
        grid.setText(n, 0, pages[n].getName()).setWidget(n, 1, app.createAnchor('open', pages[n].getUrl()))
        }
        app.add(mainPanel.add(scroll.add(grid)))
        return app
    }
    

    I suppose you know ho to use it in your sitepage but just for info for anyone else : you have to go to manage site > script apps > create a new script > save a version > deploy with appropriate authorizations and finally return to your page and insert it as a gadget (better with a border and a size of 450x300, personal pov ^^) . You can also use it as a standalone webapp using its deploy url.

    enter image description here