繁体   English   中英

如何将表单数据传递到客户端函数(google.scripts.run)并将结果返回到HTML?

[英]How to pass a form data to a client side function (google.scripts.run) and return result to HTML?

我正在尝试从表单中获取一个ID,并针对CODE.gs中的函数运行该ID,该函数会检查电子表格中的ID,并在HTML中返回名称。 现在,当它运行时,它给了我一个不确定的信息。

HTML:

<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
    <script>
    var result
    var id
    function getName() {
     var id = document.getElementById("SearchID").value;
      document.getElementById("SearchID").value = "";
     var result = google.script.run.findName(id);
     document.getElementById("NameDisp").innerHTML = result;
    }
    </script>
  </head>
  <body>
    Welcome to Verify. To begin, enter an ID below.<br><br>


    Student ID:<input type="text" id="SearchID" name="id" size="10">
    <input type="button" name="search" type="submit" value="Go" onclick="getName();";/>

    <div id="NameDisp"></div>
    <div id="grade"></div>

    <br><br>
    <input type="button" value="Close"
        onclick="google.script.host.close()" />
  </body>

代码库

function findName(empID) {
  var ss = SpreadsheetApp.getActive();
  var sh = ss.setActiveSheet(ss.getSheetByName('STU401'), true);
  var last=ss.getLastRow();
  var data=sh.getRange(1,1,last,5).getValues();  // create an array of data from columns A and B

  for(nn=0;nn<data.length;++nn){
    if (data[nn][0]==empID){break} ;// if a match in column A, break loop
      }
  var empName = data[nn][4]

return empName

}

google.script.run异步调用一个函数,因此您必须提供一个回调函数,该回调函数将被发送返回值。

您可以使用withSuccessHandler提供回调,如下所示:

google.script.run
.withSuccessHandler(function(result) {
  // this function is called if the function runs successfully
  // and the parameter "result" will have the value returned
  // by the findName function
  document.getElementById("NameDisp").innerHTML = result;

})
.findName(id);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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