简体   繁体   中英

App script doesn't update the google spreadsheet cell values always

I am using html code to create a dashboard where user can select a date and then based on selected date fetch some values from remote APIs and then show these values in the sheet.

I have html file something like:

<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
  <script src="//code.jquery.com/jquery-1.10.2.js"></script>
  <script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
  <link rel="stylesheet" href="/resources/demos/style.css">
  <script>
  $(function() {
    $( "#datepicker" ).datepicker();
  });
  </script>

</head>
<body>
<form>
<select name="Student" id="category">

<option value="" selected="selected">Select Student</option>

<option value="Abercrombie, Amber">Abercrombie, Amber(Gr 11)</option>
<option value="Yupa, Jason">Yupa, Jason(Gr 9)</option>
</select>
Date: <input type="text" id="datepicker" name="datepicker">
<input type="submit" value="Submit" onclick="myFunction()">
</form>

<p id="demo"></p>
<script>
function myFunction() {
    var x = document.getElementById("category").value;
    var x2 = document.getElementById("datepicker").value;
//document.getElementById("demo").innerHTML = x;

    google.script.run.functionToRunOnFormSubmit(x, x2);
    google.script.host.close();

}
</script>

</body>
</html>

I have code.gs as follows:

function fncOpenMyDialog() {
  //Open a dialog

  var htmlDlg = HtmlService.createHtmlOutputFromFile('HTML_myHtml')
      .setSandboxMode(HtmlService.SandboxMode.IFRAME)
      .setWidth(500)
      .setHeight(300);
  SpreadsheetApp.getUi()
      .showModalDialog(htmlDlg, 'Dashboard');


};

function functionToRunOnFormSubmit(fromInputForm, datevalue) {
  Logger.log(fromInputForm);
  Logger.log(datevalue);
  SpreadsheetApp.getActiveSheet().getRange('B3').setValue(fromInputForm);
  SpreadsheetApp.getActiveSheet().getRange('B4').setValue(datevalue);
};

When I select the function(fncOpenMyDialog()) from script-editor, It create a dashboard on the spreadsheet, Where I am able to select the date but as in functionToRunOnFormSubmit function I am logging the argument and then correspondingly setting the B3 and B4 cell values. It is not getting updated also It is not getting logged in the script editor.

The problem is you are calling "close" right after calling the google.script.run function which is an asynchronous ajax call.

In some cases it likely doesnt even give the browser enough time to start the ajax call or it gets cancelled because the page is closing. So sometimes it might reach the backend script, and sometimes wont.

take a look at the documentation and handle both success (close dialog from there) and failure (show error to the user)

https://developers.google.com/apps-script/guides/html/reference/run

While the documentation doesnt show it explicitly, you can hook both calls like this:

google.script.run.withFailureHandler(onFailure).withSuccessHandler(onSuccess).yourCall();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM