Created
February 17, 2025 01:57
-
-
Save totuworld/2aeee46db4bf9a0cf585ce6ce47f833e to your computer and use it in GitHub Desktop.
create_form_from_sheet.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function onOpen() { | |
// 스프레드시트 열릴 때 사용자 정의 메뉴 추가 | |
const ui = SpreadsheetApp.getUi(); | |
ui.createMenu('나의 메뉴') // "나의 메뉴"라는 사용자 메뉴 생성 | |
.addItem('설문지 만들기', 'createFormFromSheet') // "설문지 만들기" 버튼 추가 | |
.addToUi(); | |
} | |
function createFormFromSheet() { | |
const ui = SpreadsheetApp.getUi(); | |
const spreadSheet = SpreadsheetApp.getActiveSpreadsheet(); | |
const sheet = spreadSheet.getSheets()[0]; // 첫 번째 시트를 사용 | |
const range = sheet.getDataRange(); // 데이터가 있는 전체 범위를 가져옴 | |
const values = range.getValues(); // 모든 데이터를 2차원 배열로 가져옴 | |
// 설문지 생성 중 메시지 표시 | |
const loadingMessage = ui.alert('알림', '설문지를 만드는 중입니다...', ui.ButtonSet.OK); | |
try { | |
const form = FormApp.create('새로운 설문지'); // 새로운 구글 폼 생성 | |
for (let i = 1; i < values.length; i++) { | |
const type = values[i][0]; // 설문 타입 열 값 가져오기 | |
const required = values[i][1]; // 필수 여부 열 값 가져오기 | |
const question = values[i][2]; // 질문 열 값 가져오기 | |
const options = values[i].slice(3); // 선택지 내용 가져오기 (3번째 열부터 나머지) | |
let item; // 설문지 항목을 결정하기 위한 변수 | |
switch (type) { // 설문 타입에 따라 구글 폼 항목 설정 | |
case 'MultipleChoiceItem': | |
item = form.addMultipleChoiceItem(); | |
item.setChoiceValues(options.filter(String)); // 빈 문자열 제거 | |
break; | |
case 'TextItem': | |
item = form.addTextItem(); | |
break; | |
case 'ParagraphTextItem': | |
item = form.addParagraphTextItem(); | |
break; | |
case 'CheckboxItem': | |
item = form.addCheckboxItem(); | |
item.setChoiceValues(options.filter(String)); // 빈 문자열 제거 | |
break; | |
case 'SectionHeaderItem': | |
form.addSectionHeaderItem().setTitle(question); | |
continue; // 다음 반복으로 넘어감 | |
} | |
if (item) { | |
item.setTitle(question).setRequired(required === 'Y'); // 질문 및 필수 여부 설정 | |
} | |
} | |
// 설문지 생성 완료 메시지 표시 | |
ui.alert('알림', '설문지를 만들었습니다!', ui.ButtonSet.OK); | |
} catch (error) { | |
// 오류가 발생하면 오류 메시지 표시 | |
ui.alert('오류', `설문지를 만드는 중 문제가 발생했습니다: ${error.message}`, ui.ButtonSet.OK); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment