分数计算,考虑负面标记

时间:2020-06-28 02:15:22

标签: google-apps-script

我正在尝试查找是否可以要求脚本来计算成绩,在该成绩中,每个好的答案给出+1,对于每个错误的答案减去-0,25。如屏幕截图所示,将在C列中进行计算。在此示例中,正确的分数是6.25,而不是7。

顺便说一句,分数计算是指第2行的主要答案。

enter image description here

1 个答案:

答案 0 :(得分:2)

我相信您的目标如下。

  • 您想通过使用Google Apps脚本检查每个单元格的背景颜色来计算“ C”列的值。
    • 当单元格的背景色为#3ace9c(绿色)时,您要添加1
    • 当单元格的背景色为#ff9900(橙色)时,您要缩小0.25
  • 您想通过使用Google Apps脚本比较第2行(参考答案)和每行(第3行之后)的“ D”列与“ M”列来计算“ C”列的值。
    • 当第2行的“ D”至“ M”列与每行相同时,您要添加1
    • 当第2行的“ D”至“ M”列与每行都不相同时,您要减少0.25

为此,这个答案如何?

模式1:

在此图案中,使用背景色。该示例脚本的流程如下。

  1. 准备一个包含“好”和“错误”答案的颜色代码的对象。
  2. 从工作表中“ D3:M”的范围中检索背景颜色。
  3. 使用对象和背景色计算值。
  4. 将值放入工作表中的“ C”列。

示例脚本:

function myFunction() {
  const sheetName = "Sheet1";  // Please set the sheet name.

  // 1. Prepare an object including the color codes for "good" and "wrong" answers.
  const obj = {"#3ace9c": 1, "#ff9900": -0.25};  // Please set the color codes, if you change the color.

  // 2. Retrieve the background colors from the range of "D3:M" in the sheet.
  const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetName);
  const backgrounds = sheet.getRange("D3:M" + sheet.getLastRow()).getBackgrounds();

  // 3. Calculate the values using the object and the background colors.
  const result = backgrounds.map(r => [r.reduce((c, e) => c += obj[e], 0)]);

  // 4. Put the values to the column "C" in the sheet.
  sheet.getRange(3, 3, result.length, 1).setValues(result);
}

模式2:

在此模式下,使用第2行的引用答案。该示例脚本的流程如下。

  1. 从工作表中的“ A2:M”范围中检索值。
  2. 获取推荐答案。
  3. 通过比较参考答案来计算每个值的值,并创建一个包含结果值的数组。
  4. 将值放入工作表中的“ C”列。

示例脚本:

function myFunction() {
  const sheetName = "Sheet1";  // Please set the sheet name.
  
  // 1. Retrieve values from the range of "A2:M" in the sheet.
  const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetName);
  const values = sheet.getRange("A2:M" + sheet.getLastRow()).getValues();

  // 2. Retrieve the referring answer.
  const answers = values.shift();
  answers.splice(0, 3);

  // 3. Calculate the values of each values by comparing the referring answer, and create an array including the result values.
  const result = values.map(([,,c,...dm]) => [dm.reduce((c, e, i) => e == answers[i] ? c + 1 : c - 0.25, 0)]);

  // 4. Put the values to the column "C" in the sheet.
  sheet.getRange(3, 3, result.length, 1).setValues(result);
}

参考文献: