【问题标题】:How can I add choices in a Google Form with Google Apps Script如何使用 Google Apps 脚本在 Google 表单中添加选项
【发布时间】:2021-01-26 09:07:57
【问题描述】:

我正在尝试掌握以编程方式创建 Google 表单,但无法将选项分配给多项选择项。我可以创建项目 (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

【问题讨论】:

  • 您无法添加选项到底是什么意思?如果我运行您共享的代码,我就能成功地看到这些选项。您是否介意分享所需结果的屏幕截图?
  • 我已经编辑了问题,添加了截图。当您运行 c 代码时,您是否在“Anything here”下看到了预期的选择?我从帖子中重新复制了代码,但该问题下仍然没有标签。

标签: google-apps-script google-forms


【解决方案1】:

您没有看到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 方法。

参考

【讨论】:

  • 非常感谢!如果您只需要 createChoice(),我应该自己弄清楚不会有 setChoices() 方法。 ;
猜你喜欢
  • 2023-03-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多