Google apps script google.script.run.withSuccessHandler跳过onSuccess函数
我试图用脚本绑定到的电子表格中的数据填充HTML文件中的select元素 到目前为止,我有以下newDeal.html的代码。空的select元素(id=“contactname”)是我需要用arr_customers()函数返回的数据填充的元素:Google apps script google.script.run.withSuccessHandler跳过onSuccess函数,google-apps-script,google-sheets,Google Apps Script,Google Sheets,我试图用脚本绑定到的电子表格中的数据填充HTML文件中的select元素 到目前为止,我有以下newDeal.html的代码。空的select元素(id=“contactname”)是我需要用arr_customers()函数返回的数据填充的元素: <!DOCTYPE html> <html> <head> <base target="_top"> <link rel="stylesheet" href="http
<!DOCTYPE html>
<html>
<head>
<base target="_top">
<link rel="stylesheet" href="https://ssl.gstatic.com/docs/script/css/add-ons1.css">
</head>
<body onload="onload()">
<p>Contact name:</p>
<select id="contactname" autocorrect="on" autocomplete="on">
</select>
</body>
<script>
var vals;
function placeCustomers(values) {
var select = document.getElementById("contactname");
for(var i = 0; i < values.length; i++) {
Logger.log(i);
var opt = values[i];
var el = document.createElement('option');
el.textContent = opt;
el.value = opt;
select.appendChild(el);
}
}
function onload() {
placeCustomers(vals);
}
function onSuccess(values) {
vals = values;
}
google.script.run.withSuccessHandler(onSuccess).arr_customers();
</script>
</html>
问题在于,在arr_customer返回值后,onSuccess函数没有运行。我使用了一些日志来发现这一点。我甚至注释了所有不必要的代码,并尝试通过Spreadhseets UI在onSuccess函数中使用一行“Logger.log('test')”来运行它,但日志中没有显示任何内容
有人知道为什么会这样吗?我看过谷歌文档,上面说withSuccessHandler:
设置要在服务器端函数返回时运行的回调函数
成功地服务器的返回值作为
第一个参数和用户对象(如果有)作为第二个参数传递
争论
也许我遗漏了什么,如果有人有任何线索,请分享:)解决了
以下是newDeal.html的最终代码,该代码有效:
<!DOCTYPE html>
<html>
<head>
<base target="_top">
<link rel="stylesheet" href="https://ssl.gstatic.com/docs/script/css/add-ons1.css">
<style>
body {
padding-left: 10px;
}
</style>
</head>
<body onload='updateCustomers()'>
<p>Nome do Contato:</p>
<select id="slContato" name="Contato" autocorrect="on" autocomplete="on">
</select>
</body>
<script>
function placeCustomers(values) {
var select = document.getElementById("slContato");
for(var i = 0; i < values.length; i++) {
var opt = values[i];
var el = document.createElement("OPTION");
el.textContent = opt;
el.value = opt;
select.appendChild(el);
}
}
function updateCustomers() {
google.script.run.withSuccessHandler(placeCustomers).arr_clientes();
}
</script>
</html>
身体{
左侧填充:10px;
}
诺姆·杜康塔托:
功能位置客户(价值观){
var select=document.getElementById(“slContato”);
对于(变量i=0;i
这似乎是
Logger.log()
的一个问题。在我从HTML代码中删除了所有日志行之后,它成功了 什么时候执行google.script.run
行?函数中没有它。它在用户打开HTML时执行。我已经检查过了,arr_customer函数在窗口弹出时成功运行。另外,我还尝试了使用withFailureHandler,但回调函数没有达到文档中所述的程度,只有在服务器函数引发未处理的异常时,才会调用google.script.run
异步任务的故障处理程序。如果服务器函数通过任何其他方式退出,则其输出(从返回
,否则未定义的
)将传递给任务的成功处理程序。PS您编写的onSuccess
函数只在客户端代码中设置了一个变量-它从不告诉客户端代码中的任何其他函数使用该变量的新值做任何事情。我明白您的意思。函数arr_customers
成功返回值,而onSuccess
函数未运行(已删除所有代码并使用日志进行检查)。关于您的PS:onSuccess
将值写入变量,然后请注意
元素中的操作onload
。它应该使用变量上的值运行函数placeCustomers
。不管怎样,在我的第一个代码中,我调用了placeCustomers
insideonSuccess
。。。这只是一个解决这一问题的实验。Logger
是一个应用程序脚本类-它不是浏览器可用的功能。登录浏览器是通过控制台完成的。log
,并将输出发送到浏览器控制台。太好了!我对HTML非常陌生,这个控制台.log将非常有用,非常感谢您的关注:)
function uiBuilder() {
var ui = SpreadsheetApp.getUi();
ui.createMenu('Project')
.addItem('Create new project...', 'newProject')
.addToUi();
}
function newProject() {
var html = HtmlService.createHtmlOutputFromFile('newDeal');
SpreadsheetApp.getUi().showModalDialog(html, 'New Project');
}
<!DOCTYPE html>
<html>
<head>
<base target="_top">
<link rel="stylesheet" href="https://ssl.gstatic.com/docs/script/css/add-ons1.css">
<style>
body {
padding-left: 10px;
}
</style>
</head>
<body onload='updateCustomers()'>
<p>Nome do Contato:</p>
<select id="slContato" name="Contato" autocorrect="on" autocomplete="on">
</select>
</body>
<script>
function placeCustomers(values) {
var select = document.getElementById("slContato");
for(var i = 0; i < values.length; i++) {
var opt = values[i];
var el = document.createElement("OPTION");
el.textContent = opt;
el.value = opt;
select.appendChild(el);
}
}
function updateCustomers() {
google.script.run.withSuccessHandler(placeCustomers).arr_clientes();
}
</script>
</html>