Skip to content

Instantly share code, notes, and snippets.

@totuworld
Last active May 11, 2025 07:24
Show Gist options
  • Save totuworld/30062ee9fa6dea2f7aafce270e0727f6 to your computer and use it in GitHub Desktop.
Save totuworld/30062ee9fa6dea2f7aafce270e0727f6 to your computer and use it in GitHub Desktop.
분기 있는 설문
function onOpen() {
// 스프레드시트 열릴 때 사용자 정의 메뉴 추가
const ui = SpreadsheetApp.getUi();
ui.createMenu('나의 메뉴') // "나의 메뉴"라는 사용자 메뉴 생성
.addItem('설문지 만들기', 'createFormFromSheet') // "설문지 만들기" 버튼 추가
.addToUi();
}
function createFormFromSheet() {
const ui = SpreadsheetApp.getUi();
const ss = SpreadsheetApp.getActiveSpreadsheet();
const sheet = ss.getSheets()[0];
const data = sheet.getDataRange().getValues();
const form = FormApp.create("멘토링 신청 폼4");
const sectionMap = {}; // 섹션 이름 -> PageBreakItem or null
let currentSection = null;
let branchItem = null;``
const jobSectionRoutes = {}; // 직군 -> 섹션 페이지
// 1차 처리: 공통 질문 먼저 추가
for (let i = 1; i < data.length; i++) {
const [section, type, required, question, ...rawOptions] = data[i];
if (section !== "공통") continue;
const options = rawOptions.filter(v => v !== '' && v != null);
let item = null;
switch (type) {
case "TextItem":
item = form.addTextItem().setTitle(question);
break;
case "ParagraphTextItem":
item = form.addParagraphTextItem().setTitle(question);
break;
case "CheckboxItem":
item = form.addCheckboxItem().setTitle(question).setChoiceValues(options);
break;
case "MultipleChoiceItem":
item = form.addMultipleChoiceItem().setTitle(question);
if (question.includes("직군")) {
branchItem = item;
}
item.setChoiceValues(options);
break;
default:
continue;
}
if (item && required === "Y") {
item.setRequired(true);
}
}
// 2차 처리: 섹션별 질문 및 섹션 생성 (공통후속 제외)
for (let i = 1; i < data.length; i++) {
const [section, type, required, question, ...rawOptions] = data[i];
const options = rawOptions.filter(v => v !== '' && v != null);
if (section === "공통" || section === "공통후속") continue;
// 섹션 처리
if (!sectionMap[section]) {
const page = form.addPageBreakItem().setTitle(`${section} 섹션`);
sectionMap[section] = page;
jobSectionRoutes[section] = page;
}
currentSection = sectionMap[section];
if (type === "goEnd") continue;
let item = null;
switch (type) {
case "TextItem":
item = form.addTextItem().setTitle(question);
break;
case "ParagraphTextItem":
item = form.addParagraphTextItem().setTitle(question);
break;
case "CheckboxItem":
item = form.addCheckboxItem().setTitle(question).setChoiceValues(options);
break;
case "MultipleChoiceItem":
item = form.addMultipleChoiceItem().setTitle(question).setChoiceValues(options);
break;
default:
continue;
}
if (item && required === "Y") {
item.setRequired(true);
}
}
// 3차 처리: 공통후속 섹션 생성 및 질문 추가
const endSection = form.addPageBreakItem().setTitle("공통 후속 질문");
sectionMap["공통후속"] = endSection;
for (let i = 1; i < data.length; i++) {
const [section, type, required, question, ...rawOptions] = data[i];
if (section !== "공통후속" || type === "goEnd") continue;
const options = rawOptions.filter(v => v !== '' && v != null);
let item = null;
switch (type) {
case "TextItem":
item = form.addTextItem().setTitle(question);
break;
case "ParagraphTextItem":
item = form.addParagraphTextItem().setTitle(question);
break;
case "CheckboxItem":
item = form.addCheckboxItem().setTitle(question).setChoiceValues(options);
break;
case "MultipleChoiceItem":
item = form.addMultipleChoiceItem().setTitle(question).setChoiceValues(options);
break;
default:
continue;
}
if (item && required === "Y") {
item.setRequired(true);
}
}
// 4차 처리: 직군 선택지에 따라 각 섹션으로 분기 연결
if (branchItem) {
const choices = branchItem.getChoices().map(choice => {
const targetPage = jobSectionRoutes[choice.getValue()] || endSection;
return branchItem.createChoice(choice.getValue(), targetPage);
});
branchItem.setChoices(choices);
}
// 5차 처리: goEnd가 붙은 섹션을 공통후속으로 연결
for (let i = 1; i < data.length; i++) {
const [section, type] = data[i];
if (type !== "goEnd") continue;
if (section === "공통" || section === "공통후속") continue;
const sec = sectionMap[section];
if (sec && typeof sec.setGoToPage === 'function') {
sec.setGoToPage(endSection);
}
}
ui.alert("설문지를 성공적으로 생성했습니다.");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment