Google表单 - 自动填充多项选择调查

时间:2014-10-22 06:24:59

标签: google-apps-script google-form autofill survey

我在google表单上创建了这个心理学调查。除了通常的电子邮件,名称等之外,它还有大约20个选择题。所以你看到没有任何正确/错误的答案。

如何伪造结果,或创建50个表单提交?请原谅我的无知,但结果是否会出现在Google电子表格中?我应该在Google电子表格中填写50个详细信息吗?

2 个答案:

答案 0 :(得分:1)

  

如何伪造结果,或创建50个表单提交?

请参阅Use App Scripts to open form and make a selection

  

原谅我的无知,但结果会在Google电子表格中吗?我应该在Google电子表格中填写50个详细信息吗?

回复将记录在两个地方;在表单本身(您可以在其中查看响应摘要)以及电子表格中。如果您的目的是验证提交回复的过程,那么您应该模拟提交。

答案 1 :(得分:0)

@Mogsdad's answer引用了一个基于url的解决方案,其中您的脚本不知道可以为给定问题(或多少个问题,等等)做出什么样的答案,这在某些用例中可能会出现问题。

您可以使用Forms Service以编程方式为Google表单创建响应,该响应使您可以从可用选项中随机选择可能的答案,例如(参考预定义的“书面”答案库,例如用于文本/段落答案)。

一个示例,假设您进行的是多项选择题测验,每个问题的选择数都不恒定:

function foo() {
  const form = FormApp.openById("some form id");

  const randomSubs = [], nSubs = 50;
  while (randomSubs.length < nSubs)
    randomSubs.push(createRandomSubmission_(form).submit());

  // doAwesomeAnalysis(randomSubs); // etc.
}

// Constructs a random response for the given form, and returns it to the caller (e.g. for submission, etc).
function createRandomSubmission_(form) {
  const resp = form.createResponse();
  const questions = form.getItems().filter(isAnswerable_);
  questions.forEach(function (question) {
    var answer = getRandomAnswer_(question);
    resp.withItemResponse(answer);
  });
  return resp;
}

var iTypes = FormApp.ItemType;
function isAnswerable_(item, index, allItems) {
  const iType = item.getType();
  switch (iType) {
    case iTypes.MULTIPLE_CHOICE:
    case iTypes.CHECKBOX:
    /** add more type cases here as you implement the relevant answer generator */
      return true;
    default:
      return false;
  }
}

// Uses the item type to call the appropriate answer generator.
function getRandomAnswer_(q) {
  const qType = q.getType();
  switch (qType) {
    case iTypes.MULTIPLE_CHOICE:
      return getRandomMultipleChoiceAnswer_(q.asMultipleChoiceItem());
    /** add more type cases + handlers here as you implement the relevant answer generator */
    default:
      throw new TypeError("Answering questions of type '" + qType + "' is not yet implemented");
  }
}

// Uniformly samples possible choices (including the "other" option, if enabled).
// Returns the item's ItemResponse
function getRandomMultipleChoiceAnswer_(mcItem) {
  const choices = mcItem.getChoices();
  const i = Math.floor( Math.random() * (choices.length + mcItem.hasOtherOption()) );
  return mcItem.createResponse( (i < choices.length) ?
      choices[i] : getRandomMCOtherOption_(mcItem) 
  );
}
function getRandomMCOtherOption_(mcItem) {
  // This function will be highly dependent on your situation.
  // It's your choice how you identify the MC item to determine what an "other" option could
  // be for a given question. getTitle() and getIndex() may be useful too.
  switch (mcItem.getId()) { 
    default:
      throw new Error("Not Implemented Yet");
  }
}