应用脚本不会始终更新 google 电子表格单元格值
App script doesn't update the google spreadsheet cell values always
我正在使用 html 代码创建一个仪表板,用户可以在其中 select 日期,然后根据 selected 日期从远程 API 获取一些值,然后显示这些值在 sheet.
我有 html 文件如下:
<!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>
我有code.gs如下:
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);
};
当我 select 来自脚本编辑器的函数 (fncOpenMyDialog()) 时,它在传播 sheet 上创建了一个仪表板,在那里我可以 select 日期但是与 functionToRunOnFormSubmit 函数一样,我正在记录参数,然后相应地设置 B3 和 B4 单元格值。它也没有更新它没有在脚本编辑器中登录。
问题是您在调用异步 ajax 调用的 google.script.run 函数后立即调用 "close"。
在某些情况下,它甚至可能没有给浏览器足够的时间来启动 ajax 调用,或者由于页面正在关闭而被取消。所以有时它可能会到达后端脚本,有时不会。
查看文档并处理成功(从那里关闭对话框)和失败(向用户显示错误)
https://developers.google.com/apps-script/guides/html/reference/run
虽然文档没有明确显示,但您可以像这样挂接这两个调用:
google.script.run.withFailureHandler(onFailure).withSuccessHandler(onSuccess).yourCall();
我正在使用 html 代码创建一个仪表板,用户可以在其中 select 日期,然后根据 selected 日期从远程 API 获取一些值,然后显示这些值在 sheet.
我有 html 文件如下:
<!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>
我有code.gs如下:
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);
};
当我 select 来自脚本编辑器的函数 (fncOpenMyDialog()) 时,它在传播 sheet 上创建了一个仪表板,在那里我可以 select 日期但是与 functionToRunOnFormSubmit 函数一样,我正在记录参数,然后相应地设置 B3 和 B4 单元格值。它也没有更新它没有在脚本编辑器中登录。
问题是您在调用异步 ajax 调用的 google.script.run 函数后立即调用 "close"。
在某些情况下,它甚至可能没有给浏览器足够的时间来启动 ajax 调用,或者由于页面正在关闭而被取消。所以有时它可能会到达后端脚本,有时不会。
查看文档并处理成功(从那里关闭对话框)和失败(向用户显示错误)
https://developers.google.com/apps-script/guides/html/reference/run
虽然文档没有明确显示,但您可以像这样挂接这两个调用:
google.script.run.withFailureHandler(onFailure).withSuccessHandler(onSuccess).yourCall();