用于根据 Google 工作表中的其他列查找平均值的计算脚本

Calculation script to find average based on other columns in Google Sheets

我有一个 Google Sheet 用于跟踪求职者面试数据。我试图根据他们的面试轮次和轮次分数找到每个候选人的平均分数。我想出了如何使用查询函数收集这些数据,但对于这个用例,它必须在脚本中完成。

Here is an example of the sheet

如有任何帮助,我们将不胜感激。

如果您需要获取数据作为永久静态值,即使源数据被修改后也不会改变,您仍然可以使用query()公式来获取结果,然后使用一个简短的用静态值替换公式及其结果的脚本。要尝试一下,插入 > Sheet 并使用:

=query(sumAve!A1:E, "select A, B, avg(D) where D is not null group by A, B", 1)

/**
* Replaces formulas with values in the active sheet.
*/
function replaceFormulasWithValuesInActiveSheet() {
  const wholeSheet = SpreadsheetApp.getActiveSheet().getDataRange();
  wholeSheet.setValues(wholeSheet.getValues());
}

平均分

function lfunko() {
  const ss = SpreadsheetApp.getActive();
  const sh = ss.getSheetByName("Sheet0");
  const vs = sh.getRange(2, 1, sh.getLastRow() - 1, sh.getLastColumn()).getValues();
  let co = { pA: [] }
  vs.forEach((r, i) => {
    let p = `${r[0]}/${r[2]}`;
    if (!co.hasOwnProperty(p)) {
      co[p] = { cnt: 1, sum: r[4], idx: i }
      co.pA.push(p);
    } else {
      co[p].cnt += 1;
      co[p].sum += r[4];
    }
  });
  let vo = vs.map((r, i) => {
    let p = `${r[0]}/${r[2]}`;
    if (i == co[p].idx) {
      return [co[p].sum / co[p].cnt];
    } else {
      return [''];
    }
  })
  sh.getRange(2, 6, vo.length, vo[0].length).setValues(vo);
}

输出:

Candidate Position Interview Round Panelist Round Score Round Average Score
Bob Tester First Jon 3 4
Bob Tester First Janet 4
Bob Tester First Joe 5
Bob Tester Second Sal 4 3.333333333
Bob Tester Second Riley 3
Bob Tester Second Tae 3
Bob Tester Final Wanda 5 4.666666667
Bob Tester Final Kelly 4
Bob Tester Final Arnold 5
Al Senior Tester First Ben 2 3
Al Senior Tester First Tori 3
Al Senior Tester First Harry 4
Al Senior Tester Second Kate 4 3.666666667
Al Senior Tester Second Wendy 5
Al Senior Tester Second Carl 2
Al Senior Tester Final Sam 5 4
Al Senior Tester Final Jake 3
Al Senior Tester Final Troy 4