列表框接受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
}
謝謝,使用listitem的值參數我是abl e解決它 – olilarkin