Apps Script Sidebar Reference Error When Using Include - google-apps-script

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.

Related

Google.script.run isn't doing anything

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).

How can I add a Title of the webApp build with Google Appscript

I have a project wherein I have created a file named javascript.html, page.html , stylesheet.html and code.gs . I want to print out the title of my web app. Passing the title in page.html in <head> tag. However the same is not working. Is there any other way to do the same in Google appscript.
Below you can see there are three html tags. However I have defined only one in my page.html in appscript code.
Below is the code :
Other than that I haven't added any html tag in my code.
Kindly let me know how to do so?
If you are running your Web Apps with doGet() function, how about using setTitle? The sample script is as follows.
Sample script:
function doGet() {
return HtmlService.createHtmlOutputFromFile("index").setTitle("sample title");
}
By setTitle("sample title"), when the source of Web Apps is seen, you can see <title>sample title</title> in the HTML header.
Reference:
setTitle(title)

Google sheets: Class google.script.run not working

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.

Can't run or embed google Web App

I'm just learning google script and want to start by making a simple hello world. I have done google's tutorial and when I click "test this code" on the publish web app popup, the code runs and I get my basic hello world result. Great. But When I paste the provided URL into the browser or embed that same URL into google sites, I just get a blank page.
How do I run an web app? Am I missing something?
code.gs:
function doGet() {
var html= HtmlService
.createTemplateFromFile('Index');
html.name = 'David';
return html.evaluate();
}
index.html:
<!DOCTYPE html>
<html>
<head>
<base target="_top">
</head>
<body>
<b>Hi <?=name?>!</b>
</body>
</html>
Coming from a basic PHP background, I'm used to just going to the URL of the .php file and bang, away it goes... I'm so confused.
https://stackoverflow.com/a/40843413/6288442
Google had just recently enabled this feature. It has been under a
'feature request' status for quite a long time. Link here
You can now explicitly define X-Frame-Options.
To allow embedding under another domain, the option should be
HtmlService.XFrameOptionsMode.ALLOWALL
Google documentation on the subject:
https://developers.google.com/apps-script/reference/html/html-output#setXFrameOptionsMode(XFrameOptionsMode)
Example:
function doGet() {
return HtmlService.createTemplateFromFile('form.html')
.evaluate() // evaluate MUST come before setting the Sandbox mode
.setXFrameOptionsMode(HtmlService.XFrameOptionsMode.ALLOWALL); }
Hope this helps!
I think this should work
<!DOCTYPE html>
<html>
<head>
<base target="_top">
</head>
<body>
<b>Hi <?!=name?></b>
</body>
</html>
https://developers.google.com/apps-script/guides/html/templates#force-printing_scriptlets
Name of file should also be exactly same
I think Index and index won't work

Google Apps Script Web App: Can't separate out css and js code

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();