Here is my code for show a sidebar. When click at OK button, I will insert a new record to my sheet.
It works well when I disable V8 by Run->Disable new App Script .. V8.
When I enable V8, onClicked in Code.gs not fired anymore.
I have checked the V8 Runtime Overview but I didn't find anything.
Index.html
<!DOCTYPE html>
<html>
<head>
<base target="_top">
</head>
<body>
<button onclick="okClicked()"> OK </button>
<button onclick="close()">Close </button>
<script>
function okClicked(){
window.alert("A");
google.script.run.okClicked();
}
function close(){
google.script.host.close();
}
</script>
</body>
</html>
Code.gs
function showSidebar() {
var html = HtmlService.createHtmlOutputFromFile('Index')
.setTitle('My custom sidebar')
.setWidth(300);
SpreadsheetApp.getUi().showSidebar(html);
}
function okClicked(){
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Data");
sheet.appendRow(["A", "B"]);
}
Any help or suggestion would be great appreciated.
Someone posted an issue similar to createHtmlOutputFromFile and V8 on Google's Issue Tracker. You can check if this issue is adequate for your case and hit the Star to let them know you have the same issue.
If you believe that this issue does not correspond with yours, you can also create a new issue with your case there.
2021 Answer
FWIW, I found that Google Apps Script was erroring out like this when my partner and I were both logged into the same Google account.
We also found that this can happen when new features have been built into your Google Apps Script / Sheets backend code that require updated permissions that haven't been granted yet. It's a bit hacky, but I fixed this by:
Sharing the sheet with another account I own
Logging into that account, then trying to run the function
Agreeing to grant the permissions asked by Google Auth / Scopes in the dialog that follows
Related
I'm learning how to use Google Web App and am following this YouTube video Google Sheets Web App Example - Google Apps Script Web App Tutorial - Part 1 and I am really enjoying the series. It is very easy to follow along with the videos. But I am running into a little issue with the first video. For some reason when I try to do log the button click on the HTML page, nothing happens. I sure it's going to be something silly. But I have been at this now for a few hours and I can't figure out what is wrong.
Code.gs:
function doGet(e) {
Logger.log(e.parameter);
return HtmlService.createHtmlOutputFromFile("page");
}
function userClicked(name){
Logger.log(name + " & noah clicked it!");
}
page.html
<!DOCTYPE html>
<html>
<head>
<base target="_top">
</head>
<body>
<h1>Hello</h1>
<label>Name:</label><input type="text" id="username">
<button id="btn">Run It!</button>
<script>
document.getElementByID("btn").addEventListener("click",doStuff);
function doStuff(){
var uname document.getElementByID("username").value;
google.script.run.userClicked(uname);
}
</script>
</body>
</html>
Please tell me there is something silly that I am just not seeing.
I think that your script of HTML side is not correct. By this, the script is not run. So I think that Logger.log is not run. Please modify your script of HTML side as follows and test it again.
Modification points:
getElementByID is getElementById.
var uname document.getElementByID("username").value; is var uname = document.getElementById("username").value;.
Modified script:
From:
<script>
document.getElementByID("btn").addEventListener("click",doStuff);
function doStuff(){
var uname document.getElementByID("username").value;
google.script.run.userClicked(uname);
}
</script>
To:
<script>
document.getElementById("btn").addEventListener("click",doStuff); // Modified
function doStuff(){
var uname = document.getElementById("username").value; // Modified
google.script.run.userClicked(uname);
}
</script>
By this modification, you can see the log at Apps Script Dashboard.
IMPORTANT:
In your case, when you access to the Web Apps using your browser by logging in to your Google account, the log can be seen. But if you want to access to a script and curl, please include the access token to the request header. By this, the log can be seen. Please be careful this.
When you cannot include the access token to the request header, in that case, please link the Google Apps Script project to Google Cloud Platform (GCP) Project. By this, the log can be seen at Apps Script Dashboard.
References:
getElementById()
Apps Script Dashboard
I'm currently trying to make a paperwork wizard using google forms and apps script. At the end of the form, I'm trying to run an Apps Script once the form submits and display a HTML window. I tried using FormApp.getUI()along with the apps event trigger, but I keep getting the error Exception: Cannot call FormApp.getUi() from this context. From other posts it seems like this error is because the trigger runs the script server side. Is there a way to run this script client-side so the HTML window can be displayed?
EDIT: Conditional Sections Google forms this question is different from the one I'm asking now.
EDIT2: Code
Code.gs
function myFunction() {
var lock = LockService.getScriptLock();
var html = HtmlService.createHtmlOutputFromFile('display');
FormApp.getUi() // Or DocumentApp or SlidesApp or FormApp.
.showModalDialog(html, " Let's check if you need to present!");
SpreadsheetApp.flush();
lock.releaseLock();
}
display.html
<!DOCTYPE html>
<html>
<head>
<base target="_top">
</head>
<body>
<p>Test</p>
</body>
</html>
From the question:
At the end of the form, I'm trying to run an Apps Script once the form submits and display a HTML window.
This is not possible.
If you collected the respondent email address you could send them an email
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 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 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);
}