当存在空值且需要空值时,在 GRID 项类型中为 Google Forms .createResponse 构造一个数组
Constructing an array for Google Forms .createResponse in a GRID item type when null values are present and needed
我正在从 sheet 中获取值,并且需要将这些值转换为一个数组,然后由 Google 表单的 .createResponse() 接受 GRID 项目类型。在 spreadsheet 中,cell[0] = NYC、cell[1] = LA 和 cell[2] = Dallas 的值。当每个单元格中都有数据时,以下函数将起作用。但是,如果其中一个单元格为空白,则该函数会将空白条目转换为 null。我正在使用相同的技术将其他函数中的 CHECKBOX_GRID 项的空白转换为 null,并且已将此相同的技术声明为使 GRID 项正确格式化的解决方案。但是,该函数在 var itemResponse 行出错,错误为“异常:提交给网格问题的响应无效。”我知道这意味着响应数组中的数据与 GRID 项目类型中的可能选择不匹配。有人知道为什么这个功能不能正常使用吗?
function stackExp(){
const ss = SpreadsheetApp.getActiveSpreadsheet();
var form = FormApp.openByUrl(ss.getFormUrl());
var countMC = form.getItems()[0].asGridItem().getRows().length;
var arr = ss.getSheetByName(dataSheetName).getRange(4, 2, 1, countMC).getValues()[0].map(e => e.trim());
for(j = 0; j < arr.length; j++){
if(arr[j] == '' || arr[j] == ""){
arr[j] = null;
}
}
var itemResponse = form.getItems()[0].asGridItem().createResponse(arr); //Errors here
form.createResponse().withItemResponse(itemResponse).submit();
}
我相信你的现状和目标如下。
- 您有一个 Google 表单,其中第一项包含 GridItem。
- GridItem 有 3 行
Pizza,Burgers,Tacos
和 4 列,顺序为 Chicago,NY,LA,Dallas
。
- 您想从
dataSheetName
sheet 的单元格“B4:D4”中检索值,并希望将这些值用作此项的响应。
问题:
比如这个表单项使用脚本手动提交包含未勾选的按钮时,得到["NY", null, "Chicago"]
。但是,当使用脚本将 ["NY", null, "Chicago"]
用于提交的值时,会出现 Exception: Invalid response submitted to grid question.
.
之类的错误
- 这种情况已作为错误报告给 Google 问题跟踪器。 Ref
2022 年 3 月 16 日(昨天),Google Forms API 已作为 alpha 版本发布。但现阶段无法使用Forms提交表单回复API。我想这个方法可能会在以后的更新中加入。
解决方法:
在实现您的目标的当前解决方法中,我想提出以下解决方法。在此解决方法中,使用 UrlFetchApp 将值直接提交到表单。
function sample() {
const ids = ["entry.###", "entry.###", "entry.###"]; // Please set each entry ID.
var dataSheetName = "Sheet1"; // Please set your sheet name.
const ss = SpreadsheetApp.getActiveSpreadsheet();
const form = FormApp.openByUrl(ss.getFormUrl());
const countMC = form.getItems()[0].asGridItem().getRows().length;
const arr = ss.getSheetByName(dataSheetName).getRange(4, 2, 1, countMC).getValues()[0].map((e, i) => e ? `${ids[i]}=${e.trim()}` : null).filter(e => e);
const url = `https://docs.google.com/forms/d/${form.getId()}/formResponse`;
const endpoint = `${url}?${arr.join("&")}`;
const res = UrlFetchApp.fetch(endpoint, { method: "post" });
console.log(res.getContentText())
}
为了检索const ids = ["entry.###", "entry.###", "entry.###"];
,您可以使用以下脚本。在使用此脚本之前,请通过 3 个选中的按钮提交表单。这样,可以检索到3个entry ID。
function sample2() {
const ss = SpreadsheetApp.getActiveSpreadsheet();
const form = FormApp.openByUrl(ss.getFormUrl());
const prefilledUrl = form.getResponses().pop().toPrefilledUrl();
console.log(prefilledUrl)
}
参考文献:
我正在从 sheet 中获取值,并且需要将这些值转换为一个数组,然后由 Google 表单的 .createResponse() 接受 GRID 项目类型。在 spreadsheet 中,cell[0] = NYC、cell[1] = LA 和 cell[2] = Dallas 的值。当每个单元格中都有数据时,以下函数将起作用。但是,如果其中一个单元格为空白,则该函数会将空白条目转换为 null。我正在使用相同的技术将其他函数中的 CHECKBOX_GRID 项的空白转换为 null,并且已将此相同的技术声明为使 GRID 项正确格式化的解决方案。但是,该函数在 var itemResponse 行出错,错误为“异常:提交给网格问题的响应无效。”我知道这意味着响应数组中的数据与 GRID 项目类型中的可能选择不匹配。有人知道为什么这个功能不能正常使用吗?
function stackExp(){
const ss = SpreadsheetApp.getActiveSpreadsheet();
var form = FormApp.openByUrl(ss.getFormUrl());
var countMC = form.getItems()[0].asGridItem().getRows().length;
var arr = ss.getSheetByName(dataSheetName).getRange(4, 2, 1, countMC).getValues()[0].map(e => e.trim());
for(j = 0; j < arr.length; j++){
if(arr[j] == '' || arr[j] == ""){
arr[j] = null;
}
}
var itemResponse = form.getItems()[0].asGridItem().createResponse(arr); //Errors here
form.createResponse().withItemResponse(itemResponse).submit();
}
我相信你的现状和目标如下。
- 您有一个 Google 表单,其中第一项包含 GridItem。
- GridItem 有 3 行
Pizza,Burgers,Tacos
和 4 列,顺序为Chicago,NY,LA,Dallas
。 - 您想从
dataSheetName
sheet 的单元格“B4:D4”中检索值,并希望将这些值用作此项的响应。
问题:
比如这个表单项使用脚本手动提交包含未勾选的按钮时,得到
之类的错误["NY", null, "Chicago"]
。但是,当使用脚本将["NY", null, "Chicago"]
用于提交的值时,会出现Exception: Invalid response submitted to grid question.
.- 这种情况已作为错误报告给 Google 问题跟踪器。 Ref
2022 年 3 月 16 日(昨天),Google Forms API 已作为 alpha 版本发布。但现阶段无法使用Forms提交表单回复API。我想这个方法可能会在以后的更新中加入。
解决方法:
在实现您的目标的当前解决方法中,我想提出以下解决方法。在此解决方法中,使用 UrlFetchApp 将值直接提交到表单。
function sample() {
const ids = ["entry.###", "entry.###", "entry.###"]; // Please set each entry ID.
var dataSheetName = "Sheet1"; // Please set your sheet name.
const ss = SpreadsheetApp.getActiveSpreadsheet();
const form = FormApp.openByUrl(ss.getFormUrl());
const countMC = form.getItems()[0].asGridItem().getRows().length;
const arr = ss.getSheetByName(dataSheetName).getRange(4, 2, 1, countMC).getValues()[0].map((e, i) => e ? `${ids[i]}=${e.trim()}` : null).filter(e => e);
const url = `https://docs.google.com/forms/d/${form.getId()}/formResponse`;
const endpoint = `${url}?${arr.join("&")}`;
const res = UrlFetchApp.fetch(endpoint, { method: "post" });
console.log(res.getContentText())
}
为了检索const ids = ["entry.###", "entry.###", "entry.###"];
,您可以使用以下脚本。在使用此脚本之前,请通过 3 个选中的按钮提交表单。这样,可以检索到3个entry ID。
function sample2() {
const ss = SpreadsheetApp.getActiveSpreadsheet();
const form = FormApp.openByUrl(ss.getFormUrl());
const prefilledUrl = form.getResponses().pop().toPrefilledUrl();
console.log(prefilledUrl)
}