我正在尝试在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);
}
似乎都不起作用。有什么想法吗?
答案 0 :(得分:2)
如果我的理解是正确的,那么该修改如何?在此修改中,您的脚本已修改。
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 = [];
。如果我误解了您的问题,而这不是您想要的结果,我深表歉意。
答案 1 :(得分:0)
我如何解决
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());
}```