簡體   English   中英

如何從文本字段中獲取用戶輸入,並在Google腳本中將其設置為變量?

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

這是PARTIAL Google腳本文件。

代碼

CriteriaColumn,Choice1,Destination1,Choice2,Destination2應該是用戶在HTML文本字段中輸入的任何內容。

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

從活動行中的CriteriaColumn列獲取值。

    if (status == Choice1) { 

目標工作表的名稱為Destination1。

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

目標工作表是Destination2是什么。

      var targetSheet = ss.getSheetByName(Destination2);
    }

這是HTML文件。 在文本字段中輸入的任何內容都應成為Google腳本中的變量。

Index.html

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

下面的文本字段由用戶輸入。 它們應成為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>

點擊保存將保存其設置並將其應用於Google腳本中的相應變量。

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

  </body>
</html>

您可以在HTML中創建表單。 然后,您必須將輸入標簽放入該表單中,並將該表單發送到您的應用腳本函數。

在這里,您可以查看有關如何執行此操作的示例。 在此示例中,使用了文件輸入,但是對於您的輸入標簽來說,它是相似的。

<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”是gs文件中函數的名稱,因此您必須將其更改為函數的名稱。

參數“ this.parentNode”正在引用按鈕的父級,在這種情況下為表單。

函數“ withSuccessHandler”將執行您作為參數提供的javascript函數(html中的javascript代碼)。 在示例中,函數為“ updateUrl”。

在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>

然后,在您的Google Apps腳本文件中,像處理對象一樣處理表單。 每個值的名稱將等於您在HTML中為其分配的名稱:

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

重要的是要記住,將應用變量作用域,以便您的表單值僅對processForm()函數可見,除非將它們傳遞給另一個函數。

我通常用return 200關閉我的processForm()函數,以便成功處理程序將觸發並關閉您的自定義接口。 我不知道這是否必要,但它可以正常工作,並且以這種方式關閉自定義接口的問題也較少。

有關更多信息,請參閱有關客戶端到服務器通信的Google API參考

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM