已输入正确ID和名称却找不到Google Form?求排查方案
问题根源分析
- 核心错误:误用
getItems(FormApp.ItemType.FORM)
你通过FormApp.openById()已经直接获取到了目标表单对象,但getItems(FormApp.ItemType.FORM)是用来提取表单内嵌套的子表单项,而非获取当前表单的标题。你的表单中没有嵌套子表单,导致forms数组为空,自然找不到名为"Comments"的表单,最终返回null。
修正后的代码
直接简化findFormByName函数,跳过无意义的子表单遍历,直接验证目标表单的标题:
function openForm() { var form = findFormByName('Comments'); if (form) { populateQuestions(form); } else { Logger.log('Form not found'); } } function findFormByName(formName) { // 直接打开指定ID的表单 var targetForm = FormApp.openById('1s0rjQIRY2i8CZsoxlu--TBR7q4b3wHxBslItWOMrc1s'); // 校验表单标题是否匹配 if (targetForm.getTitle() === formName) { return targetForm; } 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; }
额外排查点
若修正后仍提示找不到表单,检查以下内容:
- 确认表单ID
1s0rjQIRY2i8CZsoxlu--TBR7q4b3wHxBslItWOMrc1s完全正确,无多余空格或字符 - 确认表单标题与
'Comments'完全匹配,注意大小写、空格及特殊字符 - 检查脚本权限:首次运行时需授权,确保已授予脚本访问该表单的权限
内容的提问来源于stack exchange,提问作者Kolev_I_N
相关产品推荐
相关产品推荐

