如何使用单个脚本创建多个具有固定数量的多项选择题的随机谷歌表单

huangapple go评论66阅读模式
英文:

How to create multiple random Google forms with a fixed number of Multiple Choice Questions with a single script

问题

免责声明:我刚刚开始使用JavaScript/Google表格。

我想要使用我创建的Google表格来验证一些数据。每个部分包含5个多项选择问题。在下面的示例中,我展示了如何将一个问题添加到部分中。

// 句子1
sentence = form.addMultipleChoiceItem();
choices = [
  sentence.createChoice('答案I'),
  sentence.createChoice('答案II'),
  sentence.createChoice('答案III'),
]
sentence.setTitle(pred_1) // pred_1 -> 从数据集中提取的变量
.setChoices(choices);

最后,我可以创建一个外观出色的表单,其中包含20个部分,每个部分都有五个问题,基于Google Drive文件夹中的单个数据集。

然而,我想要扩展这个功能。我有十个不同的数据集,我希望对它们进行评估。一个简单的方法是将脚本复制十次,并将脚本指向不同的数据集。但是,采用这种方法,我必须运行脚本十次,每次调整一些内容(例如标题/帮助文本)时,都必须重新运行所有脚本。

我在想,应该有一种解决方案,可以读取我的Drive文件夹中的所有文件,并运行主要脚本,以生成多个数据集的表单。我考虑了类似于以下方式的方法:

function createForms() {

  // 包含数据的文件夹
  var folder = DriveApp.getFolderById('文件夹ID')
  
  // 列出文件
  var files = folder.getFiles()

  for (var i = 1; i < 11; i++) {
      createMultipleChoiceForm(files[i]) // 创建单个表单的函数
  }
}

这不幸的是根本不起作用(它不会生成任何错误,但也不会生成任何内容)。

编辑:
*实际上,这是有效的,但每次都会创建一个新表单,而不会保存它(它会覆盖先前的版本)。

最后的方法当然是将所有10个数据集连接起来,并从Google Script编辑器内部对它们进行抽样。但我仍然希望最终获得十个不同的表单(这样我可以将它们发送给不同的人)。

希望有人能指导我朝正确的方向前进。

谢谢!

英文:

Disclaimer: I just started with JavaScripts/GoogleForms

I want to validate some data with a GoogleForm that I've created. Each section contains 5 multiple choice questions. In the example below I've shown how I add one question to the section.

    // Sentence 1
    sentence = form.addMultipleChoiceItem();
    choices = [
      sentence.createChoice(&#39;Answer I&#39;),
      sentence.createChoice(&#39;Answer II&#39;),
      sentence.createChoice(&#39;Answer III&#39;),
      ]
    sentence.setTitle(pred_1) // pred_1 -&gt; Variable extracted from the dataset
    .setChoices(choices);

In the end, I can build an excellent-looking form that contains 20 sections with five questions each based one a single dataset in my Google Drive Folder.

However, I want to extend this. I have ten different datasets that I want to have evaluated. A straightforward approach is copying the script ten times and pointing the script towards a different dataset. However, with this approach, I have to run the script ten times, and every time I adjust something (e.g. in the Title/HelpText), I have to rerun all the scripts.

I was thinking there should be a solution where I just read all the file inside my Drive folder and run my main scripts that produces the forms for multiple dataset. I was thinking something like this:

function createForms() {

  // Folder with data
  var folder = DriveApp.getFolderById(&#39;FOLDERID&#39;)
  
  // List files
  var files = folder.getFiles()

  for (var i = 1; i &lt; 11; i++) {
      createMultipleChoiceForm(files[i]) // Function that creates a single form
  }
}

This does unfortunately does not work at all (it does not give any errors, but does not produce anything).

EDIT:
This does actually work, but it creates a new form every time without saving it (it overruns the previous version).

I final approach is of course concatenating all the 10 datasets, and sample them from within the Google Script editor. But I still want to end up with ten different form (so I can send them to different people.

I hope anyone can give me some pointers in the right direction.

Thanks!

答案1

得分: 1

Folder.getFiles() 方法返回一个 FileIterator 对象,而不是一个数组。问题中的代码最终每次都使用 undefined 参数调用 createMultipleChoiceForm()

要使其正常工作,请使用以下模式替换 for 循环:

  const files = folder.getFiles();
  while (files.hasNext()) {
    createMultipleChoiceForm(files.next());
  }
英文:

The Folder.getFiles() method returns a FileIterator object rather than an array. The code in the question ends up calling createMultipleChoiceForm() with an undefined parameter every time.

To make it work, replace the for loop with this pattern:

  const files = folder.getFiles();
  while (files.hasNext()) {
    createMultipleChoiceForm(files.next());
  }

huangapple
  • 本文由 发表于 2023年2月8日 22:54:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/75387554.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定