在下面的处理程序函数中,我正在尝试存储和获取名为“theses”的数组的值:
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
不是数组的数组(如我所愿),而是一个没有 lenght()
的对象方法。
我该如何解决?如何在 ScriptProperties 中存储和检索数组?
最佳答案
另一个答案只处理长度上的语法错误。但它仍然无法工作,因为您只能在 scriptProperties 中存储字符串。
1) json.stringify 将你的数组存储为字符串
2) 不要调用 getProperty 一百万次。在循环之前调用一次,json.parse 成一个数组。
关于javascript - 如何在 ScriptProperties 中存储和检索数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20567914/