如何使用Java脚本在Google表单上发送对多个答案问题的回复 [英] How to send a response to multiple answer question on a Google Form using Javascript

查看:0
本文介绍了如何使用Java脚本在Google表单上发送对多个答案问题的回复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

因此,在这个答案中,我建议将其添加到查询参数中。在这种情况下,我认为有两种模式适合您的情况。

模式1:

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

// 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());
}

模式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.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())
}

这篇关于如何使用Java脚本在Google表单上发送对多个答案问题的回复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆