2015-10-07 65 views
0

這是PARTIAL Google腳本文件。如何從文本字段獲取用戶輸入並將它們變爲Google腳本中的變量?

Code.gs

CriteriaColumn,選擇1,DESTINATION1,選擇2,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> 

回答

0

你可以在你的html中創建一個表單。然後,您必須將輸入標籤放在該表單中,並將表單發送到您的應用腳本功能。

Here你可以檢查如何做到這一點的例子。在這個例子中使用了一個文件輸入,但它對於你的輸入標籤是相似的。

<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」。

+0

感謝您的回覆。我如何在我的gs文件中將Choice1,Choice2等分配爲變量? – FlappyBird

0

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

然後在你的谷歌Apps腳本文件,你處理表單就像它是一個對象。每個值的名稱將等於您在HTML賦予它的名字:

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

重要的是要記住,變量的作用域將應用,讓你的表單值只能查看到您的processForm()功能是非常重要的,除非你將它們傳遞給另一個函數。

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

有關詳細信息,請參閱client-to-server communication上的Google API參考

+0

如何存儲表單中的變量?本質上,我試圖讓最終用戶更改變量值。 – FlappyBird

+0

在上面的第二部分代碼中,腳本從UI獲取表單。您可以使用您在HTML中給出的相同名稱訪問表單的屬性(即在腳本文件中,您可以通過調用'form.Destination1'來訪問腳本中名稱爲'Destination1'的文本框。在你的腳本中分配你的變量如果你想讓這些值在用法之間保持不變,你需要查看[腳本屬性](https://developers.google.com/apps-script/guides/properties)。有助於澄清? – Silinus

相關問題