自动 GMail 评分(Google 脚本)

Automatic GMail Grading (Google Script)

下面的代码是一个 sendEmail 函数,我用它来使用 Google 脚本发送 GMail 消息,以便学生获得有关他们数学测试的反馈。我创建了一个测试,顶部的第一个值列表是我输入的 (see example of grading here)。我想标记不等于顶部行的问题(“不正确”)。

特别是给我带来麻烦的代码行如下:if (Answer(j)!=row[j]) {row[j]=row[j]+" (Incorrect)";}

如果我输入 Answer(5),它会将其识别为顶部的第一行和第五列,它正在做它应该做的事情。但是,当我使用变量 j 调用 Answer(j) 时,它会将所有问题标记为不正确。现在,Answer(j) 对应于学生输入的 row[j]。我不知道自己做错了什么,一段时间以来我一直在尝试修改代码,但似乎没有任何效果。任何帮助将不胜感激!

function sendEmail() {
 var sheet = SpreadsheetApp.getActiveSheet();
 var startRow = 2; // First row of data to process
 var numRows = 5; // Number of rows to process
 var numQuestions = 17; //Number of Questions Asked on Assessment
 // Fetch the range of cells A2:B3
 var dataRange = sheet.getRange(startRow, 1, numRows, 25);
 // Fetch values for each row in the Range.
 var data = dataRange.getValues();
 for (var i = 0; i < data.length; ++i) {
   var row = data[i];
   var emailAddress = row[1]; // First column
   var message = row[0]+","+"\n"+"\n"+"Below is feedback on your assessment. Your score is a "+row[4]+".\n";
   for(var j = 4; j < numQuestions+4; ++j) {
     k=j-3;
     function Answer(n){
     return SpreadsheetApp.getActiveSheet().getRange(1, n).getValue();
     }
     if (Answer(j)!=row[j]) {row[j]=row[j]+" (Incorrect)";
     }
     message = message+"\n"+"Question #" +k+": "+row[j];
   }
   var emailSent = row[2]; // Third column
   if (emailSent !== EMAIL_SENT) { // Prevents sending duplicates
     var subject = 'Math Feedback';
     MailApp.sendEmail(emailAddress, subject, message);
     sheet.getRange(startRow + i, 2+1).setValue(EMAIL_SENT);
     // Make sure the cell is updated right away in case the script is interrupted
     SpreadsheetApp.flush();
   }
 }
}

我认为你的问题的原因可能是数组的索引(第一个数字是 0.)和列号(第一个数字是 1.)之间的差异。在您的脚本中,for(var j = 4; j < numQuestions+4; ++j) {j 用作 row 的索引。但是在 function Answer(n){return SpreadsheetApp.getActiveSheet().getRange(1, n).getValue();} 处, j 被用作列号。这样的话,下面的修改怎么样?

发件人:

return SpreadsheetApp.getActiveSheet().getRange(1, n).getValue();

收件人:

return SpreadsheetApp.getActiveSheet().getRange(1, n + 1).getValue();

注:

  • 在您的脚本中,当 header 行和数据行通过分离通过一个请求检索时,处理成本可能会降低。当这反映到您的脚本中时,它会变成如下。

      function sendEmail() {
        var sheet = SpreadsheetApp.getActiveSheet();
        var startRow = 1; // First row of data to process <--- Modified
        var numRows = 5; // Number of rows to process
        var numQuestions = 17; //Number of Questions Asked on Assessment
        // Fetch the range of cells A2:B3
        var dataRange = sheet.getRange(startRow, 1, numRows, 25);
        // Fetch values for each row in the Range.
        var [header, ...data] = dataRange.getValues(); <--- Modified
        for (var i = 0; i < data.length; ++i) {
          var row = data[i];
          var emailAddress = row[1]; // First column
          var message = row[0] + "," + "\n" + "\n" + "Below is feedback on your assessment. Your score is a " + row[4] + ".\n";
          for (var j = 4; j < numQuestions + 4; ++j) {
            k = j - 3;
            if (header[j] != row[j]) { <--- Modified
              row[j] = row[j] + " (Incorrect)";
            }
            message = message + "\n" + "Question #" + k + ": " + row[j];
          }
          var emailSent = row[2]; // Third column
          if (emailSent !== EMAIL_SENT) { // Prevents sending duplicates
            var subject = 'Math Feedback';
            MailApp.sendEmail(emailAddress, subject, message);
            sheet.getRange(startRow + i, 2 + 1).setValue(EMAIL_SENT);
            // Make sure the cell is updated right away in case the script is interrupted
            SpreadsheetApp.flush();
          }
        }
      }
    

参考: