简体   繁体   English

将数据从下拉列表 => 传递到 javascript function(Google App 脚本)

[英]Passing data from dropdown => to javascript function (Google App script)

Hopefully I've included enough of the code w/o having to post it all...希望我已经包含了足够多的代码,而不必全部发布...

I have a main function that calls displayDropdown()- which calls an HTMLService and displays a modal with a dropdown and a text box:我有一个主要的 function 调用 displayDropdown()- 它调用 HTMLService 并显示带有下拉列表和文本框的模式:
模态截图 . .
This is the (condensed) javascript code that stores the data:这是存储数据的(压缩的)javascript 代码:

<html>
<input type="submit" value="Submit" class="action" onclick="sendData()" />       
</html>
<script>
function sendData() {
          var values = {};
          values.textJob = document.getElementById("input").value;
          values.selectedJob = document.getElementById("dropJob").value;
          google.script.run.withSuccessHandler(closeIt).grabData(values);
       };
        function closeIt(){
          google.script.host.close()
        };
</script>

Then the grabData() function in my.gs file:然后my.gs文件中的grabData() function:

function grabData(values) {
  if(values.textJob=="")
   //return values.selectedJob;
    Logger.log(values.selectedJob);
  else 
    //return values.textJob;
    Logger.log(values.textJob);
}

If I keep the returns commented out and try to log the data, I get the expected data logged.如果我将退货注释掉并尝试记录数据,我会记录预期的数据。 But if I reverse that, and return it instead, go back up to the main function just after displayDropdown() was called, and set a variable to equal the grabData function:但是,如果我反转它,而是将其返回,则 go 在调用 displayDropdown() 后立即备份到主 function,并将变量设置为等于 grabData ZC1C425268E68A94D1AB5074C14

displayDropdown();
var stuff = grabData();
Logger.log(stuff);

I get an error that says:我收到一条错误消息:

错误信息

Why can't I access the data?为什么我无法访问数据?

This is what I usually do to send data from HTML form to GS:这是我通常将数据从 HTML 表单发送到 GS 的方法:

HTML HTML

<form method="POST" action="#" id="formID">
  <button class="btn" type="submit">Send</button>
</form> 

JS JS

document.querySelector("#formID").addEventListener("submit", function(e) {
  var test = google.script.run.withSuccessHandler('client side function').processForm(this);
}); 

I usually pass 'this' as an argument and I process the information on the GS.我通常将“this”作为参数传递,并在 GS 上处理信息。

EDIT:编辑:

GS GS

function processForm(values){

  Logger.log(values);
  Logger.log(typeof values);

}

Screenshoots :截图

1- Web app 1- Web 应用程序

在此处输入图像描述

2- Server logs (function processForm) 2- 服务器日志(函数 processForm)

在此处输入图像描述

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

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