我正在尝试查找是否可以要求脚本来计算成绩,在该成绩中,每个好的答案给出+1,对于每个错误的答案减去-0,25。如屏幕截图所示,将在C列中进行计算。在此示例中,正确的分数是6.25,而不是7。
顺便说一句,分数计算是指第2行的主要答案。
答案 0 :(得分:2)
我相信您的目标如下。
#3ace9c
(绿色)时,您要添加1
。#ff9900
(橙色)时,您要缩小0.25
。1
。0.25
。为此,这个答案如何?
在此图案中,使用背景色。该示例脚本的流程如下。
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行的引用答案。该示例脚本的流程如下。
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);
}