2

I have 2 html documents inside my google apps script project (showHtml1, showHtml2). I serve the first one by

function doGet() {
    return HtmlService.createTemplateFromFile('showHtml1').evaluate();
}

and inside the first html i want to include the href to the second html (showHtml2). Is that possible?

Giannis Spiliopoulos
  • 2,628
  • 18
  • 27

2 Answers2

7

This sample should show you the pattern. You can use Html files wherever I used HtmlOuput objects. I just wanted to keep it simple.

function doGet(requestInfo) {
  var url = ScriptApp.getService().getUrl();
  if (requestInfo.parameter && requestInfo.parameter['page'] == '2') {
    return HtmlService.createHtmlOutput(
      "This is Page 2. <a href='" + url + "?page=1'>Page 1</a>");
  }
  return HtmlService.createHtmlOutput(
      "This is Page 1. <a href='" + url + "?page=2'>Page 2</a>");
}

Bear in mind when working with this that the URL from ScriptApp will be the deployed url, not the dev mode url, so if you are experimenting you might want to replace the "/exec" at the end with "/dev".

Corey G
  • 7,754
  • 1
  • 28
  • 28
  • You are a google apps script GOD! Jokes aside i would like to know if you discovered this just from the documentation provided. – Giannis Spiliopoulos Jul 02 '12 at 18:43
  • I am the engineer who wrote most of this, so unfortunately I can't claim this is from the docs. But I hope you are enjoying the new docs by my awesome teammates - I do think this was _possible_ for someone to figure out from the docs, albeit not as easily as it would have been for me. – Corey G Jul 02 '12 at 18:59
  • Say a thanks to your awesome teammates for all these new features. – Giannis Spiliopoulos Jul 02 '12 at 19:12
  • @CoreyG Just wanted to confirm: Does replacing the "/exec" with "/dev" switches from Deployed version to Dev version? I was trying, but it still shows me the Deployed version. Also, when I replaced "/dev" with "/exec" in Dev URL, it still showed me Developer Version – Waqar Ahmad Aug 05 '12 at 09:42
  • Yeah, that doesn't work... you need to change the full URL :) – Corey G Nov 19 '12 at 21:28
0

Yes you can follow the above tip but build the template from your html file, something as this:

function doGet() {
  var template;
  if(parameter.page='html01') {
    template= HtmlService.createTemplateFromFile('fileHtml01');
     template.page='html01';
  } else {
     template= HtmlService.createTemplateFromFile('fileHtml02');
     template.page='html02';
  }
  return template.evaluate();
}

and according the paramater show one url or the other one.

Daniel
  • 10,864
  • 22
  • 84
  • 115
Walter
  • 51
  • 3