Last active
January 9, 2020 03:08
-
-
Save ttsukagoshi/990450fb6f1aef6552d4a2ab70ed427c to your computer and use it in GitHub Desktop.
GoogleフォームにバインドされたGoogle App Script (GAS)。ラジオボタン型の設問に対する選択肢を、一人が選択してフォーム送信したときに、以降の回答者からはその選択肢が見えないようにする。Google Form-bound app script for making a form with dynamic choices, i.e., when one respondent of the form selects a choice in a radio button-type question, that choice is deleted for later respondents.
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
//元の選択肢。 | |
var originalChoice = [ | |
'Choice1', | |
'Choice2', | |
'etc.' | |
]; | |
// 設定 | |
var numQuestion = 2; // 動的に変化させる質問の番号。配列なので0からカウント。0, 1, 2, ... | |
var textQuestion = 'Title of Item (form question)'; // 動的に変化させる質問のテキスト | |
var myEmail = Session.getActiveUser().getEmail(); | |
/** | |
* 回答者がフォーム提出時に実行される | |
* set trigger to execute this function onFormSubmit | |
*/ | |
function modifyChoice(event) { | |
// フォームの質問一覧 | |
var sourceFormItems = event.source.getItems(); | |
// 動的に変化させたい選択肢。質問(Item)IDと選択肢一覧(配列) | |
var choiceId = sourceFormItems[numQuestion].asMultipleChoiceItem().getId(); | |
var openChoices = sourceFormItems[numQuestion].asMultipleChoiceItem().getChoices(); | |
var openChoiceArray = []; | |
for (var i = 0; i < openChoices.length; i++) { | |
var openChoice = openChoices[i]; | |
openChoiceArray.push(openChoice.getValue()); | |
} | |
// 送信された回答 | |
var itemResponses = event.response.getItemResponses(); | |
// 回答の連想配列を作成 | |
var response = {}; | |
for (var i = 0; i < itemResponses.length; i++) { | |
var itemResponse = itemResponses[i]; | |
var question = itemResponse.getItem().getTitle(); | |
var answer = itemResponse.getResponse(); | |
response[question] = answer; | |
} | |
// 今回必要な回答 | |
var name = response['Name']; | |
var email = response['Email']; | |
var timeSlot = response[textQuestion]; | |
// 選択肢を編集する | |
if (timeSlot == 'None of above' || timeSlot == '変化させない選択肢' || timeSlot == '変化させない選択肢2') { | |
sourceFormItems[numQuestion].asMultipleChoiceItem().setChoiceValues(openChoiceArray) | |
} else { | |
openChoiceArray = openChoiceArray.filter(function (value){return value !== timeSlot}); | |
sourceFormItems[numQuestion].asMultipleChoiceItem().setChoiceValues(openChoiceArray); | |
} | |
// メール送信用のテキスト | |
// フォーム送信者へのメール | |
var thankYouText = | |
'Dear ' + name + ',\n\n' | |
+ 'Thank you for your cooperation.\n' | |
+ 'The time slot you chose is:\n' | |
+ timeSlot + '\n\n' | |
+ 'Please come to VenueA by the above time.\nWe will contact you again if you chose "変化させない選択肢", "変化させない選択肢2", or "None of above."\n\n' | |
+ 'If you have any questions, please contact us at our@email\n\n' | |
+ 'Best regards,\n' | |
+ 'hogehoge'; | |
// 担当者へのメール | |
var confirmText = '申し込みがありました。\nName: ' + name + '\nEmail: ' + email + '\nTime Slot: ' + timeSlot + '\n\n重複の有無を念のため、確認してください。\n[Spreadsheet URL]'; | |
// メール送信 | |
MailApp.sendEmail(email, 'Time Confirmed', thankYouText); | |
MailApp.sendEmail(myEmail, '申込みがありました', confirmText); | |
} | |
// 元の選択肢を復元したい時に実行 | |
function return2original() { | |
var form = FormApp.getActiveForm(); | |
var items = form.getItems(); | |
var itemId = items[numQuestion].getId(); | |
form.getItemById(itemId).asMultipleChoiceItem().setChoiceValues(originalChoice); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment