简体   繁体   English

如何从文本字段中获取用户输入,并在Google脚本中将其设置为变量?

[英]How do I take user inputs from a textfield and make them variables in a Google Script?

This is the PARTIAL Google Scripts file. 这是PARTIAL Google脚本文件。

Code.gs 代码

CriteriaColumn, Choice1, Destination1, Choice2, Destination2 should be whatever the user entered in the HTML textfield. CriteriaColumn,Choice1,Destination1,Choice2,Destination2应该是用户在HTML文本字段中输入的任何内容。

  if (colIndex == CriteriaColumn && rowIndex != 1) {

Get value from column CriteriaColumn, in the active row. 从活动行中的CriteriaColumn列获取值。

    if (status == Choice1) { 

The target sheet is named whatever Destination1 is. 目标工作表的名称为Destination1。

      var targetSheet = ss.getSheetByName(Destination1);
    }
    else if (status == Choice2) { 

The target sheet is whatever Destination2 is. 目标工作表是Destination2是什么。

      var targetSheet = ss.getSheetByName(Destination2);
    }

This is the HTML file. 这是HTML文件。 Whatever is entered into the textfields should become variables in the Google Script. 在文本字段中输入的任何内容都应成为Google脚本中的变量。

Index.html Index.html

<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
  </head>
  <body>

The text fields are below are entered by the user. 下面的文本字段由用户输入。 They should become variables in the Google Script. 它们应成为Google脚本中的变量。

    <p>What is the criteria column? Use a number, not a letter. ie. A=1....Z=26<input type="text" name="CriteriaColumn"/></p>
    <p>Choice 1<input type="text" name="Choice1"/></p>
    <p>Choice 2<input type="text" name="Choice2"/></p>
    <p>Destination 1<input type="text" name="Destination1"/></p>
    <p>Destination 2<input type="text" name="Destination2"/></p>

Clicking save will save their settings and apply them to the corresponding variables in the Google Script. 点击保存将保存其设置并将其应用于Google脚本中的相应变量。

    <p><input type="button" value="Save" onclick="google.script.host.close()" /></p>

  </body>
</html>

You can create a form in your html. 您可以在HTML中创建表单。 Then you have to put your input tags inside that form and send the form to your apps script function. 然后,您必须将输入标签放入该表单中,并将该表单发送到您的应用脚本函数。

Here you can check an example on how to do this. 在这里,您可以查看有关如何执行此操作的示例。 In this example a file input was used, but it would be similar for your input tags. 在此示例中,使用了文件输入,但是对于您的输入标签来说,它是相似的。

<body>
 <form>
   <p>What is the criteria column? Use a number, not a letter. ie. A=1....Z=26<input type="text" name="CriteriaColumn"/></p>
    <p>Choice 1<input type="text" name="Choice1"/></p>
    <p>Choice 2<input type="text" name="Choice2"/></p>
    <p>Destination 1<input type="text" name="Destination1"/></p>
    <p>Destination 2<input type="text" name="Destination2"/></p>

    <input type="button" value="Save" onclick="google.script.run.processForm(this.parentNode)" />
 </form>
</body>

"processForm" is the name of the function in the gs file, so you have to change it to the name of your function. “ processForm”是gs文件中函数的名称,因此您必须将其更改为函数的名称。

The parameter "this.parentNode" is making the reference to the parent of the button, which in this case is the form. 参数“ this.parentNode”正在引用按钮的父级,在这种情况下为表单。

The function "withSuccessHandler" will execute the javascript function (javascript code in your html) that you give as parameter. 函数“ withSuccessHandler”将执行您作为参数提供的javascript函数(html中的javascript代码)。 In the example the function is "updateUrl". 在示例中,函数为“ updateUrl”。

Create a form in your HTML: 在HTML中创建一个表单:

<form>
  <p>What is the criteria column? Use a number, not a letter. ie. A=1....Z=26<input type="text" name="CriteriaColumn"/></p>
  <p>Choice 1<input type="text" name="Choice1"/></p>
  <p>Choice 2<input type="text" name="Choice2"/></p>
  <p>Destination 1<input type="text" name="Destination1"/></p>
  <p>Destination 2<input type="text" name="Destination2"/></p>
  <input type="submit" value="Save" onclick="google.script.run.withSuccessHandler(google.script.host.close())processForm(this.parentNode)" />
</form>

Then in your Google Apps Script file, you process the form like it's an object. 然后,在您的Google Apps脚本文件中,像处理对象一样处理表单。 The name of each value will equal the name that you assigned it in your HTML: 每个值的名称将等于您在HTML中为其分配的名称:

function processForm(form) {
  var destination1 = form.Destination1;
  var destination2 = form.Destination2;
  //etc......
  //do other things with your variables
}

It is important to remember that variable scope will apply, so that your form values will only be viewable to your processForm() function unless you pass them to another function. 重要的是要记住,将应用变量作用域,以便您的表单值仅对processForm()函数可见,除非将它们传递给另一个函数。

I usually close my processForm() functions with a return 200 so that the success handler will trigger and close your custom interface. 我通常用return 200关闭我的processForm()函数,以便成功处理程序将触发并关闭您的自定义接口。 I don't know if this is necessary or not, but it works and I have fewer problems with closing custom interfaces this way. 我不知道这是否必要,但它可以正常工作,并且以这种方式关闭自定义接口的问题也较少。

For more information, see the Google API reference on client-to-server communication 有关更多信息,请参阅有关客户端到服务器通信的Google API参考

暂无
暂无

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

相关问题 如何从同一个输入框中获取多个输入并存储它们? - How Do I Take Multiple Inputs From the same Input Box and store them? 如何从php的会话数组中获取未知数量的元素,并将其用作JavaScript中的单独变量? - How do I take an unknown number of elements from a session array in php and use them as separate variables in JavaScript? 如何从输入中获取多个字符串并将它们放入同一数组中? - How to take multiple strings from inputs and place them into the same array? 如何使用 Jquery 从用户输入制作表格内容? - How I could make a table content from user inputs with Jquery? 如何制作表格(两个输入)将我带到另一页,其选择取决于输入 - How do I make form (two inputs) take me to another page which selections depends on the input 接受用户的多个输入 - Take multiple inputs from user 如何根据用户输入从Google图片中提取图片并将其放置在页面上? - How do I extract images from Google Images based on user input and place them on a page? 如何使用 Javascript 从父 div 获取输入并从中获取实时值 - How to take the inputs from a parent div and take realtime values from them with Javascript 如何捕获用户输入并将其发送到服务器? - How do you capture user inputs and send them to a server? 如何从JavaScript中获取变量并将其放入MySQL数据库? - How can I take variables from JavaScript and put them into a MySQL Database?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM