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.
Code.gs
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!