【问题标题】:listbox does not pass string to google script列表框不将字符串传递给谷歌脚本
【发布时间】:2015-03-20 19:05:07
【问题描述】:

当我从列表框中选择一个用户时,onChange() 事件会触发一个函数。它应该将字符串传递给函数。然后代码找到用户的密码并将其返回以进行比较。如果我硬编码用户值,以下是可以正常工作的代码,但当我从列表框中选择它时就不行。

function addClients(clients){
  $('#customer').empty(); 
  $('#customer').append('<option> ---- Choose a user ----</option>');
  for (var i in clients) {
    $('#customer').append('<option>'+clients[i]+'</option>');
    $('#customer').trigger("chosen:updated"); 
  }
} 

getval 函数:

function getval(sel){
    var usrpass = google.script.run.getuserpass(sel.value);
    alert(usrpass);
}

code.gs中的函数如下

function getuserpass(userval){
 var usrpass = "";
 var doc = SpreadsheetApp.openById("spreadsheet id");
 var sheet = doc.getActiveSheet();
 var data = sheet.getRange(3, 3, sheet.getLastRow(),5).getValues();; 
 for(n=0;n<data.length;++n){
 // iterate row by row and examine data in column A
 if(data[n][0].toString().match(userval)==userval){ usrpass = data[n][4]};
 }
 return usrpass;
}

为什么返回值是未定义而不是密码。

如果我在函数中硬编码用户名并运行该函数,则返回值为第五列中的值。

【问题讨论】:

  • getval() 函数的顶部放置一个 Logger.log('value is: ' + sel.value); 语句,运行代码,查看日志。 sel.value 的值是你所期望的吗?
  • 谢谢。它返回列表框名称、ID 号和文本的字符串。我需要获取列表框中的值,而不是全文
  • 这是一个 HTML 对话框吗?你为什么使用google.script.run()google.script.run 仅用于放入 HTML &lt;script&gt; 标记中。你能显示列表框吗?它是 HTML 中的列表框吗?
  • 它是一个使用电子表格中的查询和数据构建的 html 列表框。函数 addClients(clients){ $('#customer').empty(); $('#customer').append(''); for (var i in clients) { $('#customer').append(''); $('#customer').trigger("chosen:updated"); } }
  • 什么是sel?它是一个对象吗?它是一个表单对象吗? sel 是如何传递给 getval 函数的?

标签: javascript jquery google-apps-script google-sheets


【解决方案1】:

尝试像这样构造代码:

<script>
  function onSuccess(returnVal) {
    alert('Success! ' + returnVal);
  };

  function getval(sel){

    var selectValue = sel.value;
    console.log('selectValue: ' + selectValue);

    google.script.run
      .withSuccessHandler(onSuccess)
      .getuserpass(sel.value);
  };

</script>

作为调试测试,您可以遍历对象以查看其中的真实内容。

for (var propertyVal in sel) {
  console.log('this property: ' + propertyVal);
  console.log('this value: ' + sel[propertyVal]);
};

然后看看对象里到底有什么。

【讨论】:

  • 是的,这不是字符串,但该属性会返回很多值
  • 函数 getuserpass 根本没有被调用!
  • 查看更新的答案。您需要一个withSuccessHandler 来处理来自gs 代码的返回。我之前犯了一个错误,将Logger.log() 放在应该是console.log() 的地方
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-07
  • 2016-09-15
  • 2011-05-21
  • 1970-01-01
相关资源
最近更新 更多