Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
104 views
in Technique[技术] by (71.8m points)

setTitle() "sometimes" doesn't work. Google Form

Purpose:

I want to add a hundred items to Google Form from Google Spreadsheet.

Problem:

All the items and choices are safely added to the form but not "some" titles.

  1. When I excute the code, 70% of the titles are added but 30% of the titles fail.
  2. That 30% of failed titles varies on every execution. Even though the title adding was successful this time, it might fails next time. It's so random for me.
  3. There is no error code.

Hypothesis:

I am assuming that getTitles is unstable. Google Server fails sometimes. So that there is no way to control the stability. Am I wrong?

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...