I am just beginning with App Script and am unable to run a function using google.script.run. Ive included code below its pretty much copied and pasted from the App script sight so not sure whats wrong. Also my console and log aren't returning anything. Any help would be hugely appreciated.
Code.gs
function doGet(e) {
return HtmlService.createHtmlOutputFromFile('index');
Logger.log(e);
}
function doSomething() {
Logger.log('I was called!');
console.log('I was called!');
}
Index.HTML
<!DOCTYPE html>
<html>
<head>
<base target="_top">
<script>
google.script.run.doSomething();
</script>
</head>
<body>
</body>
</html>
Two things:
Logger.log(e); will never log anything if it is positioned after a return statement. return makes a function return a value / content and finish after this - without running any code located after the return statement.
If function doSomething() does not run and return anything - most likely it is because you tried to run doGet(e) manually from the script editor. This is not how it is intended.
You should deploy your code as a WebApp and execute it by opening the WebApp URL in a browser window.
To see if your functions ran correctly and logged something, go on View -> Executions
Be careful with case sensitivity (index vs Index).
Related
My problem is simple. All the possible solutions I searched for online did not address my question.
Google's developer website for Class google.script.run (https://developers.google.com/apps-script/guides/html/reference/run#withSuccessHandler) showcased the method myFunction(...) (any server-side function).
I have copied their exact code and html code and deduced that the function doSomething() does not execute. Nothing gets logged.
I intend to use this to execute an HTML file so that I could play a sound file. I could do this so far with a sidebar popping up from the side, as discussed in this thread: Google Script: Play Sound when a specific cell change the Value.
However, this code provided by Google does not work. Why?
function doGet() {
return HtmlService.createHtmlOutputFromFile('Index');
}
function doSomething() {
Logger.log('I was called!');
}
<!DOCTYPE html>
<html>
<head>
<base target="_top">
<script>
google.script.run.doSomething();
</script>
</head>
<body>
</body>
</html>
By using google.script.run you are calling a server-side Apps Script function.
https://developers.google.com/apps-script/guides/html/reference/run
Please double-check that you follow the following steps to do it correctly:
Please make sure that you put the html part of the code in a separate HTML file (which you create through File->New->HTML file) with the name corresponding to the one you are calling in HtmlService.createHtmlOutputFromFile() - in your case Index.html
Select “doGet” as the function to be run.
Deploy the script as a web app - this is the requirement for using Apps Script HTML service. Please find the instructions here: https://developers.google.com/apps-script/guides/web
Make sure that every time after you implement changes in your code, you deploy the script as a NEW project version. This is necessary to update the changes.
Open the current web app URL you obtain after updating your version, to open your html output.
In your case only an empty HTML file will be opened, to test functionality - insert some text in your HTML body, to test the correct functionality. The latter can be confirmed by viewing the Logs after running the code.
I created a Google Apps Script add-on to display a sidebar on click:
function displayPage_() {
getScriptProperties_()
var htmlTemplate = HtmlService.createTemplateFromFile('PAGE');
var html = htmlTemplate.evaluate()
.setTitle('Connector');
SpreadsheetApp.getUi().showSidebar(html);
}
This successfully loads the file PAGE.html as long as it is simple html. However, when I try to add include like this:
<!DOCTYPE html>
<html>
<head>
<base target="_top">
<?!= include("STYLESHEET"); ?>
</head>
<body>
</body>
</html>
It throws this error:
[17-02-09 08:55:50:239 MST] Execution failed: ReferenceError: "include" is not defined.
It doesn't matter what is in the include it always fails.
I have done this before and it worked. As far as I can tell, I have this set up just like the other project, but it doesn't work in this project. I assume that I forgot to enable something, but don't know how to tell what is missing because the transcript is vague.
What am I missing? Or, doing wrong?
I got crazy too...
Found it in an example provided in Google Script Reference, what I didn't know was that include() is a user-defined function. Paste this code and it will work:
function include(File) {
return HtmlService.createHtmlOutputFromFile(File).getContent();
};
Let me know if I understood properly.
I can't get the simple example copied from the google dev site to work.
The error is: Cannot call FormApp.getUi() from this context
I am NOT attempting to deploy this a web form/app but I am simple trying to run this via the editor!!!
Not sure what I'm doing wrong?!?!?!
The following has been pasted into index.html:
<!DOCTYPE html>
<html>
<head>
<base target="_top">
</head>
<body>
Hello, World!
</body>
</html>
The following is pasted into Code.gs:
// Use this code for Google Docs, Forms, or new Sheets.
function onOpen() {
FormApp.getUi()
.createMenu('Dialog')
.addItem('Open', 'openDialog')
.addToUi();
}
function openDialog() {
var html = HtmlService.createHtmlOutputFromFile('Index');
FormApp.getUi()
.showModalDialog(html, 'Dialog title');
}
Answered my own question.
The script must be bound to a Form (which mine wasn't!)
So now I need to figure out how to create a Dialog for an Unbound script.
I don't think this is possible!
If I deploy it as a Web App then obviously I have no access to the user UI.
So how would I take the output from ContentService.createTextOutput() and cause a download of a file to occur?
I'm trying to separate my javascript and stylesheet from the main HTML file in my Google Apps Spreadsheet script that is published as a Web App. I have seen this answer to the problem, but I cannot get this approach to work for me.
When I have my stylesheet and javascript in the main HTML file, it works fine. When I try to separate them exactly as the answer recommends, the stylesheet and javascript code is not processed and instead the line that calls 'getContent()' is displayed in my browser. It looks like getContent() is not being executed at all.
I've tried moving my code away from the Spreadsheet to a Standalone Web App but still have the same problem. Any ideas why it's not working for me? Thank you!
A bit from my Code.gs:
function doGet() {
var output = HtmlService.createHtmlOutputFromFile('index');
output.setSandboxMode(HtmlService.SandboxMode.IFRAME);
output.setTitle('Dashboard Tool');
return output;
}
function getContent(filename) {
Logger.log('getContent()');
return HtmlService.createTemplateFromFile(filename).getRawContent();
}
index.html:
<?!= getContent("stylesheet") ?>
<div class='header'>Dashboard</div>
<div id=content>
Content Here
</div>
<?!= getContent("javascript") ?>
'stylesheet.html' code is surrounded by the style tag and 'javascript.html' code is surrounded by the script tag.
You forgot evaluate() in the createHtmlOutputFromFile(), also you should use the createTemplateFromFile, as such.
var output = HtmlService.createTemplateFromFile('index').evaluate();
As #brian-p pointed out, you need the 'Template' instead of 'Output', for the evaluate occur on the scriplets <?!= ?>.
In this line of code:
return HtmlService.createTemplateFromFile(filename).getRawContent();
createTemplateFromFile() is being used. That method is for creating the original template, but the getContent() function is not for that purpose. Use:
return HtmlService.createHtmlOutputFromFile(filename).getContent();
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);
}