gpt4 book ai didi

javascript - 为什么这种从 `e.parameter.variableName` 方法恢复数组的尝试不起作用?

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:33:10 25 4
gpt4 key购买 nike

这段代码出了什么问题?当我在 sumbit 函数之前记录数组列表时,它没问题(具有预期的内容和元素在数组中的位置)。但是,当我通过 e.parameter.arrayList 获取它时,它没有相同的值,也不是元素。如何解决?

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();
}

最佳答案

您不能简单地将一个数组传递到表单字段并取回该数组的值。这与您在 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);

关于javascript - 为什么这种从 `e.parameter.variableName` 方法恢复数组的尝试不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27974631/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com