Last active
April 27, 2023 01:22
-
-
Save Tomokatsu-Sakamoto/a0cd9a7257291a9a021b670e0be55caf to your computer and use it in GitHub Desktop.
This file contains 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
"use strict"; // 変数の宣言を強要 | |
const START_ROW = 12; // 班毎の情報が開始される行番号 | |
function onOpen() { | |
var sheet = SpreadsheetApp.getActiveSpreadsheet(); | |
var menuItems = [ | |
{ name: '班毎のフォームを作成', functionName: 'createForm' } | |
]; | |
sheet.addMenu('振り返りフォーム', menuItems); | |
} | |
/*** | |
* 班毎の振り返りフォームを作成する | |
*/ | |
function createForm() { | |
const ss = SpreadsheetApp.getActiveSpreadsheet(); // スプレッドシートの Class を取得 | |
const sheet = SpreadsheetApp.getActiveSheet(); // シートの Class を取得 | |
// スプレッドシート設定された班の情報を読み込む | |
let value = sheet.getRange(START_ROW, 1, sheet.getLastRow() - START_ROW + 1, sheet.getMaxColumns()).getValues(); | |
let formTitle = sheet.getRange('B1').getValue(); // フォームのタイトル | |
const formDesc = sheet.getRange('B2').getValue(); // フォームの冒頭の説明 | |
const nameAdd = sheet.getRange('B3').getValue(); // 設問のタイトルに付加する敬称 | |
const qDesc = sheet.getRange('B4').getValue(); // 各設問の説明 | |
const bCollect = sheet.getRange('B5').getValue(); // メールアドレスを収集する | |
const bRequireLogin = sheet.getRange('B6').getValue(); // ログインが必須 | |
let iMode = 0; // フォームの回答モード(0:記述、1:ラジオボタン、2:チェックボックス) | |
if (sheet.getRange('B7').getValue() == true) { | |
iMode = 1; // 1:ラジオボタン | |
} | |
else if (sheet.getRange('B8').getValue() == true) { | |
iMode = 2; // 2:チェックボックス | |
} | |
let iNeed = sheet.getRange('B9').getValue(); // 回答を必須にするか? | |
if (formTitle == '') { // フォームのタイトルが設定されていない場合には、デフォルト値を設定する | |
formTitle = '振り返りフォーム'; | |
} | |
for (let row = 0; row < value.length; row++) { // 班毎の処理を行うループ | |
if (value[row][0] != '') { | |
// 班毎のフォームを新規作成する | |
const formName = formTitle + ' - ' + value[row][0]; | |
const form = FormApp.create(formName); // フォームを新規作成 | |
console.info('[' + row + '] ' + formName); | |
// 作成したフォームは、このスプレッドシートと同じフォルダに移動させる | |
// 参考: https://tetsuooo.net/gas/748/ | |
// 参考: https://e-work-education.com/2021/12/11/filesmove/ | |
const sheetFile = DriveApp.getFolderById(ss.getId()); // このスプレッドシートのファイル ID を取得 | |
const sheetFolderID = sheetFile.getParents().next().getId(); // このスプレッドシートが保存されているフォルダの ID を取得 | |
const sheetFolder = DriveApp.getFolderById(sheetFolderID); // 移動先となるフォルダの Folder クラスを取得 | |
const formFile = DriveApp.getFileById(form.getId()); // 作成したフォームの File クラスを取得 | |
formFile.moveTo(sheetFolder); // ファイルが保存されているフォルダを移動させる | |
form.setCollectEmail(bCollect); // フォームが回答者の電子メール アドレスを収集するかどうかを設定します。 | |
try { | |
form.setRequireLogin(bRequireLogin); // 回答者が回答する前に、ログインする必要があるかどうかを設定します。 | |
} | |
catch (e) { | |
// Google Workspace 環境でなければ、setRequireLogin() は利用できないので例外が発生する。仕方がないことなので、無視する… | |
console.log(e); | |
} | |
if (formDesc != '') { | |
form.setDescription(formDesc); // フォームに説明を追加する | |
} | |
let cItem = []; // 記述ではない場合の選択肢 | |
let choice = ''; | |
if (iMode != 0) { | |
let i = 3; | |
do { | |
choice = sheet.getRange(6 + iMode, i).getValue(); | |
if (choice != '') { | |
cItem.push(choice); | |
} | |
i++; | |
} while (choice != ''); | |
} | |
for (let col = 4; col < value[0].length; col++) { | |
if (value[row][col] != '') { | |
let item; // 回答のためのアイテム | |
switch (iMode) { | |
default: | |
case 0: // 0:記述 | |
item = form.addParagraphTextItem(); | |
break; | |
case 1: // 1:ラジオボタン | |
item = form.addMultipleChoiceItem(); | |
item.setChoiceValues(cItem); | |
break; | |
case 2: // 2:チェックボックス | |
item = form.addCheckboxItem(); | |
item.setChoiceValues(cItem); | |
break; | |
} | |
item.setTitle(value[row][col] + nameAdd); // それぞれの設問のタイトルをメンバー名から設定 | |
if (qDesc != '') { | |
item.setHelpText(qDesc); // 設定されていれば、それぞれの設問に説明を設定する | |
} | |
item.setRequired(iNeed); // 回答が必須かどうかを設定する | |
} | |
} | |
sheet.getRange(START_ROW + row, 2).setValue(formFile.getName()); | |
sheet.getRange(START_ROW + row, 3).setValue(form.getPublishedUrl()); | |
sheet.getRange(START_ROW + row, 4).setValue(form.getEditUrl()); | |
} | |
else { // 班名が設定されていない場合は対象外とする | |
console.info('「班名」が設定されていない行は無視します。'); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment