我之前提到的“答案”并沒有解決這個問題。在我在這里發布之前,我嘗試了這種技術。該技術不起作用,我收到了與我現在收到的相同的錯誤。
我正在嘗試創建一個 Google Apps 腳本,它允許我使用表單中以前的資料并將資料“重新提交”到表單中。用例是我會讓老師在收到班級部分提交的內容后更改表格問題。此更改將導致以前的資料不再包含在表單回應摘要頁面或單個回應中。相反,資料是從視圖中隱藏的,只能通過下載回應的 csv 或在鏈接的作業表中找到的資料來訪問(前提是教師在編輯表格之前鏈接了作業表)。當前的解決方法是使用表單的預填充 URL 功能,然后通過使用作業表中的公式為每行資料創建一個 URL。然后,教師必須手動單擊每個鏈接以將資料輸入回表格中。
為了解決這個問題,我正在創建一個腳本,當這種情況發生時我可以和這些老師一起使用。除了網格和時間專案外,我已經弄清楚了一切。這是腳本的精簡版本,僅適用于 checkbox_grid 問題。
資料在一列(B1:B)中,如下所示:
| 1X2 網格 [R1] |
|---|
| C1、C2 |
| C1 |
| C2 |
| C1、C2 |
在腳本中,我已經能夠為每個單元格創建一個陣列,如下所示:
array[0] = [['C1'].['C2']]
array[1] = [['C1'],['']]
array[2] = [[''],['C2']]
array[3] = [['C1'].['C2']]
正在回傳以下錯誤:
例外:錯誤的回應數量:2。完全預期:1。
此外,在進一步研究這一點時,我在開發者網站上發現了以下內容:
對于GridItem問題,這將回傳一個String[]陣列,其中索引 n 處的答案對應于網格中第 n 1 行的問題。如果受訪者沒有回答網格中的問題,則該答案將回傳為''。
For CheckboxGridItem questions, this returns a String[][] array in which the answers at row index n corresponds to the question at row n 1 in the checkbox grid. If a respondent did not answer a question in the grid, that answer is returned as ''.
What is the correct way to structure the array so that GAS will accept it?
Code found in sample file is here:
//Global Variables for Spreadsheet
const ss = SpreadsheetApp.getActiveSpreadsheet();
var dataSheet = ss.getSheetByName('Data');
var lastRow = dataSheet.getLastRow();
var lastCol = dataSheet.getLastColumn();
var sheetHeader = dataSheet.getRange('1:1');
var sheetTitles = sheetHeader.getValues();
var error = 'None';
var cellValue = [];
//Global Variables for Form
var form = FormApp.openByUrl(ss.getFormUrl());
var formResponses = form.getResponses();
var formCreate = form.createResponse();
var formItems = form.getItems();
//sendValues() calls the setResponses() to set the values of each form response.
// The function then submits the forms and clears the responses.
// Flush is used to make sure the order of the code is retained.
//------------------ New Variables -------------------------------
// r = row
function sendValues() {
for (r = 2; r <= lastRow; r ) {
setResponses(r);
//formCreate.submit();
console.log('submitted');
formCreate = form.createResponse();
SpreadsheetApp.flush();
}
}
//setResponses(r) sets the response for each cell of the data sheet.
//calls gridResponse(cell,i) if the question type is either a checkbox grid or multiple choice grid question.
//----------------------- New Variables ---------------------------
// c = column
// i = item response
// ssHeaderValue = Values of the Row 1 in the Data Sheet (dataSheet)
// cell = Value of each cell in dataSheet
// cellValue = Converts commas in cell variable to ;
// formItem = each item from all of the Form items
// fHeaderValue = Value of each "header" (Title) of each Form value
// itemAs = item set to correct type
// itemResponse = created response
function setResponses(r) {
for (c = 2; c <= lastCol; c ) {
for (i = 0; i < formItems.length; i ) {
var ssHeaderValue = dataSheet.getRange(1, c).getValue();
var itemType = formItems[i].getType();
var cell = dataSheet.getRange(r, c).getValue();
gridResponse(cell, i);
var formItem = formItems[i];
var fHeaderValue = formItem.getTitle();
var itemResponse = formItem
.asCheckboxGridItem()
.createResponse(cellValue);
}
//ERROR HERE: formCreate.withItemResponse(itemResponse);
}
}
//checkboxGridResponse(cell,i) makes an array of cellValue that can be used in CHECKBOX_GRID item.
//--------------------- New variables ------------------------------
// z = loop counter
// gridColumns = number of possible responses in the Form
// cellValueLength = number of responses in the cell data
// cellColumns = cell data split into separate values
// cellValue = value to be returned
// arr = temporary array for making cellValue into a 2D array
function gridResponse(cell, i) {
var gridColumns = formItems[i].asCheckboxGridItem().getColumns();
var cellValueLength = cell.split(',').length;
var cellColumns = cell.split(',');
for (z = 0; z < cellValueLength; z ) {
console.log(
'cellColumns '
z
' = '
cellColumns[z]
'; gridColumns '
z
' = '
gridColumns[z]
);
var arr = [gridColumns[z] == cellColumns[z] ? gridColumns[z] : null];
cellValue.push(arr);
}
console.log(cellValue);
console.log('cellValue[0][0] = ' cellValue[0][0]);
console.log('cellValue[0][1] = ' cellValue[0][1]);
console.log('cellValue [1][0] = ' cellValue[1][0]);
return cellValue;
}
uj5u.com熱心網友回復:
a 的回應結構CheckboxGridItem是一個二維陣列:
- 外部陣列應包含行陣列
- 內部陣列應包含列元素
- 元素本身應該是列標簽
此 1 項表單的示例腳本提交一個 2x2 CheckboxGridItem 陣列,為兩行選擇“列 1”。
| 第 1 列 | 第 2 欄 |
|---|---|
| ? | ? |
| ? | ? |
function myFunc16() {
const form = FormApp.getActiveForm();
const newRes = form
.getItems()[0]
.asCheckboxGridItem()
.createResponse([['Column 1'], ['Column 1']]);
form.createResponse().withItemResponse(newRes).submit();
}
提交另一個回應選擇
| 第 1 列 | 第 2 欄 |
|---|---|
| ? | ? |
| ? | ? |
陣列應該是:
[
null, //Row1
['Column 1'] //Row2
]
同樣,對于
| 第 1 列 | 第 2 欄 |
|---|---|
| ? | ? |
| ? | ? |
大批:
[
['Column 2'],
null
]
如果你插入一個陣列
[
['Column 2']
]
對于 2x2 CheckboxGridItem,錯誤,
例外:錯誤的回應數量:1. 完全預期:2。
被拋出。這意味著array.length與網格中的行數不匹配。
可以通過手動提交 1 個復選框網格項表單并使用以下方式以編程方式檢查回應來找到陣列的結構:
//Examine third response [2]
//First item's response [0]
console.log(form.getResponses()[2].getItemResponses()[0].getResponse())
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/438806.html
標籤:javascript arrays google-apps-script google-sheets google-forms
