GCP/Apps Script

Apps Script로 Google Forms 항목(item) 추가하기

whistory 2023. 6. 7. 14:26
반응형

 

 

Apps Script로 Google Sheets 를 위주로 글을 쓰다가,

Google Forms에 대해서도 한번 써보려고한다.

 

 

제공되는 Form class docs를 기반으로 작성해본다.

 

Class Form  |  Apps Script  |  Google for Developers

이 페이지는 Cloud Translation API를 통해 번역되었습니다. Switch to English 의견 보내기 Class Form 컬렉션을 사용해 정리하기 내 환경설정을 기준으로 콘텐츠를 저장하고 분류하세요. 양식 전체 속성 및

developers.google.com

 

Apps Script 로 Google Form 사용하기

1. 타이틀 설정하기

function setFormTitle() {
    const form = FormApp.getActiveForm();

    form.setTitle("Form Test Title").setDescription("Form Test Description");
}

 

2. 그리드 형태의 checkbox 선택

/*
 * 응답자가 체크박스 시퀀스에서 행당 여러 선택사항을 선택할 수 있는 열과 행의 그리드로 표시되는 새 질문 항목을 추가합니다.
 */
const item2 = form.addCheckboxGridItem();
// Sets the title 'Where did you celebrate New Year's?'
item2.setTitle('Where did you celebrate New Year');
// Sets the grid's rows and columns.
item2.setRows(['New York', 'San Francisco', 'London'])
     .setColumns(['2014', '2015', '2016', '2017']);

 

3. 단일 행 형태의 checkbox 선택

/*
 * 응답자가 하나 이상의 체크박스와 '기타' 필드(선택사항)를 선택할 수 있는 새 질문 항목을 추가합니다.
 */
const item3 = form.addCheckboxItem();
// Sets the title of the checkbox item to 'Do you prefer cats or dogs?'
item3.setTitle('Do you prefer cats or dogs?');
// Sets the choices.
item3.setChoiceValues(['Cats', 'Dogs']);

 

4. 날짜 입력

/*
 * 응답자가 날짜를 표시할 수 있도록 새로운 질문 항목을 추가합니다.
 */
const item4 = form.addDateItem();
// Sets the title to 'When were you born?'
item4.setTitle('When were you born?');
// Sets the description for the date item.
item4.setHelpText('Some helper text.');

 

5. 날짜 시분 입력

/*
 * 응답자가 날짜 및 시간을 나타낼 수 있는 새 질문 항목을 추가합니다.
 */
const item5 = form.addDateTimeItem();
// Sets the title to 'When were you born?'
item5 .setTitle('When were you born?');
// Sets the question as required.
item5 .setRequired(true);

 

6. 시분초 입력

/*
 * 응답자가 시간을 표시하는 데 사용할 수 있는 새로운 질문 항목을 추가합니다.
 */
const item6 = form.addDurationItem();
// Sets the title to 'How long can you hold your breath?'
item6.setTitle('How long can you hold your breath?');
// Sets the question as required.
item6.setRequired(true);

 

7. 그리드 형태의 라디오 버튼 선택

/*
 * 열과 행의 그리드로 표시되는 새 질문 항목을 추가하여 응답자가 일련의 라디오 버튼에서 행당 하나의 항목을 선택할 수 있도록 합니다.
 */
const item7 = form.addGridItem();
// Sets the title to 'Rate your interests.'
item7.setTitle('Rate your interests');
// Sets the grid's rows and columns.
item7.setRows(['Cars', 'Computers', 'Celebrities'])
     .setColumns(['Boring', 'So-so', 'Interesting']);

 

8. 단일 행 형태의 라디오 버튼 선택

/*
 * 응답자가 라디오 버튼 목록 또는 '기타' 필드(선택사항) 중 하나를 선택할 수 있는 새 질문 항목을 추가합니다.
 */
const item8 = form.addMultipleChoiceItem();
// Sets the title.
item8.setTitle('What is your favorite ice cream flavor?');
// Creates some choice items.
const vanilla = item8.createChoice('vanilla');
const chocolate = item8.createChoice('chocolate');
const strawberry = item8.createChoice('strawberry');
// Sets the choices.
item8.setChoices([vanilla, chocolate, strawberry]);

 

9. 드롭다운 선택

/*
 * 응답자가 드롭다운 목록에서 하나의 선택지를 선택할 수 있도록 새로운 질문 항목을 추가합니다.
 */
const item9 = form.addListItem();
// Sets the title to 'Do you prefer cats or dogs?'
item9.setTitle('Do you prefer cats or dogs?');
// Sets the description to 'This is description text...'
item9.setHelpText('This is description text...');
// Creates and adds choices to the dropdown list.
item9.setChoices([
    item9.createChoice('dog'),
    item9.createChoice('cat')
]);

10. 텍스트 영역 입력

/* 
 * 응답자가 텍스트 블록을 입력할 수 있는 새 질문 항목을 추가합니다.
 */
const item10 = form.addParagraphTextItem();
// Sets the title to 'What is your address?'
item10.setTitle('What is your address?');

 

11. 텍스트 입력

/*
 * 응답자가 한 줄의 텍스트를 입력할 수 있도록 새로운 질문 항목을 추가합니다.
 */
const item11 = form.addTextItem();
// Sets the title to 'What is your name?'
item11.setTitle('What is your name?');

 

12. 시간 입력

/*
 * 응답자가 하루 중 시간을 나타낼 수 있는 새 질문 항목을 추가합니다.
 */
const item12 = form.addTimeItem();
// Sets the title to 'What time do you usually wake up in the morning?'
item12.setTitle('What time do you usually wake up in the morning?');

 

13. 이미지 삽입

/*
 * 이미지를 표시하는 새 레이아웃 항목을 추가합니다.
 */
const item13 = form.addImageItem();
// Gets the Google icon to use as the image.
const img = UrlFetchApp.fetch('<https://img1.daumcdn.net/thumb/C428x428/?scode=mtistory2&fname=https%3A%2F%2Ftistory1.daumcdn.net%2Ftistory%2F5576974%2Fattach%2F2f8e51a2595d475899f25a1dd944137d>');
// Sets the image, title, and description for the item.
item13.setTitle('Google icon').setHelpText('Google icon').setImage(img);

반응형