function createForm() {
const ss = SpreadsheetApp.getActiveSpreadsheet(); //スプレッドシート
const ssId = ss.getId(); // スプレッドシートIDを取得
const parentFolder = DriveApp.getFileById(ssId).getParents(); // IDからスプレッドシートのファイルを取得⇒親フォルダを取得
const folderId = parentFolder.next().getId(); // 親フォルダIDを取得
const sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
// フォームのタイトルをB1から取得
const formTitle = sheet.getRange('B1').getDisplayValue();
// フォームの説明をB2から取得
const formDescription = sheet.getRange('B2').getDisplayValue();
const firstRow = 5;
const lastRow = sheet.getLastRow();
const dataRows = lastRow - (firstRow - 1);
//質問の選択肢は、N列(14)までの最大10個
var questionList = sheet.getRange(firstRow, 1, dataRows, 14).getDisplayValues();
questionList = questionList.map(question => {
return {
title: question[0], //質問のタイトル
helpText: question[1], //質問の説明
type: question[2], //回答の種類
must: question[3], //必須かどうか
choices: [question[4], question[5], question[6], question[7], question[8],question[9],question[10],question[11],question[12],question[13]]
}
});
//フォームのタイトルを設定
const form = FormApp.create(formTitle);
//フォームの説明を設定
form.setDescription(formDescription);
//グリッド用データ保存場所
let gridRows = [];
let gridColumns = [];
let gridContent = gridColumns.length;
let gridQ = 0 ;
let gTitle = '';
let gHelpText = '';
let gTrue = '';
//以下、質問の種類による分岐
questionList.forEach(question => {
//グリッド要素
if (question.type == '選択式(グリッド)') {
console.log('選択式(グリッド)');
gridQ = 1;
gTitle = question.title;
gHelpText = question.helpText;
gTrue = question.must;
question.choices.forEach((choice) => {
if (choice != '') {
gridColumns.push(choice);
};
gridContent = gridColumns.length;
});
}
else if (question.type == 'チェックボックス(グリッド)') {
gridQ = 2;
gTitle = question.title;
gHelpText = question.helpText;
gTrue = question.must;
question.choices.forEach((choice) => {
if (choice != '') {
gridColumns.push(choice);
};
gridContent = gridColumns.length;
});
}
else if (gridContent > 0){ //どちらかのグリッド形式だったら
if (gridQ == 1 ){
var choiceItem = form.addGridItem();
} else if (gridQ == 2){
var choiceItem = form.addCheckboxGridItem();
}
question.choices.forEach((choice) => {
if (choice != '') {
gridRows.push(choice);
}
});
choiceItem.setTitle(gTitle)
.setHelpText(gHelpText)
.setRows(gridRows)
.setColumns(gridColumns);
if (gTrue =='必須'){
choiceItem.setRequired(true)
}
//グリッド要素を初期化
gridRows = [];
gridColumns = [];
gridQ = 0;
gridContent = gridRows.length;
}
else if (question.type == '記述式'){
console.log('記述式')
var choiceItem = form.addTextItem();
choiceItem.setTitle(question.title)
.setHelpText(question.helpText)
if (question.must =='必須'){
choiceItem.setRequired(true)
}
}
else if (question.type == '段落'){
console.log('段落')
var choiceItem = form.addParagraphTextItem();
choiceItem.setTitle(question.title)
.setHelpText(question.helpText)
if (question.must =='必須'){
choiceItem.setRequired(true)
}
}
else if (question.type == 'ラジオボタン') {
console.log('ラジオボタン')
var choiceItem = form.addMultipleChoiceItem();
var choiceList = [];
question.choices.forEach((choice) => {
if (choice != '') {
let choiceObj = choiceItem.createChoice(choice);
choiceList.push(choiceObj);
}
});
choiceItem.setTitle(question.title)
.setHelpText(question.helpText)
.setChoices(choiceList);
if (question.must =='必須'){
choiceItem.setRequired(true)
};
}
else if (question.type == 'チェックボックス') {
console.log('チェックボックス')
var choiceItem = form.addCheckboxItem();
var choiceList = [];
question.choices.forEach((choice) => {
if (choice != '') {
let choiceObj = choiceItem.createChoice(choice);
choiceList.push(choiceObj);
}
});
choiceItem.setTitle(question.title)
.setHelpText(question.helpText)
.setChoices(choiceList);
if (question.must =='必須'){
choiceItem.setRequired(true)
};
}
else if (question.type == 'プルダウン') {
console.log('プルダウン')
var choiceItem = form.addListItem();
var choiceList = [];
question.choices.forEach((choice) => {
if (choice != '') {
let choiceObj = choiceItem.createChoice(choice);
choiceList.push(choiceObj);
}
});
choiceItem.setTitle(question.title)
.setHelpText(question.helpText)
.setChoices(choiceList);
if (question.must =='必須'){
choiceItem.setRequired(true)
};
}
else if (question.type == '均等目盛') {
console.log('均等目盛')
var choiceItem = form.addScaleItem();
var choiceList = [];
question.choices.forEach((choice) => {
if (choice != '') {
choiceList.push(choice);
}
});
choiceItem.setTitle(question.title)
.setHelpText(question.helpText)
.setBounds(choiceList[0],choiceList[1])
.setLabels(choiceList[2],choiceList[3]);
if (question.must =='必須'){
choiceItem.setRequired(true)
};
}
else if (question.type == '日付'){
console.log('日付')
var choiceItem = form.addDateItem();
choiceItem.setTitle(question.title)
.setHelpText(question.helpText)
if (question.must =='必須'){
choiceItem.setRequired(true)
}
}
else if (question.type == '時刻'){
console.log('時刻')
var choiceItem = form.addTimeItem();
choiceItem.setTitle(question.title)
.setHelpText(question.helpText)
if (question.must =='必須'){
choiceItem.setRequired(true)
}
}
else if (question.type == '日時'){
console.log('日時')
var choiceItem = form.addDateTimeItem();
choiceItem.setTitle(question.title)
.setHelpText(question.helpText)
if (question.must =='必須'){
choiceItem.setRequired(true)
}
}
else{
Browser.msgBox('「'+ question.title +'」は作成できない問題形式です');
}
});
//作成したフォームのアドレスをB3セルに入力
sheet.getRange('B3').setValue(form.getEditUrl());
Browser.msgBox('「' + formTitle + '」の作成が完了しました');
//作成したフォームをスプレッドシートのフォルダに移動
const formFile = DriveApp.getFileById(form.getId())
DriveApp.getFolderById(folderId).addFile(formFile)
// 作成したフォームを開く
var url = form.getPublishedUrl();
var script = "<script>window.open('" + url + "', '_blank').focus()</script>";
var html = HtmlService.createHtmlOutput(script);
SpreadsheetApp.getUi().showModalDialog(html, '作成したフォームを開きます ' );
// 通常のURLをログに表示
console.log('URL= ' + url);
// 短縮したURLをログに表示
console.log('短縮URL= ' + form.shortenFormUrl(url));
}
Sinxe thhe admin off thi wweb page iis working, nno hesitatjon very rapidly iit willl bbe
renowned, due to its quawlity contents.
Permissions have been changed.
Thanks for letting me know about my mistake!