2014-03-07 55 views
0

我正在努力處理處理函數中的多選列表框。我的列表框條目包含逗號,理想情況下,我想要獲取處理程序中所選項目的數組,但它似乎是單個字符串。處理GAS列表框多選

有誰知道我該怎麼做?

感謝

function doGet() { 
... 
var completedReportsList = app.createListBox(true) 
.setId('list') 
.addItem("Bloggs, Joe") 
.addItem("Doe, Jane"); 
var generatePDFButton = app.createButton("Generate PDFs") 
.setId("generatePDFButton"); 
var generatePDFButtonHandler = app.createServerClickHandler('generatePDF') 
.addCallbackElement(completedReportsList) 
generatePDFButton.addClickHandler(generatePDFButtonHandler); 
... 
} 

function generatePDF(e) { 
var app = UiApp.getActiveApplication(); 
Logger.log(e.parameter.list.length); // undefined 
Logger.log(e.parameter.list); // <- how to parse multiple selected items? 
return app; 
} 

回答

1

列表框接受2個參數,a 「所示」 值和一個 「返回」 值。第二個參數是可選的,如果不存在,則返回的值將成爲顯示的值。

是否有可能爲您使用第二個字段的值不包含逗號,例如使用|分隔符,以便您可以使用每個數組元素中的替換輕鬆地重建原始值?

代碼將如下所示:

(你也忘了給一個名稱列表,這就是爲什麼你有長度=未定義e.parameter.list)

function doGet() { 
    var app = UiApp.createApplication().setTitle('test'); 

    var completedReportsList = app.createListBox(true) 
    .setId('list').setName('list') 
    .addItem("Bloggs, Joe","Bloggs| Joe") 
    .addItem("Doe, Jane","Doe| Jane");// this could of course be obtained in the script with a .replace string method. 
    var generatePDFButton = app.createButton("Generate PDFs").setId("generatePDFButton"); 
    var generatePDFButtonHandler = app.createServerClickHandler('generatePDF').addCallbackElement(completedReportsList); 
    generatePDFButton.addClickHandler(generatePDFButtonHandler); 
    app.add(completedReportsList).add(generatePDFButton) 
    return app 
} 

function generatePDF(e) { 
    var app = UiApp.getActiveApplication(); 
    Logger.log(e.parameter.list.split(',').length); // undefined 
    var listValues = e.parameter.list.split(','); // an array of values 
    for(var n in listValues){ 
    Logger.log(listValues[n].replace('|',',')); 
    } 
    return app; 
} 

而且這裏是一個修改後的版本,在doGet函數中自動創建替代字段:

function doGet() { 
    var app = UiApp.createApplication().setTitle('test'); 
    var completedReportsList = app.createListBox(true).setId('list').setName('list'); 
    var items = ["Bloggs, Joe","Doe, Jane"]; 
    for(var n in items){ 
    item = items[n]; 
    modItem = items[n].replace(',','|'); 
    completedReportsList.addItem(item,modItem); 
    } 
    var generatePDFButton = app.createButton("Generate PDFs").setId("generatePDFButton"); 
    var generatePDFButtonHandler = app.createServerClickHandler('generatePDF').addCallbackElement(completedReportsList); 
    generatePDFButton.addClickHandler(generatePDFButtonHandler); 
    app.add(completedReportsList).add(generatePDFButton) 
    return app 
} 
+0

謝謝,使用listitem的值參數我是abl e解決它 – olilarkin