2016-01-21 68 views
0

我有一個程序,我試圖使電子表格中的數據,並使其成爲一個谷歌形式,目前我試圖使11個單元格填充一個下拉列表中。有setChoices方法,在它內部創建選擇,但你不能有任何其他類型的代碼在裏面說11使用for循環的選擇每一個都是數組中的下一個字符串。如果你能想到的任何其他方法,請讓我知道...有沒有辦法使用循環在谷歌表單腳本中的列表項中設置選項?

這是目前我的代碼:

function onOpen() { 
    var sheet = SpreadsheetApp.getActiveSheet(); 

    var names = sheet.getRange(1, 1, 200); 

    var values = names.getValues(); 

    var number = 0 

    var count = 0; 

    var array = "'" + values[0] + "'"; 

    while (values[count] != "") { 
    number++; 
    count++; 
    if (values[count] != "") { 
     array = array + ", " + "'" + values[count] + "'"; 
    } 
    } 

    Logger.log(number); 
    Logger.log(array); 
    for (var i = 0; i < number; i++) { 
    Logger.log(values[i]); 

    } 

    var form = FormApp.create("Cleaners"); 

    var formid = form.getId(); 

    var dropdown1 = form.addListItem(); 

    dropdown1.setTitle("Choose a Cleaner:"); 

    dropdown1.setChoices([ 
    dropdown1.createChoice(values[0]), 
    dropdown1.createChoice(values[1]), 
    dropdown1.createChoice(values[2]), 
    dropdown1.createChoice(values[3]), 
    dropdown1.createChoice(values[4]), 
    dropdown1.createChoice(values[5]), 
    dropdown1.createChoice(values[6]), 
    dropdown1.createChoice(values[7]), 
    dropdown1.createChoice(values[8]), 
    dropdown1.createChoice(values[9]), 
    dropdown1.createChoice(values[10]) 
    ]); 

} 

當我做我不下拉希望有去並更改的量如果將另一個名稱添加到列表中,則創建選項。

+0

如果你能告訴我們你到目前爲止已經嘗試過的效果會更好 – iJay

+0

只是一種瘋狂的猜測,這與你的代碼的結構比使用的方法更有關係。 –

回答

0
//try replacing your code 
//this code requires you to access each value in the array "values" separately. 
dropdown1.setTitle("Choose a Cleaner:"); 

dropdown1.setChoices([ 
    dropdown1.createChoice(values[0]), 
    dropdown1.createChoice(values[1]), 
    dropdown1.createChoice(values[2]), 
    dropdown1.createChoice(values[3]), 
    dropdown1.createChoice(values[4]), 
    dropdown1.createChoice(values[5]), 
    dropdown1.createChoice(values[6]), 
    dropdown1.createChoice(values[7]), 
    dropdown1.createChoice(values[8]), 
    dropdown1.createChoice(values[9]), 
    dropdown1.createChoice(values[10]) 
    ]); 

// with the code 
//this code allows you to access this entire array 
//if values = ['cleaner1', 'cleaner2', 'cleaner3', ...] 
//all of those values would be accessed by drowdown1.setChoiceValues(values); 
//setChoiceValues(values); is doing this .setChoiceValues(['cleaner1', 'cleaner2', 'cleaner3', ...]); 
//where values is an array 
dropdown1.setTitle("Choose a Cleaner:") 
     .setChoiceValues(values); 
+0

添加一些解釋和回答這個答案如何幫助OP在解決當前問題 –

0

最近我有同樣的問題,最後我已經成功地用一個字符串數組來做到這一點:

var item = newForm.addListItem(); 
var arr = []; 

item.setTitle('Please choose somthing'); 

    for (var y = 0; y < 10; y++) { 
    arr.push(y); 
} 

item.setChoiceValues(arr); 

好運!

相關問題