HTML Dialog don't close in a shared spreadsheet on Google Scripts - html

I have a spreadsheet that has a function to call an HTML dialog to choose some dates:
htmlDate = HtmlService.createHtmlOutputFromFile('datePicker')
.setHeight(120)
.setWidth(550);
SpreadsheetApp.getUi().showModalDialog(htmlDate, 'Select the dates for presentation:');
This is the part of the HTML code that defines the actions of buttons in datePicker.html:
<input type="button" onClick="submitForm()" value="OK" />
<input type="button" onClick=google.script.host.close() value="Cancel" />
And this is the part of HTML code that calls my function 'newPresentation':
<script type="text/javascript">
function submitForm() {
google.script.run.newPresentation(document.forms[0]);
Utilities.sleep(1000);
google.script.host.close();
}
</script>
The problem is that: When I run the script, it works perfectly. First, it calls my function 'newPresentation', and then it closes itself, running the rest of the script. In function 'newPresentation' there are other HTML dialogs that are called up.
But, when another user runs the script, this HTML dialog doesn't close itself. Even though it doesn't close itself, the rest of the script is executed normally. I have already shared the spreadsheet with another user, with edit privileges.
Both me and the other user are logged in a Google Account.
Some idea how to fix that problem?
PS.: Sorry for some grammatical errors, but I'm not fluent in english.

Try this:
google.script.run
.withSuccessHandler(function(){google.script.host.close();})
.newPresentation(document.forms[0]);
and put a return at the end of newPresentation();
Another possibility is to try using the dummy dialog described here: https://stackoverflow.com/a/55731456/7215091

I'm not certain of the reason that issue occurs. In all tests that i haved make, i used the same computer, with two windows of Google Chrome (one for each user logged in). I can't resolv the problem.
After that, i have tested in a new computer, with one single user logged in, and the script works perfectly.
Aparently, may be some problem related with some cache in browser, or something. I really don't know.

Related

jquery function constant monitoring

Let's say I got the folloning testing html code:
<button class="button is-small is-danger is-light" id="choose">choose</button>
and the following jquery code:
function c3 () { $("#choose").click(function() { alert("clicked")})}
once a click is registered an alert goes of withe the message "clicked".
this happens when the c3 function is executed.
But suppose I want the c3 function to constantly wait for an click event? How do I get the function to constantly monitor the id tag?
ok, I think I now understand how this works. Every time I click on the button the whole page is reloaded and subsequently the jquery function is gone. All my functions reside in a scripts.js file on the server side. The solution is to have the jquery function on the page that is being reloaded.
I hope this is heplfull for others.

Can't run function from sidebar [duplicate]

This question already has answers here:
Google Apps Script PERMISSION_DENIED with sheets addon
(2 answers)
Closed 1 year ago.
I'm having problems calling a server-side Apps Script function from a html sidebar in Google Sheets.
I've replicated my issue with the simple example code below.
What it should do
Clicking the button should call alert in my Code.gs script and display an alert to the user.
What actually happens
Clicking the button shows the error:
We're sorry, a server error occurred while reading from storage. Error code PERMISSION_DENIED.
There are no entries in the 'executions' section of the Apps Script dashboard for alert(), so it seems like the function never even gets called?
Code.gs:
function onOpen() {
const ui = SpreadsheetApp.getUi();
ui.createMenu('Matthew')
.addItem('Show Sidebar', 'sidebar')
.addToUi();
};
function sidebar() {
const html = HtmlService.createHtmlOutputFromFile('test.html')
.setTitle('Matthew Experiment')
.setWidth(300);
SpreadsheetApp.getUi().showSidebar(html);
}
function alert() {
SpreadsheetApp.getUi().alert("alert!");
}
test.html (well the body)
<body>
<div class="container">
<button class="btn btn-success" onclick="doSomething()">Click Me!</button>
</div>
<script>
function doSomething() {
google.script.run.withFailureHandler(function(error) {
console.log("error")
console.log(error.message)
}).withSuccessHandler(function(result) {
console.log("done!")
}).alert();
}
</script>
</body>
I've confirmed that the client-code is getting properly inflated with server-side function names, it's just calling the function that doesn't work.
My code seems to be the same as all of the getting started guides, but I don't see anyone else posting about this issue. Hoping someone here has deeper knowledge of the problem than I do.
Update, even weirder
I opened a private window and it worked, so I thought, maybe this is an extension causing problems? But even disabling all extensions doesn't fix it in the main window. I've logged out/in to my google account but still can't get it to work without opening a private browser window.
This problem is consistent across chrome/edge/firefox.
This happens when the global logged in user is different from the owner of script.
Logout all your google account and then re-login with the account you used to created the script. This fixed the issue.
Here's a simple example of a side bar dialog communicating with server
function showMySideBar() {
var html='<input type="button" value="Click Me" onClick="doSomething()" />';
html+='<script>function doSomething(){google.script.run.withSuccessHandler(function(msg){window.alert(msg);}).pleaseHelp();}</script>';
SpreadsheetApp.getUi().showSidebar(HtmlService.createHtmlOutput(html));
}
function pleaseHelp() {
return 'What can I do to help?';
}

Google Apps script function does not work when implemented via a library

I am right at the beginning of my Google Apps Script programming and I have a real problem that I hope some one can help me with. I spent all yesterday trying to solve this but I am going in circles.
I wrote a script that was bound to a Google Doc. From a custom menu I call a html form where I collect the necessary information.
function initiateInvoice() {
var html = HtmlService.createHtmlOutputFromFile('askQu.html')
.setWidth(800)
.setHeight(500);
DocumentApp.getUi()
.showModalDialog(html, 'Daten Eingabe');
}
This form has a „submit“ button and an "exit" button.
<input class="action" onclick="formSubmit()" type="button" value="Submit" />
<input onclick="google.script.host.close()" type="button" value="Exit" />
The script called when the "submit" button is pressed is
<script type="text/javascript">
function formSubmit() {
google.script.run.getValuesFromForm(document.forms[0]);
google.script.host.close();
}
</script>
The function getValuesFromForm then does the stuff that I need doing. And the html form is closed. The stuff that happens is a few calculations and then replacing place holders with text or numbers. I don't think this is relevant but can supply any code needed as required.
This all works exactly as I had hoped.
But now I have moved all the code and html in to an extra library (except the calling of the custom menu). The form still shows when called via the custom menu but the submit button in the form has no function any more. The "exit" button will still work. I have tried stripping everything out of the function that is called so that it does nothing nothing (to be sure it wasn't being called and getting hung up) but this does not help.
I guess that the javascript in the form is not working or not reaching my function getValuesFromForm but I really have no idea where to look or what to do.
I really appreciate any help or tips that any one can give me. Thank you.
Greetings from the Bavarian Alps.
Neill
google.script.run can't run a function from a library, only from a .gs file in the same project.
If you must have your logic code in a library, you will have to create functions in your local .gs file to call the relevant library functions, and these local functions you can invoke with google.script.run

Interactive poput at google spreadsheet, HOW: GUI or HTML service?

I have a doubt regarding how can I interact with the user at a google spreadsheet.
The idea is to show a menu, with some options, let the user select one and then process some spreadsheet data according to it. This should be done within the srpeadsheet.
The problem is, I'm not sure what is the best way to accomplish this.
At a firs instance, I though at HTML service, but I'm not sure the communication to the server side works from a spreadsheet.
I have this code, which in fact, acess to the server side, but It's pretty simple
function showHtml(htmlText){
htmlText=htmlText+'<input type="button" value="Close" onclick="google.script.host.close()" />';
var html = HtmlService.createHtmlOutput(htmlText);
thisSpr.show(html);
}
Any ideas? Just want to know which should be the best way and one or two tips on how to.
Check out Class Menu, maybe you can get what you need.

Form Double Post Issue

I understand that double posts has been a problem with forms forever.
I am using the token server-side method to handle this issue, but I find that it doesn't seem to work flawlessly. I have the system set to create a unique token for every form, and then record that token in a SESSION after it has been posted.
The SESSION is actually an array of every form the user has ever posted (to be reset when the SESSION expires), and on each submit the system checks in_array() to see if that form has ever already been posted... if so then it stops them.
Seems like in production the system cannot record the completed token into the SESSION quick enough to deal with double clicks on the submit button. So revisiting an old page is handled fine, but the immediate double click of the submit creates a problem.
Not sure what I can do to fix this issue.
How about disabling the submit button immediately upon clicking (via Javascript, with an onClick handler)? This obviously won't fix all issues, but it might cover the cases where the system isn't quick enough to record the token into SESSION.
I have had this issue as well with something internal for the company I am working for. In my experience people click multiple times because they don't think anything is happening. What I have done is to remove the ability to submit the form and display some sort of message saying that the information is being processed.
Pop-up divs and just disabling the button work well.
I had same problem and I resolve with jQuery.
I added class singleClick in submit button there I would like to have single click and also added some javascript code
<input type="submit" class="singleClick" value="Send Request">
$(function () {
$('.singleClick').on('click', function () {
$(this).attr('disabled', true);
});
});