我将正确的表单 ID 和名称添加到此应用程序脚本中:
function openForm() {
var form = findFormByName('Comments');
if (form) {
populateQuestions(form);
} else {
Logger.log('Form not found');
}
}
function findFormByName(formName) {
var forms = FormApp.openById('1s0rjQIRY2i8CZsoxlu--TBR7q4b3wHxBslItWOMrc1s').getItems(FormApp.ItemType.FORM);
for (var i = 0; i < forms.length; i++) {
var form = forms[i].asForm();
if (form.getTitle() === formName) {
return form;
}
}
return null;
}
function populateQuestions(form) {
var googleSheetsQuestions = getQuestionValues();
var itemsArray = form.getItems(FormApp.ItemType.LIST);
if (googleSheetsQuestions.length > 0) {
var choiceArrayA = removeDuplicates(googleSheetsQuestions.map(function(row) {
return row[0] ? String(row[0]).trim() : '';
}));
if (itemsArray.length > 0) {
var question1 = itemsArray[0].asListItem();
question1.setChoiceValues(choiceArrayA);
}
}
}
function removeDuplicates(array) {
return array.filter((value, index, self) => self.indexOf(value) === index);
}
function getQuestionValues() {
var ss = SpreadsheetApp.openById('1OK0EIoGRD8RdQ4gOeCqy6ycT5Y-MvLZYXBvQo2MpJpI');
var questionSheet = ss.getSheetByName('Raw Data');
var returnData = questionSheet.getRange(2, 1, questionSheet.getLastRow() - 1, 2).getValues();
return returnData;
}
知道我做错了什么吗?我在与其他函数相关的其余代码中添加了前两个代码块所引用的代码。
编辑:图片显示我实施的解决方案无济于事。
获取表单标题
您的代码发生的情况是您试图获取问题类型并将其与您假定的表单标题进行比较。我用您的表单标题部分代码创建了修复程序。
示例代码:
参考:
获取标题()