如何使用 Google Apps 脚本在 Google 表单中添加选项

How can I add choices in a Google Form with Google Apps Script

我正在尝试掌握以编程方式创建 Google 表单,但无法将选择分配给 multiple-choice 项目。我可以创建项目 (testQuestion) 并给它一个标题,但我的 createChoice() 语句不添加选项。

这是我的代码,基于 https://developers.google.com/apps-script/reference/forms/page-navigation-type

function testCreateChoices() {

/* modification of
  testPageNavigation()
  to see how to import choices from a range on sheet
*/

// Create a form 
var form = FormApp.create('createChoice Test');

// add a multiple-choice item
var testQuestion = form.addMultipleChoiceItem();
testQuestion.setTitle('Anything here?');
var whatsis = testQuestion.createChoice('bozzle');
var firstChoice = testQuestion.createChoice('this');
testQuestion.createChoice("that");
testQuestion.createChoice("the other");
testQuestion.createChoice("Ouch!");


//add a new multiple-choice item and a pagebreak item
var item = form.addMultipleChoiceItem();
var pageBreak = form.addPageBreakItem();

// Set some choices with go-to-page logic.
var rightChoice = item.createChoice('Vanilla', FormApp.PageNavigationType.SUBMIT);
var wrongChoice = item.createChoice('Chocolate', FormApp.PageNavigationType.RESTART);

// For GO_TO_PAGE, just pass in the page break item. For CONTINUE (normally the default), pass in
// CONTINUE explicitly because page navigation cannot be mixed with non-navigation choices.
var iffyChoice = item.createChoice('Peanut', pageBreak);
var otherChoice = item.createChoice('Strawberry', FormApp.PageNavigatio[enter image description here][1]nType.CONTINUE);
item.setChoices([rightChoice, wrongChoice, iffyChoice, otherChoice]);
  
}

这是我得到的,没有显示“bozzle”等选项,以及我想要但无法创建的图像。

非常感谢您的帮助!

Here's a screenshot, with no labels/choices under "Anything here?"

And a mockup with "bozzle", "this" and so on as choices

您没有看到 testQuestion 选项的原因是您没有为问题设置选项。

因此,我建议您将创建第一个问题的位置更新为:

var testQuestion = form.addMultipleChoiceItem();
testQuestion.setChoices([testQuestion.createChoice('bozzle'), testQuestion.createChoice('this'), testQuestion.createChoice("that"), testQuestion.createChoice("the other"), testQuestion.createChoice("Ouch!")]);

createChoice方法仅用于创建选择项,为了将它们实际添加到问题中,您还必须使用setChoices方法。

参考