function testLoop() {
//* Definition of Form & Sheet. *//
const testform4 = FormApp.openById('1gp3M8wsXHNdbEBstaT5B86XmiMoSM43aSwdi8zSC5qU');
const testsheet = SpreadsheetApp.openById('1rWNVZ8nzdXdCxFtGHe2_NxzkXyxurazfjU8pWYPhmKQ')
.getSheetByName('testsheet1');
// Add Items
for (let i = 3; i < 104; i++) {
//* Sheet to Form *// // REFACTOR:
// Get the item type.
let itemType = testsheet.getRange(i, 2).getValue();
//* Add items *//
// Add section. //
if (itemType == "addPageBreakItem") {
// Set an Item-Object.
let itemObject = testform4.addPageBreakItem();
// Get the Title from Sheet.
let itemTitle = testsheet.getRange(i, 1).getValue();
// Set the title.
// @ts-ignore
itemObject.setTitle(itemTitle);
console.log('- The item, "' + itemTitle + '": ' + itemType + ', is added.')
}
// Add description.
else if (itemType == "addSectionHeaderItem") {
// Set an Item-Object.
let itemObject = testform4.addSectionHeaderItem();
// Get the Title from Sheet.
let itemTitle = testsheet.getRange(i, 1).getValue();
//FIX: It fales to get the value.
// Set the title.
// @ts-ignore
itemObject.setTitle(itemTitle);
console.log('- The item, "' + itemTitle + '": ' + itemType + ', is added.')
}
// Add short-answer.
else if (itemType == "addTextItem") {
// Set an Item-Object.
let itemObject = testform4.addTextItem();
// Get the Title from Sheet.
let itemTitle = testsheet.getRange(i, 1).getValue();
// Set the title.
// @ts-ignore
itemObject.setTitle(itemTitle);
// require number
// Check if it requires number.
const requireNumberCheck = testsheet.getRange(i, 3).getValue();
if (requireNumberCheck == 1) {
let textValidation = FormApp
.createTextValidation()
.requireNumber()
.setHelpText('Please input the number')
.build();
itemObject.setValidation(textValidation);
console.log("requireNumber is TRUE.");
}
console.log('- The item, "' + itemTitle + '": ' + itemType + ', is added.')
}
// Add multiple choice.
else if (itemType == "addMultipleChoiceItem") {
// Set an Item-Object.
// @ts-ignore
let itemObject = testform4.addMultipleChoiceItem();
// Get the Title from Sheet.
let itemTitle = testsheet.getRange(i, 1).getValue();
// Set the title.
// @ts-ignore
itemObject.setTitle(itemTitle);
// Add choices. // MEMO: Inside square brackets loop didn't work.
itemObject.setChoices([
// @ts-ignore
itemObject.createChoice(testsheet.getRange(i, 5).getValue()),
// @ts-ignore
itemObject.createChoice(testsheet.getRange(i, 6).getValue()),
// @ts-ignore
itemObject.createChoice(testsheet.getRange(i, 7).getValue()),
// @ts-ignore
itemObject.createChoice(testsheet.getRange(i, 8).getValue()),
// @ts-ignore
itemObject.createChoice(testsheet.getRange(i, 9).getValue()),
// @ts-ignore
itemObject.createChoice(testsheet.getRange(i, 10).getValue()),
// @ts-ignore
itemObject.createChoice(testsheet.getRange(i, 11).getValue())]);
// Add other option if the other option is true.
if (testsheet.getRange(i, 11).getValue() == 4) {
itemObject.showOtherOption(true);
console.log('Other-Option is added.');
}
console.log('- The item, "' + itemTitle + '": ' + itemType + ', is added.')
}
// Add checkbox.
else if (itemType == "addCheckboxItem") {
// Set an Item-Object.
let itemObject = testform4.addCheckboxItem();
// Get the Title from Sheet.
let itemTitle = testsheet.getRange(i, 1).getValue();
// Set the title.
// @ts-ignore
itemObject.setTitle(itemTitle);
// Add choices. // MEMO: Inside square brackets loop didn't work.
itemObject.setChoices([
// @ts-ignore
itemObject.createChoice(testsheet.getRange(i, 5).getValue()),
// @ts-ignore
itemObject.createChoice(testsheet.getRange(i, 6).getValue()),
// @ts-ignore
itemObject.createChoice(testsheet.getRange(i, 7).getValue()),
// @ts-ignore
itemObject.createChoice(testsheet.getRange(i, 8).getValue()),
// @ts-ignore
itemObject.createChoice(testsheet.getRange(i, 9).getValue()),
// @ts-ignore
itemObject.createChoice(testsheet.getRange(i, 10).getValue()),
// @ts-ignore
itemObject.createChoice(testsheet.getRange(i, 11).getValue())]);
// Add other option if the other option is true.
if (testsheet.getRange(i, 11).getValue() == 4) {
itemObject.showOtherOption(true);
console.log('Other-Option is added.');
}
console.log('- The item, "' + itemTitle + '": ' + itemType + ', is added.')
}
// Add date.
else {
// Set an Item-Object.
let itemObject = testform4.addDateItem();
// Get the Title from Sheet.
let itemTitle = testsheet.getRange(i, 1).getValue();
// Set the title.
// @ts-ignore
itemObject.setTitle(itemTitle);
console.log('- The item, "' + itemTitle + '": ' + itemType + ', is added.')
}
}
console.log("All the items are added.");
}
question from:
https://stackoverflow.com/questions/65899142/settitle-sometimes-doesnt-work-google-form