I have lost an entire day fighting this and none of the search results I am finding are helping me.
I am writing script in Google Sheets and using Chrome browser on Windows 10 OS. I need this to run in Chrome at minimum because most of my company uses Chrome.
When my code is in full force, everything works fine but the onsubmit action in my html form calls the function in my gs TWICE and I can't figure out why.
What I am seeing is; regardless of content in my html file or gs function, if my function takes ten seconds or longer to execute then it is called a second time. How could this be? What am I doing wrong?
Step by step, here is what I am doing and what happens.
First, via add-on menu, I call the initial function that spawns the HTML model window containing the form.
function importPool() {
var ui = SpreadsheetApp.getUi();
var html = HtmlService.createHtmlOutputFromFile("importPool")
.setWidth(750)
.setHeight(300);
var dialog = ui.showModalDialog(html, "Import pool");
}
This launches "importPool.html"
<!DOCTYPE html>
<html>
<head>
<base target="_top">
</head><body>
<form id="myForm" onsubmit="processForm(myForm);">
<div style=" text-align: left; text-indent: 0px; padding: 0px 0px 0px 0px; margin: 0px 0px 0px 0px;">
<table width="100%" border="1" cellpadding="2" cellspacing="2" style="background-color: #c0c0c0;">
<tr valign="top">
<td style="border-width : 0px;text-align: left"></td>
<td style="border-width : 0px;text-align: right">
<input type="button" value="Cancel" onclick="google.script.host.close()"><input type="submit" value="Import">
</td>
</tr>
</table>
</div>
</form>
<script>
function processForm(formdata){
google.script.run.processImport(formdata);
google.script.host.close();
}
</script>
</body>
</html>
I click my "Submit" button and that calls my processImport function within my gs. For testing purposes this function only contains
function processImport(form) {
Logger.log("I am running!");
Utilities.sleep(12000);
}
As I mentioned, if I reduce the timer so the function takes less than ten seconds to execute, the function is only run once for every click of the Submit button. However if the function takes longer than ten seconds to execute it is run twice when I click Submit. It runs the first time as normal, then almost exactly ten seconds into the run time it executes the function again, top-to-bottom.
What could be causing this? I am convinced I am doing something stupid but I can't find it. Please help!