r/GoogleAppsScript • u/furofo • Jul 21 '21
Question Help with linking multiple HTML pages google app script
So i want to link a couple of pages together in google app script and ran across a few tutorials on how to do it. I know i know to pass a ?page=HTMLIwanttoOPen to the end of the deployed app url. And this actual works when I type it manually in the address bar. For instance i can put a ?page=my1 or ?page=my2 to open the right html file this way.
Here is the code I have so far
Code.gs
**
* Get the URL for the Google Apps Script running as a WebApp.
*/
function getScriptUrl() {
var url = ScriptApp.getService().getUrl();
return url;
}
/**
* Get "home page", or a requested page.
* Expects a 'page' parameter in querystring.
*
* @param {event} e Event passed to doGet, with querystring
* @returns {String/html} Html to be served
*/
function doGet(e) {
if (!e.parameter.page) {
// When no specific page requested, return "home page"
return HtmlService.createTemplateFromFile('my1').evaluate();
}
// else, use page parameter to pick an html file from the script
return HtmlService.createTemplateFromFile(e.parameter['page']).evaluate();
}my1.html
<!DOCTYPE html>
<html>
<head>
<script>
let scriptUrl = '';
function onSuccess(url) {
scriptUrl = url;
console.log("connection success")
let a = document.getElementById("myform");
a.setAttribute('action', url + "?page=my2.html");
}
google.script.run.withSuccessHandler(onSuccess).getScriptUrl();
window.onload = function() {
}
</script>
</head>
<body>
<h1>Source = my1.html</h1>
<form id="myform" method="get" target = "_blank"></form>
<a onclick="document.getElementById('myform').submit();">Submit to action</a>
</body>
</html>My2.html
<html>
<body>
<h1>Hello There</h1>
</body>
</html>When i run this code, the form/ a element ticket is the right URL, again I can even copy and past and use it in another tab to open the right html file, but yet when i click on it all it does is redirect me to same page without any url parameters passed as get requests.
Any help with this would be appreciated as I feel like i am missing something obvious on this but have been stuck for a while and was wondering if this some kind of security feature google has to prevent you from posting get requests from the script?
UPDATE!!!!!!! (I FOUND SOLUTION)
Sorry if this turns into a bit of long post, but for anyone curious i was able to get it work by using this code.
function doGet(e) {
Logger.log( Utilities.jsonStringify(e) );
if (!e.parameter.page) {
// When no specific page requested, return "home page"
return HtmlService.createTemplateFromFile('my1').evaluate();
}
// else, use page parameter to pick an html file from the script
return HtmlService.createTemplateFromFile(e.parameter['page']).evaluate();
}
/**
* Get the URL for the Google Apps Script running as a WebApp.
*/
function getScriptUrl() {
var url = ScriptApp.getService().getUrl();
return url;
}my1.html
<!DOCTYPE html>
<html>
<head>
<base target="_top">
</head>
<body>
<h1>Page 1</h1>
<?var url = getScriptUrl();?><a href='<?=url?>?page=my2'>Link to page2</a>
</body>
</html>my2.html
!DOCTYPE html>
<html>
<head>
<base target="_top">
</head>
<body>
<h1>Page 2</h1>
<?var url = getScriptUrl();?><a href='<?=url?>?page=my1'>Link to page1</a>
</body>
</html>I found out a few interesting with this. First is the use of google scriplets link here https://developers.google.com/apps-script/guides/html/templates. These are the <? ?> which allow you to run JavaScript inline kind of like php but also runs it before the page is served. Honestly I think this code may work without this but not sure.
The second thing is in the header of html file you have to put <base target="_top"> tag so it will actually update address bar. Apparently from what I have read this is based on older coding methodology when coding was done in frames, (and google runs in a frame environment based on other things I have read) since technically <a> elements and <form> elements I believe change the frame they are in unless set othersiwe, which does not necessarily hold the web page url at the top unless I am mistaken. But the gist of it is I got to work using scriplets and base target!
2
u/LateDay Jul 22 '21
Whenever you submit the form in your HTML you are not passing any value I believe. Try adding an input with type="hidden" and name="page". Without the name attribute the form submits nothing.
1
u/LateDay Jul 22 '21
The form submits first and then runs your code. So the submission is done without the url parameter. I believe. You are better off with the hidden input.
1
u/furofo Jul 22 '21
<!DOCTYPE html>
<html>
<head>
<base target="_top">
<script>
let scriptUrl = '';
function onSuccess(url) {
scriptUrl = url;
console.log("connection success")
let a = document.getElementById("hiddenInput");
a.setAttribute('name', "?page=my2.html");
}
google.script.run.withSuccessHandler(onSuccess).getScriptUrl();
window.onload = function() {
}
</script>
</head>
<body>
<h1>Source = my1.html</h1>
<form id="myform" method="get" target = "_blank"></form>
<input type = "hidden" id = "hiddenInput">
<a onclick="document.getElementById('myform').submit();">Submit to action</a>
</body>
</html>
II tried this if this what you meant, but doesn't like like it worked i did however find a solution and i will update this question with it for those who are curious1
u/LateDay Jul 22 '21
The input had to be inside the form element and is missing the name attribute. But I am glad you got it working.
1
u/furofo Jul 22 '21
oh, okay I am curious about that may try again later by putting it in because i am curious, it was adding the name attribute using the onsuccess call back function of google app scripts if that makes sense, but honestly now that I think about there is no real need to set it that way since I already know what it needs to be lol, but I will let you know if that works when I try it
2
u/LateDay Jul 22 '21
Right. You can do it directly. Now, read up on forms and post/get methods. In theory, your input must have a name which is not the value. So in your example you need these attributes:
name="page" value="my2.html"
That way the URL has a new added parameter of page=my2.html which the doGet function will read.
Edit: saw your updated post. I was gonna suggest completely bypassing the form and just have a hyperlink directing your new page.
1
u/furofo Jul 22 '21
Yeah that makes perfect sense actually i had to set action attribute to but this code finally worked thanks!
<form action = '<?=getScriptUrl()?>'id="myform" method="get" target = "_blank">
<input type = "hidden" id = "hiddenInput" name = "page" value = "my2">
<input type = "submit">
</form>
1
u/rjohn0612 May 03 '23
I’m having trouble getting this to work. This thread is pretty old so is there any chance that an easier solution to this issue exist? I have a list of anchor tags and I want them each to take users to different pages when clicked. Can someone please help me with this or point me in the right direction?
2
u/HomeBrewDude Jul 21 '21
This looks interesting! Let us know if you get it working.
I'm not sure if this is the piece you're missing but I found this in the docs:
https://developers.google.com/apps-script/guides/html/reference/url
It looks like the `getScriptUrl()` runs server-side and can't see the client's url parameters, whereas, this `google.script.url.getLocation()` is client-side, so maybe that's the key to accessing the parameters.