javascript - 如何使用 Javascript 在 Google 表单上发送对多选问题的回复

标签 javascript google-apps-script google-sheets google-forms

我在一家特许学校工作,当我有新学生时,我必须通过 Google 表单为学生填写设备申请表。我希望能够自动提交回复,而不必每次都填写表格。我已经在 Google 表格中获得了我需要的有关学生的所有信息。
没有对表单的编辑权限,并且只能使用 Google 应用程序脚本进行编码,因为我在我的工作笔记本电脑上没有服务器访问权限或管理员访问权限。所以这真的是我唯一的自动化选择。
到目前为止,我已经能够使用表单 url 和一个使用问题的 entryID 的对象进行提取调用。

const obj = {
"entry.1111": tName,
"entry.2222": sFirst,
"entry.3333": sLast,
"entry.4444": 'Option 1', //multiple answer question
"entry.5555": 'yes',
"entry.6666": 'yes',
"entry.7777": 'minor',
"entry.8888": 'initial'
}
const options = {
  'method': 'post',
  'payload': obj
}
UrlFetchApp.fetch(url,options)

只要我不需要为每个问题包含一个以上的答案,此方法就有效,但对于某些学生,我需要这样做。如果我尝试发送类似“选项 1,选项 2”的字符串,脚本将失败。

编辑:我的代码没有对象中的注释。

最佳答案

当发送一个问题的多个答案时,不幸的是,我认为 JSON 对象可能无法直接用于有效负载。因为在这种情况下使用相同的 key 。而且,即使将数组用于值,也会发生错误。这已在您的评论中提到。

所以在这个回答中,我想建议将它添加到查询参数中。在这种情况下,我认为您的情况有 2 种模式。

模式一:

在此模式中,所有值都用作查询参数。

// This script is from https://gist.github.com/tanaikech/70503e0ea6998083fcb05c6d2a857107
String.prototype.addQuery = function(obj) {
  return this + Object.keys(obj).reduce(function(p, e, i) {
    return p + (i == 0 ? "?" : "&") +
      (Array.isArray(obj[e]) ? obj[e].reduce(function(str, f, j) {
        return str + e + "=" + encodeURIComponent(f) + (j != obj[e].length - 1 ? "&" : "")
      },"") : e + "=" + encodeURIComponent(obj[e]));
  },"");
}

function myFunction() {
  const url = "### your URL ###"; // Please set your URL.
  const query = {
    "entry.1111": tName,
    "entry.2222": sFirst,
    "entry.3333": sLast,
    "entry.4444": ['Option 1', 'Option 2'],
    "entry.5555": 'yes',
    "entry.6666": 'yes',
    "entry.7777": 'minor',
    "entry.8888": 'initial'
  };
  const endpoint = url.addQuery(query);
  const options = {'method': 'post'};
  const res = UrlFetchApp.fetch(endpoint, options);
  console.log(res.getContentText());
}

模式二:

在这种模式下,只有多选问题作为查询参数发送。

// This script is from https://gist.github.com/tanaikech/70503e0ea6998083fcb05c6d2a857107
String.prototype.addQuery = function(obj) {
  return this + Object.keys(obj).reduce(function(p, e, i) {
    return p + (i == 0 ? "?" : "&") +
      (Array.isArray(obj[e]) ? obj[e].reduce(function(str, f, j) {
        return str + e + "=" + encodeURIComponent(f) + (j != obj[e].length - 1 ? "&" : "")
      },"") : e + "=" + encodeURIComponent(obj[e]));
  },"");
}

function myFunction() {
  const url = "### your URL ###"; // Please set your URL.
  const query = {
    "entry.4444": ['Option 1', 'Option 2'],
  };
  const obj = {
    "entry.1111": tName,
    "entry.2222": sFirst,
    "entry.3333": sLast,
    "entry.5555": 'yes',
    "entry.6666": 'yes',
    "entry.7777": 'minor',
    "entry.8888": 'initial'
  };
  const endpoint = url.addQuery(query);
  const options = {
    'method': 'post',
    'payload': obj
  };
  const res = UrlFetchApp.fetch(endpoint, options);
  console.log(res.getContentText())
}

关于javascript - 如何使用 Javascript 在 Google 表单上发送对多选问题的回复,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69293786/

相关文章:

google-apps-script - Google Sheet 脚本将文件夹范围导入到一张工作表

javascript - 在数据验证中使用自定义函数

javascript - 本地存储在页面重新加载时不断重置

javascript - 向格式为 'h:i:s: A' 的字符串添加分钟和秒的最快方法

javascript - getCreators() 类型错误

javascript - CurrentTime 脚本在 Google 文档中不起作用

javascript - 删除 Google 表单提交的文件

google-sheets - 创建具有过滤范围的单元格内下拉列表

javascript - 使用 Angular 格式化文本输入

javascript - <a> svg 周围的链接行为异常