2013-12-13 51 views
2

裏面的處理函數波紋管我試圖存儲和獲得命名的數組的數組的價值「論文」:Google Apps腳本:如何在ScriptProperties中存儲和檢索數組?

var fact_list = [ ["Kennedy Inauguration", "politics", "tZwnNdFNkNklYc3pVUzZINUV4eUtWVWFSVEf"], ["Pericles’ Funeral Oration", "politics", "sdgrewaNkNklYc3pVUzZINUV4eUtW345ufaZ"], ["The Pleasure of Books", "culture", "1234rFszdgrfYc3pVUzZINUV4eU43usacd"], ["I Am The First Accused (Nelson Mandela)", "law", "34rsgadOsidjSZIswjadi95uydnfklsdks"] ]; 
function submit(e){ 
    Logger.log("running submit(e)"); 
    var numberOfItems = e.parameter.checkbox_total; 
    var itemsSelected = []; 
    // for each item, if it is checked/selected, add it to itemsSelected 
    for(var i = 0; i < numberOfItems; i++){ 
    if(e.parameter['checkbox_isChecked_'+i] == 'true'){ 
     itemsSelected.push(e.parameter['checkbox_value_'+i]); 
    } 
    } 
    var app = UiApp.getActiveApplication(); 

    Logger.log("itemsSelected = " + itemsSelected); 

    ScriptProperties.setProperties({'theses': itemsSelected}, true); 


    var thesesArrays = ScriptProperties.getProperty('theses'); 
    Logger.log("thesesArrays = " + thesesArrays); 
    for (var i = 0; i < thesesArrays.lenght(); i++){ 
     var thesesId = ScriptProperties.getProperty('theses')[i][2]; 
     var thesesType = ScriptProperties.getProperty('theses')[i][1]; 
     importTheses(target, thesesId, thesesType); 
}  
    app.close(); 
    return app; 
} 

但是,當代碼獲取我得到了一個錯誤的行for (var i = 0; i < thesesArrays.lenght(); i++){,因爲thesesArrays是不是數組的數組(按照我的意思),而是一個沒有數組的方法。

我該如何解決這個問題?如何在ScriptProperties中存儲和檢索數組?

回答

0

在JavaScript中,你不能這樣做

array.length(); 

您需要刪除(),因爲長度()不存在!

編輯:

,如果它返回對象的數組的數組,你能做到這一點通過循環它的對象數組轉換爲特定對象的數組的函數,然後創建你的對象,並設置它的這樣的屬性例如:

this.name = object['name']; 
+0

正如@ZigMandel指出的,這個答案只是指出了一個小錯誤(包括錯誤拼寫錯誤)。更重要的是,'setProperties()'不支持除字符串以外的其他任何東西。此外,不再支持'ScriptProperties.setProperties()'及其附屬的靜態方法。 –

11

另一個答案只處理長度上的語法錯誤。但它仍然不能工作,因爲你只能在scriptProperties中存儲字符串。

1)json.stringify您的陣列來存儲它作爲字符串

2)不調用的getProperty一百萬次。在循環和json之前調用它一次。將其解壓縮到一個數組中。

+1

謝謝@Zig Mandel,您的評論非常有用! – craftApprentice

+0

我通常會認爲這是對所述問題的「正確」答案:爲了將數組存儲在Google App腳本屬性中,必須首先對其進行字符串化。謝謝@ zig-mandel。 – lilbyrdie

+0

是@craftapprentice不知道爲什麼你標記另一個正確 –