谷歌脚本,以获取谷歌表格的平均分数和电子邮件(作为测验)

问题描述 投票:1回答:1

我需要一个Google脚本(GAS)来检索已转换为测验的Google表单中的电子邮件和总分。

在GAS中,您可以获得每个问题的分数,但我需要获得所有问题的分数,然后平均最终分数(每个测验有20个问题)。我还需要完成测验的每个人的电子邮件。这些测验在机构内完成,因此选择了“收集电子邮件地址”。

我需要这个而不是“回复电子表格”,因为我将进行数百个测验,我需要将所有成绩放在一个电子表格中,作为摘要。我不想拥有数百个响应电子表格。

我现在所拥有的是下面的内容,但我似乎无法获得每个测验的平均分数,并将其放在电子表格中的电子邮件旁边。任何帮助,将不胜感激。

function getPoints() {
var form = FormApp.openById('ID');
 var formResponses = form.getResponses();
 var formItems = form.getItems();

 for (var i = 0; i < formResponses.length; i++) {
  var formResponse = formResponses[i];
   var email = formResponse.getRespondentEmail();

/* I need to get all emails from those who responded, 
   not just one and put them in column A.*/

   var s = SpreadsheetApp.openById("ID").getSheetByName("Sheet1");
   var sr = s.getRange("A:A").setValues(email);
 }

 for (var j = 0; j < formItems.length; j++) { 
   var item = formItems[i];
    if (item.getType() === item.getType().TEXT){
    var points = item.asTextItem().getPoints(); 
    var itemResponse = formResponse.getGradableResponseForItem(item);
    var answer = itemResponse.getResponse();
    var sc = itemResponse.getScore();

    /* I need to get all the scores, not just one, and then average
       them, and them put them in column B, beside the corresponding
       email in column A. */

    var s = SpreadsheetApp.openById("ID").getSheetByName("Sheet1");
    var sr = s.getRange("B:B").setValues(sc);
    }
 }
}
google-apps-script google-sheets google-form google-form-quiz
1个回答
1
投票

你的脚本非常接近。

由于没有一种方法可以立即获得所有项目分数,因此一种方法是构建项目分数数组,然后计算每个测验的平均值(提交的响应)。

例:

以下脚本旨在用作有界脚本,包括三个函数:

  • onOpen添加自定义菜单
  • showAvarage设置要处理的表单的主函数,并调用创建2D数组的函数。它可以用于将值发送到电子表格,但为了简单起见,这被省略了。
  • calculateAvarage收集受访者的电子邮件并计算每个测验的平均分数(回复提交)
function onOpen(e) {
  var ui = FormApp.getUi();
  var menu = ui.createMenu('My Menu')
  .addItem('Average', 'showAverage')
  .addToUi();
}

function showAverage(){
  // Works for bounded scripts called from custom menus or the script editor
  var form = FormApp.getActiveForm(); 

  Logger.log(calculateAverage(form));
}

/**
 * Calculate the average score for each submitted response
 *
 * @param {Object} form      The form to be processed.
 * @return {Array}           2D array. First column respondent email, 
 *                             second column response average.
 */
function calculateAverage(form){

  var formResponses = form.getResponses();

  // If there aren't submitted responses, exit.
  if(formResponses.length == 0) return 'No responses';

  // Initialize output
  var output = [];

  for(var i = 0; i < formResponses.length ; i++){
    var itemResponses = formResponses[i].getGradableItemResponses();

    // Initialize scores array. Later it will be used to calculate the score average.
    var scores = [];
    for(var j = 0; j < itemResponses.length; j++){
      var score = itemResponses[j].getScore();
      scores.push(score);
    }

    // Average calculation. Reference https://stackoverflow.com/a/10624256/1595451
    var sum = scores.reduce(function(a, b) { return a + b; });
    var avg = sum / scores.length;

    // Add row
    var email = formResponses[i].getRespondentEmail();
    output.push([email,avg]);
  }

  return output;
}

参考

© www.soinside.com 2019 - 2024. All rights reserved.