我正在尝试在Google表单的复选框中添加新选项。我试图拥有与我选择的文件一样多的复选框。
function listFilesInFolder() {
var folderName = "Box A";
var folder = DriveApp.getFolderById('some id');
var contents = folder.getFiles();
var list = [];
list.push(['Name','ID','Size']);
var files = folder.getFiles();
while (files.hasNext()) {
file = files.next();
//spread sheet info if needed
var row = []
row.push(file.getName(), file.getId(), file.getSize())
list.push(row);
var name = file.getName();
AddToDropDown(name);
console.log(name)
}
}
function AddToDropDown(name) {
var form = FormApp.openById('some id');
var items = form.getItems();
var ImageSelection = items[7].asCheckboxItem();
ImageSelection.setChoices([
ImageSelection.createChoice(ImageSelection.getChoices().toString()),
ImageSelection.createChoice(name),
])
}
我已经尝试过这种方式,以及
function AddToDropDown(name) {
var form = FormApp.openById('some id');
var items = form.getItems();
var ImageSelection = items[7].asCheckboxItem();
ImageSelection.createChoice(name);
}
似乎都没有用。有什么想法吗?
如果我的理解是正确的,那么这个修改怎么样?在此修改中,您的脚本已被修改。
createChoice()
和检索到的文件名创建“选择”对象。setChoices()
设置“选择”对象。function addfileselect() {
var folder = DriveApp.getFolderById('1--5CnFK5DtptuO1bUu6MsA9oTknr-Fw2');
var files = folder.getFiles();
var names = [];
while (files.hasNext()){
var file = files.next();
names.push(file.getName());
}
var form = FormApp.openById('1gcktQP5bKxCtAvtlgz4d9x7zI6M4koRmZkkjgefswIg');
var items = form.getItems();
var ImageSelection = items[7].asCheckboxItem();
var choices = ImageSelection.getChoices();
for (var i = 0; i < names.length; i++) {
var choice = ImageSelection.createChoice(names[i]);
choices.push(choice);
}
ImageSelection.setChoices(choices);
}
var choices = ImageSelection.getChoices();
替换为var choices = [];
。如果我误解了你的问题并且这不是你想要的结果,我道歉。
我是怎么解决的
var folder = DriveApp.getFolderById('1--5CnFK5DtptuO1bUu6MsA9oTknr-Fw2');
contents = folder.getFiles();
var list = [];
var files = folder.getFiles();
while (files.hasNext()){
file = files.next();
list.push(file.getName());
var name = file.getName();
//console.log(name)
}
AddToDropDown(list);
}
function AddToDropDown(row) {
console.log(row.length)
var form = FormApp.openById('1gcktQP5bKxCtAvtlgz4d9x7zI6M4koRmZkkjgefswIg');
var items = form.getItems();
var ImageSelection = items[0].asCheckboxItem();
ImageSelection.setChoiceValues(row.sort());
}```