在 Google 表单多选网格项中设置列选项?

Setting the Column Choices in a Google Form Multi Choice Grid Item?

我一直在努力获取正确的代码以在 Google 表单多选网格中设置列选择。我的代码一直给我调试错误

TypeError: Cannot find function setColumns in object Item. (line 26, file "Populate Hazard Choices").

我试过 setColumnChoices 和 setColumns,结果似乎一样糟糕。

function PopulateHazardChoices(){

// call the form and connect to the Question Item
  var form = FormApp.openById("FakeFormID");  
  var QuestionItem = form.getItemById("fakeItemID");

// identify the sheet Hazard Choices needed to populate the question selections
  var ss = SpreadsheetApp.getActive();
  var DataFactors = ss.getSheetByName("DataFactors");

// grab the Hazards in the first column of the sheet from the Group of Hazard Choices
  // use sheet Row Number for First Hazard in the group of choices; use 1 as the index column A; number of rows included in range
  // 7,1,3 would be Row 7, Column A, 4 rows in the range - therefore A7 through A10

  // [Conditions of Runway] Hazard Group
  var HazardValues = DataFactors.getRange(7,1,4);
  var HazardSelections = [];

// convert the array ignoring empty cells
  for(var i = 0; i < HazardValues.length; i++)    
    if(HazardValues[i][0] != "")
      HazardSelections[i] = HazardSelections[i][0];

// populate the Wind Question with the array data
  QuestionItem.setColumns(HazardSelections);
}  

计划从名为 "DataFactors" 的 Sheet 填充网格列,以便对危害列表进行的任何更改都将完全相同地填充到表格中。当用户提交表单时,会将选项与 sheet 进行比较并分配一个分值。希望这将解决表单提交和危险值之间的错误比较问题。 我使用下拉列表取得了很好的效果,但似乎无法使该方法适用于多选网格。

以下示例是针对新项目的

来自Class GridItem

// Open a form by ID and add a new grid item.
var form = FormApp.openById('1234567890abcdefghijklmnopqrstuvwxyz');
var item = form.addGridItem();
item.setTitle('Rate your interests')
    .setRows(['Cars', 'Computers', 'Celebrities'])
    .setColumns(['Boring', 'So-so', 'Interesting']);

如果物品已经存在,那么一旦你得到物品并且在使用setColumns之前,你应该使用界面物品的asGridItem

var gridItem = QuestionItem.asGridItem();
gridItem.setColumns(HazardSelections);

Ruben,感谢您的帮助。这是包含几种不同类型的表单项(Grid、CheckboxGrid 和 MultipleChoice)的完整脚本。 谢谢, 杰克

function PopulateHazardChoices(){

// call the sheet and form needed to populate the question selections
  var ss = SpreadsheetApp.getActive();
  var DataFactors = ss.getSheetByName("RiskValues");  
  var form = FormApp.openById("FakeFormID");

// For each Group of Hazard Choices - repeat the folowing
  // identify the Hazards in Column A of the sheet for each Group of Hazard Choices 
     // Use Row Number for First Hazard in the Group of Harard Choices
     // Use 1 as the index Column A
     // Number of rows included in the Group
     // 2,1,3 would be Row 2, Column A, 3 rows in the range - therfore A2 through A4
  //declare QuestionItem including type of item i.e. .asGridItem();  
  // populate the Question with the array data

// Crew Compliment - Hazard Group A3 through A5
  var HazardValues = DataFactors.getRange(3,1,3).getValues();  
  var QuestionItem = form.getItemById("FakeItemID").asGridItem();
  QuestionItem.setColumns(HazardValues);
  //End of Hazard Group

// pAve Rotorcraft - Hazard Group A40 through A43
  var HazardValues = DataFactors.getRange(40,1,4).getValues();  
  var QuestionItem = form.getItemById("FakeItemID").asCheckboxGridItem();
  QuestionItem.setColumns(HazardValues);
  //End of Hazard Group

// paVe Runway Length - Hazard Group A72 through A75
  var HazardValues = DataFactors.getRange(72,1,4).getValues();  
  var QuestionItem = form.getItemById("FakeItemID").asMultipleChoiceItem();
  QuestionItem.setChoiceValues(HazardValues);
  //End of Hazard Group


} //End function PopulateHazardChoices

// Edit Journal - Created December 2019, Jack Gainer, TSTC Chief Pilot
  // Initial Implentation Tests - January 2019
  // Add remaiining Hazard Group setValues code - February 2019

这令人满意地将 Google 工作表中的列和行插入到 Google 表单多项选择网格项目:

var form = FormApp.openById('MyForm');
var PtjGridList = form.getItemById("MyGridItem Eg.4158415230").asGridItem();
var ss = SpreadsheetApp.openById("MySpreadsheet");
var PtjNombre = ss.getSheetByName("MySheet");
var RowValues = PtjNombre.getRange(2, 5, PtjNombre.getMaxRows() - 1).getValues();
var ValuesRow = [];
for(var i = 0; i < sheetValues.length; i++)
if(RowValues[i][0] != "")
ValuesRow[i] = RowValues[i][0];
PtjGridList.setRows(ValuesRow)
var ColumnValues = PtjNombre.getRange(2, 6, PtjNombre.getMaxRows() - 1).getValues();
var ValuesColumn = [];
for(var i = 0; i < sheetValues.length; i++)
if(ColumnValues[i][0] != "")
ValuesColumn[i] = ColumnValues[i][0];
PtjGridList.setColumns(ValuesColumn)

希望对您有所帮助。