2015-01-15 39 views
3

這段代碼出了什麼問題?當我登錄數組列表時,在sumbit函數之前,它的確定(包含陣列中元素的期望內容和位置)。但是,當我通過e.parameter.arrayList時,它不具有相同的值,也不是元素。如何解決這個問題?爲什麼嘗試從`e.parameter.variableName`方法恢復數組不工作?

function showList(folderID) { 
    var folder = DocsList.getFolderById(folderID); 
    var files = folder.getFiles(); 
    var arrayList = []; 
    for (var file in files) { 
    file = files[file]; 
    var thesesName = file.getName(); 
    var thesesId = file.getId(); 
    var thesesDoc = DocumentApp.openById(thesesId); 
    for (var child = 0; child < thesesDoc.getNumChildren(); child++){ 
    var thesesFirstParagraph = thesesDoc.getChild(child); 
    var thesesType = thesesFirstParagraph.getText(); 
     if (thesesType != ''){ 
     var newArray = [thesesName, thesesType, thesesId]; 
     arrayList.push(newArray); 
     break; 
     } 
     } 
    } 
    arrayList.sort(); 
    var mydoc = SpreadsheetApp.getActiveSpreadsheet(); 
    var app = UiApp.createApplication().setWidth(550).setHeight(450); 
    var panel = app.createVerticalPanel() 
        .setId('panel'); 

    var label = app.createLabel("Choose your theses").setStyleAttribute("fontSize", 18); 
    app.add(label); 
    panel.add(app.createHidden('checkbox_total', arrayList.length)); 
    panel.add(app.createHidden('arrayList', arrayList)); 
    Logger.log(" arrayList before submit = " + arrayList); 


    for(var i = 0; i < arrayList.length; i++){  
     var checkbox = app.createCheckBox().setName('checkbox_isChecked_'+i).setText(arrayList[i][0]); 
     Logger.log("arrayList[i][0] = " + arrayList[i][0]); 
     Logger.log("arrayList[i] ====> " + arrayList[i]); 
     panel.add(checkbox); 
    } 
    var handler = app.createServerHandler('submit').addCallbackElement(panel); 
    panel.add(app.createButton('Submit', handler)); 
    var scroll = app.createScrollPanel().setPixelSize(500, 400); 
    scroll.add(panel); 
    app.add(scroll); 
    mydoc.show(app); 


} 

function include(arr, obj) { 
    for(var i=0; i<arr.length; i++) { 
     if (arr[i] == obj) // if we find a match, return true 
      return true; } 
    return false; // if we got here, there was no match, so return false 
} 

function submit(e){ 
    Logger.log(" arrayList = " + arrayList); 
    var arrayList = e.parameter.arrayList; 
    var numberOfItems = Number(e.parameter.checkbox_total); 
    var thesesArrays = []; 
    var usedThesesType = []; 
    var usedThesesName = []; 
    for(var i = 0; i < numberOfItems; i++){ 
    if(e.parameter['checkbox_isChecked_'+i] == 'true'){ 
    Logger.log(" arrayList inside for loop = " + arrayList); 
     Logger.log(" arrayList[i] = " + arrayList[i]); 
     thesesArrays.push(arrayList[i]); 
     usedThesesType.push(arrayList[i][1]); 
      Logger.log(" arrayList[i][1] = " + arrayList[i][1]); 

     usedThesesName.push(arrayList[i][0]); 
     Logger.log(" arrayList[i][0] = " + arrayList[i][0]); 
    } 
    } 
    var allThesesTypeArray = []; // To control Theses type apparence in the final doc 
    for (var i = 0; i < arrayList.length; i++) { 
    var thesesType = arrayList[i][1]; 
    if (!(include(allThesesTypeArray, thesesType))){ 
     allThesesTypeArray.push(thesesType);  } 
    } 
    var targetDocId = userProperties.getProperty('targetDocId'); 
    for (var i = 0; i < thesesArrays.length; i++) { 
    var thesesType = thesesArrays[i][1]; 
    Logger.log(" thesesArrays = " + thesesArrays); 
    var thesesId = thesesArrays[i][2]; 
    importTheses(targetDocId, thesesId, thesesType);  
    } 
    cleanNotUsedThesesTitles(targetDocId, allThesesTypeArray, usedThesesType); 
    if(userProperties.getProperty('atLeastOneTheseType') == 0){ 
     Browser.msgBox('There was no theses inside your model. Check it!'); 
    } 
    var joinAndInsert = userProperties.getProperty('joinAndInsert'); 
    showURL(usedThesesName, joinAndInsert); 
    return UiApp.getActiveApplication().close(); 
} 
+0

因此,'submit(e)'函數是從與電子表格關聯的窗體觸發的?你有一個觸發器的功能名稱設置爲「提交」? –

+0

你從'e'得到的值是多少? –

回答

2

你應該得到一個對象被傳遞並分配給e參數。您可以通過該對象遍歷,看看有什麼屬性和值是:

function submit(e) { 
    Logger.log("submit ran: " + e); 
    Logger.log("values?: " + e.values); 

    for(var propertyName in e) { 

    Logger.log("propertyName: " + propertyName); 
    Logger.log("This property value is: " + e[propertyName]); 
    Logger.log(" "); 
    } 
} 
+0

是的,我已經完成了。但是,我也不知道發生了什麼 – craftApprentice

3

你不能簡單地傳遞一個數組表單域,並得到該數組的值回。這與您在Get String Value of Blob Passed to e.parameter in Apps Script上詢問的完全相同。唯一的區別是你現在想要傳遞一個數組而不是Blob。

您只能傳遞字符串作爲字段的值,因此您需要將您的數據(arrayList)轉換爲字符串。然後您需要將該字符串轉換回數組。使用JSON.stringify()和JSON.parse()來做到這一點。

變化

panel.add(app.createHidden('arrayList', arrayList)); 

panel.add(app.createHidden('arrayList', JSON.stringify(arrayList))); 

,改變

var arrayList = e.parameter.arrayList; 

var arrayList = JSON.parse(e.parameter.arrayList); 
2

我認爲這是一個錯誤,因爲你寫的:

for (var file in files) { 
    file = files[file]; 

其中變量文件是數組的索引,讓你改變你的bucle該值。 您是否嘗試過使用其他名稱,例如:

for (var file in files) { 
    f = files[file]; 
相關問題