我有一个谷歌表格,问题的输入基于谷歌表格。 googlesheet 有两张表“类别”和“项目”
Categories sheet:
Category Options
Salary Mike
Salary John
Travel Flight
Manintanence Water Supply
Manintanence Drain cleaning
Projects:
Proj1
Proj2
Proj3
表格中的第一个问题是“选择类别”。根据所选类别,下一个问题将显示相应“选项”的列表 例如:如果选择薪水,下一个问题将显示迈克和约翰之间的选择
接下来的问题应该是“选择项目”(显示项目栏中的项目列表)。
如果选择维护,则会显示供水和排水清洁 然后转到项目问题。
问题:如果选择“旅行”,一旦选择,它就会显示“航班”选项, 它再次显示“供水”和“排水沟清洁” 跳到项目问题
我有以下代码: 如果我选择最后一个类别,效果很好。但是,如果我选择第一个或第二个类别,则在跳转到“projectDetailsSection”之前,所有后续类别选项都会依次显示
const FORM_URL = "https://docs.google.com/forms/d/xxxxxxxxxxxx/edit"
let form=FormApp.openByUrl(FORM_URL)
function resetForm() {
let items=form.getItems()
items.forEach(item=>form.deleteItem(item))
}
function createConditionalForm(){
resetForm();
let ws = SpreadsheetApp.getActiveSpreadsheet();
let projectsSheet = ws.getSheetByName("Projects");
let projectsData = projectsSheet.getDataRange().getValues();
let categoriesSheet = ws.getSheetByName("Categories");
let categoriesData = categoriesSheet.getDataRange().getValues();
categoriesData.shift();
let categoryQuestion = form.addMultipleChoiceItem().setTitle("Category").setRequired(true);
let categoryOptions = [];
let categoryToSubcategories = new Map();
categoriesData.forEach(row => {
let category = row[0];
let subcategory = row[1];
if (!categoryToSubcategories.has(category)) {
categoryToSubcategories.set(category, []);
}
categoryToSubcategories.get(category).push(subcategory);
});
let firstSubcategorySection = null;
categoryToSubcategories.forEach((subcategories, category, index) => {
let section = form.addPageBreakItem().setTitle(category + " - Select a Subcategory");
if (index === 0) firstSubcategorySection = section; // Save the first subcategory section for navigation
let subcatQuestion = form.addMultipleChoiceItem().setTitle("Subcategory").setRequired(true).setChoiceValues(subcategories);
let catChoice = categoryQuestion.createChoice(category, section);
categoryOptions.push(catChoice);
});
categoryQuestion.setChoices(categoryOptions);
if (firstSubcategorySection) {
categoryQuestion.setGoToPageBreakItem(firstSubcategorySection)
}
let projectDetailsSection = form.addPageBreakItem().setTitle("Project Details");
let projectsQuestion = form.addListItem().setTitle("Project Name").setRequired(true).setChoiceValues(projectsData.map(row => row[0]));
let amountQuestion = form.addTextItem().setTitle("Amount").setRequired(true);
}
结合我上面的评论,
根据我的理解,用户选择子类别后,您希望他们被引导至生成的调查问卷的最后部分,即
Project Details
。
为此,您需要在
setGotoPage(FormApp.PageNavigationType.GO_TO_PAGE)
上使用 let section = form.addPageBreakItem().setTitle(category + " - Select a Subcategory");
,因为那时正在生成这些部分,它看起来像这样:
let section = form.addPageBreakItem().setGoToPage(FormApp.PageNavigationType.GO_TO_PAGE5).setTitle(category + " - Select a Subcategory");
但是,由于
PAGE5
或 Project Details
问题尚未创建,此逻辑将不起作用,这将导致代码错误。
相反,我建议更改问题的生成方式,您可以使用以下代码:
const FORM_URL = "https://docs.google.com/forms/d/xxxxxxxxxxxx/edit"
let form = FormApp.openByUrl(FORM_URL)
function resetForm() {
let items = form.getItems()
items.forEach(item => form.deleteItem(item))
}
function createConditionalForm() {
resetForm();
let ws = SpreadsheetApp.getActiveSpreadsheet();
let projectsSheet = ws.getSheetByName("Projects");
let projectsData = projectsSheet.getDataRange().getValues();
let categoriesSheet = ws.getSheetByName("Categories");
let categoriesData = categoriesSheet.getDataRange().getValues();
categoriesData.shift();
form.addListItem().setTitle("Project Name").setRequired(true).setChoiceValues(projectsData.map(row => row[0]));
form.addTextItem().setTitle("Amount").setRequired(true);
let categoryQuestion = form.addMultipleChoiceItem().setTitle("Category").setRequired(true);
let categoryOptions = [];
let categoryToSubcategories = new Map();
categoriesData.forEach(row => {
let category = row[0];
let subcategory = row[1];
if (!categoryToSubcategories.has(category)) {
categoryToSubcategories.set(category, []);
}
categoryToSubcategories.get(category).push(subcategory);
});
categoryToSubcategories.forEach((subcategories, category, index) => {
let section = form.addPageBreakItem().setGoToPage(FormApp.PageNavigationType.SUBMIT).setTitle(category + " - Select a Subcategory");
if (index === 0) firstSubcategorySection = section;
form.addMultipleChoiceItem().setTitle("Subcategory").setRequired(true).setChoiceValues(subcategories);
let catChoice = categoryQuestion.createChoice(category, section);
categoryOptions.push(catChoice);
});
categoryQuestion.setChoices(categoryOptions);
}
这会改变您的问题创建方式的逻辑。
这解决了子类别问题,
将与Project Details
一起询问。Category