how to refresh a google site using google apps scripts ? - google-apps-script

My site fetches chart from spreadsheet but it is getting updated only when it is refreshed manually. Publishing on web is not working as expected. So Any method to refresh a site is much appreciated. Please do not include browser extensions or plugins or widgets, because of site audience.

After some hours struggling with this, i found a solution that worked for me.
Place this code in Templated HTML file. Then call function reloadSite() in Javascript to reload current site.
<script>
function reloadSite() {
window.top.location.href = '<?!= SitesApp.getActivePage().getUrl(); ?>';
}
</script>

Should be able to end the onSubmit() with the function that calls the Ui. You state it is doGet(), like this :
function onSubmit(e){
// Your code ...
doGet();
}

Related

Get the full browser URL in Apps Script

I'm looking to get the full URL of the current browser window in Apps Scripts while displaying the web app via HTMLService
When I try this, it doesn't work because the Apps Scripts is sandboxed and I get another server url of some sort
window.location.href
When I try this, I can only fetch the hash or parameters, not the full url
google.script.url.getLocation(function(location) {
console.log(location.parameters);
console.log(location.hash);
});
source: https://developers.google.com/apps-script/guides/html/reference/url#methods
Purpose for the above:
I want a button that people click on in the published Web App to remove the browser toolbar. However, the browser toolbar is not removable in the current browser window. So I want to fetch the Web App URL and put it in window.open() as a new window with the current web app because the browser toolbar can be remove this way. But I can't seem to get the current URL.
Any ideas how I can achieve this?
Thanks in advance!
I don't know what you meant by developer mode either but I took a guess at this being the answer to the question. If not, let me know and I'll remove it.
On the clientside you can use Javascript:
<script>
function getUrl(){
google.script.run
.withSuccessHandler(function(url){
document.getElementById('mydiv').innerHTML='<p>' + url + '</p>';
})
.getScriptUrl()
</script>
<body>
<div id="mydiv"></div>
</body>
In Google Script:
function getScriptURL() {
return ScriptApp.getService().getUrl();
}
Jan 10, 2021: Tested the above code in a window.onload and it is returning the correct url with a business account, V8 runtime and the new editor.
I'd suggest this code for the current script URL:
function getScriptURL() {
// dev URI
'https://script.google.com/macros/d/' + ScriptApp.getScriptId() + '/edit';
}

SitesApp.getActivePage() always return landing page

I use the following function:
function doGet(e) {
Logger.log( SitesApp.getActivePage().getName())
}
It always returns the name of the home page
This seems to be an issue with Google currently. All Google Sites are facing this issue. Please submit a bug report, and cross your fingers it'll be solved soon.
Here is an existent bug report of this issue. Please +1 it to give it more attention.
There is an older discussion about this issue here Apps Script .getActivePage() only returning "home" page
which suggests the problem can be related to using setHtmlContent() when the embedded html gets modified. There has been a recent problem with htmlbox on Google Sites for which a fix is in the process of rolling out which may have some bearing on this issue
Hi I have the same issue. My script worked on Old Google Site, but it doesn't work anymore on a new Google Site.
And the issue in this code:
var pageUrl = SitesApp.getActivePage().getUrl();
My question is almost the same: How to get URL of the current page on the New Google Site with GAS?
google.script.url.getLocation
google.script.url.getLocation(function(location) {
console.log("locations:----------------------------------------");
console.log(location);
console.log(location.parameters);
console.log(location.hash);
console.log("----------------------------------------");
});
The code above gives the next output:

Redirecting from doPost() in Google Web App, HTML form with App script

Say I have an HTML file with various inputs. According to these inputs I would like to send an email and then display a thank you message or redirect to a thank you page.
I am loading the index.html file in the doGet() as below:
function doGet(){
var template = HtmlService.createTemplateFromFile('index');
template.action = ScriptApp.getService().getUrl();
return template.evaluate();
}
After adding the implementation which I require in doPost(e) function, deploying the code into a web app, filling in the form and submitting, everything seems to be working perfectly apart from the last bit. I want to show an output message or redirect to a thank you page, or something of the sort, but all I am seeing is a blank page, where there would have been the HTML form.
I have tried:
function doPost(e) {
//all logic
return ContentService.createTextOutput("Thank you");
}
And..
function doPost(e) {
//all logic
return ContentService.createTextOutput(JSON.stringify(e.parameter));
}
And..
function doPost(e) {
//all logic
var htmlBody = "<h1>Thank you.</h1>";
return HtmlService.createHtmlOutput(htmlBody);
}
And..
function doPost(e) {
//all logic
var template = HtmlService.createTemplateFromFile('Thanks');
return template.evaluate();
}
In all the listed cases the HTML form simply seems to disappear and I can only see a blank screen.
Any ideas?
In GAS, sending a POST request via web app is not as simple as it seems. Go ahead and try a little experiment: add the following JS code to the HTML page served by the doGet() function just before the closing <body> tag:
...
<script>
console.log(window.location);
</script>
</body>
When inspecting the output in the console, you'll likely get something like:
The issue is that the web app URL ending with '/exec' doesn't actually link to self. Ratner, the googleusercontent.com link is the URL you'll be redirected to when opening links and forms. However, in order to get the form submit to work, the POST request should be sent to the "published" URL of your web app (the one ending with '/exec').
You must therefore override the defaults by implementing your own routing. There are many ways to achieve this result - here's the easiest one:
<form method="post" action="<?!= ScriptApp.getService().getUrl() ?>">
<input type="submit" value="submit">
</form>
If the <?!= ?> notation looks unfamiliar to you, please check the section on scriptlets and HTML templates in GAS https://developers.google.com/apps-script/guides/html/templates
Basically, what happens is that the web app URL is force-printed to the template before raw HTML is sent to your browser for rendering. This makes sure you'll hit the correct URL when 'submit' event occurs.

Apps Script HTMLService not working and it was working perfectly

I have created a lot of apps using Apps Script and HTMLService for my work and all the apps have been working perfectly(reports,submission tools,trackers,etc) however, since Feb 25th,2014 they are not working, not even a single one of them.
When I click the button of a html form in my app it should load another page but now I click on the link and nothing happen, I really need your help since I dont know why they are not working when they used to work without any inconvenient.
Below you can find a code that worked before and now is not working,
Code.gs
function doGet() {
var template= HtmlService.createTemplateFromFile('test');
template.action = ScriptApp.getService().getUrl();
return template.evaluate()
}
function doPost(e){
var template = HtmlService.createTemplateFromFile('test.html');
template.action = ScriptApp.getService().getUrl();
return template.evaluate();
}
test.html
<!DOCTYPE html>
<html>
<body>
Exception Tracker Report <br/><br/>
<form >
<input name = "test" value="test">
Test<input type="submit" value="Load" />
</form>
</body>
</html>
I really request your help in this matter since all the apps that I have created using HTMLService are not working (20 or more apps).
Once again, the code above should work without any problem but I dont know if there have been changes in the HTMLService Google class or anything regarding this.
Thanks for your help
Google made a change yesterday on htmlservice, by defect if not mentioned the sandbox mode was emulated. They changed it to native.
As it's not mentioned in your script, you where running in emulated mode and since yesterday ... it changed to native mode.
You need to specify in your script that you want to use the emulated mode.
this way:
function doGet() {
var template= HtmlService.createTemplateFromFile('test');
template.action = ScriptApp.getService().getUrl();
return template.evaluate().setSandboxMode(HtmlService.SandboxMode.EMULATED);
}

Automatically Redirecting to a Page

Inside a button click handler, I'm creating a new web page like so:
var page = SitesApp.getPageByUrl(url).createPageFromTemplate(title, name, template);
and I want to redirect the user automatically to that page.
I wasn't able to find much information, can this be done?
Corey G's answer worked for me, but the problem is that the web page that I was redirecting was embedded into an iframe in the response of the GAS, so the real URL in the web browser was the script's one.
This is what actually worked for me:
function doGet() {
return HtmlService.createHtmlOutput(
"<script>window.top.location.href='https://example.com';</script>"
);
}
This similar Q/A helped me to solve it.
Hope it helps.
This cannot be done in UiApp but it's doable in HtmlService:
function doGet() {
return HtmlService.createHtmlOutput(
"<form action='http://www.google.com' method='get' id='foo'></form>" +
"<script>document.getElementById('foo').submit();</script>");
}
This should be easier; please file a feature request in the issue tracker and we will see how we can make this more pleasant.
(Edit: To be clear, there's no way to do this from a UiApp callback; your entire app would have to be using HtmlService for this to work.)