如何从 Google Sheets 获取数据并将其显示在 Google Forms 上?

oni*_*619 0 google-sheets google-apps-script google-forms

我正在制作一个Google 表单,我想向其中添加一些问题,但这些问题应该从Google Sheet中的问题池中随机挑选。为此需要做什么?例如:我想从 Google 表格的 20 个问题池中随机显示 5 个问题

Ps:我确实尝试了 Stackoverflow 上已经提到的解决方案,但它对我的目的没有帮助。

Nig*_*Eye 6

为了创建一个可以回答您的问题的脚本,我创建了一个如下所示的示例表:

样本表

其中 A 是问题,后续列是选项。注意:如果您只需生成没有任何选项的问题,您可以选择将其从工作表中删除。我包含了一些选项,因为这将是您问题中最糟糕的情况。

首先,我们需要拥有表单和电子表格的 ID:

var formID = <FORM_ID>;
var ssID = <SPREADSHEET_ID>;
var fData = FormApp.openById(formID);
var wsData = SpreadsheetApp.openById(ssID).getSheetByName("Sheet1");
Run Code Online (Sandbox Code Playgroud)

下一步是尝试从电子表格生成问题:

function populateForm() {
  // get all values from spreadsheet
  var ssValues = wsData.getDataRange().getValues();

  // traverse all values
  for(var row = 0; row < ssValues.length; row++){
    var newItem;
    var options = [];
    for(var col = 0; col < ssValues[row].length; col++){
      var cellValue = ssValues[row][col];
      switch(col) {
        case 0:
          // if question, add as item
          newItem = fData.addListItem().setTitle(cellValue).getId(); 
          break;
        default:
          // if not question, add as choice
          options.push(cellValue);
      }
    }
    // add accumulated options as choices for the recently added item
    fData.getItemById(newItem).asListItem().setChoiceValues(options);
  }
}
Run Code Online (Sandbox Code Playgroud)

完成此步骤后,您应该能够获得添加到表单中的所有问题。现在,由于我们只需要生成 5 个随机问题,因此我添加了以下函数供我们使用。

function getFiveRandomQuestions(array){
  // randomly remove questions until 5 remains
  for(var i = array.length - 1; i >= 5; i--){
    array.splice(Math.floor(Math.random() * array.length), 1);
  }
  return array;
}
Run Code Online (Sandbox Code Playgroud)

上面的函数接受一个数组( 的结果getValues()),然后随机从中删除项目,直到只剩下 5 个项目。

下面是整合以上所有功能后的代码。注意:我添加了clearForm()删除现有项目,以便每当我运行时表单都会重置populateForm()。这是为了测试,以防万一您需要它。根据需要修改代码。

// Randomize Form from Sheets
var formID = <FORM_ID>;
var ssID = <SPREADSHEET_ID>;
var fData = FormApp.openById(formID);
var wsData = SpreadsheetApp.openById(ssID).getSheetByName("Sheet1");

function clearForm(){
  // clears all items
  var items = fData.getItems();
  while(items.length > 0){
    fData.deleteItem(items.pop());
  }
}

function getFiveRandomQuestions(array){
  // randomly remove questions until 5 remains
  for(var i = array.length - 1; i >= 5; i--){
    array.splice(Math.floor(Math.random() * array.length), 1);
  }
  return array;
}

function populateForm() {
  // call clearForm to prevent appending newly randomized questions
  clearForm();

  var ssValues = wsData.getDataRange().getValues();

  // remove random questions until 5 are remaining
  var formItems = getFiveRandomQuestions(ssValues);

  for(var row = 0; row < formItems.length; row++){
    var newItem;
    var options = [];
    for(var col = 0; col < formItems[row].length; col++){
      var cellValue = formItems[row][col];
      switch(col) {
        case 0:
          // if question, add as item
          newItem = fData.addListItem().setTitle(cellValue).getId(); 
          break;
        default:
          // if not question, add as choice
          options.push(cellValue);
      }
    }
    // add accumulated options as choices for the recently added item
    fData.getItemById(newItem).asListItem().setChoiceValues(options);
  }
}
Run Code Online (Sandbox Code Playgroud)

这是示例输出:

样本输出

请注意,这个答案很简单,仍然可以根据您的测试用例进行优化。如果您有任何不清楚的地方,请随时提问。

编辑:

如果要添加硬编码问题,则需要在循环之前添加。

// remove random questions until 5 are remaining
var formItems = getFiveRandomQuestions(ssValues);
    
// start of hardcoded questions
fData.addListItem().setTitle('Name').getId(); 
fData.addListItem().setTitle('Email').getId(); 
// end of hardcoded questions    

for(var row = 0; row < formItems.length; row++){
Run Code Online (Sandbox Code Playgroud)

另外,如果您只想有 5 个问题,并且已经通过硬编码有 2 个问题,我们需要将随机数函数减少到 3 个而不是 5 个。

// note that I renamed the function to getRandomQuestions so bear in mind to update the function call too
function getRandomQuestions(array){
  for(var i = array.length - 1; i >= 3; i--){
    array.splice(Math.floor(Math.random() * array.length), 1);
  }
  return array;
}
Run Code Online (Sandbox Code Playgroud)

示例输出: 硬编码示例

  • 我会说很好的答案。 (3认同)
  • 这确实是这里的最佳答案,详细的解释并准确地解决了您的问题 (2认同)