根据映射 table 替换 google sheet 中的数据

Replace data in google sheet based on a mapping table

Sheet1 是正在更改的 sheet-

Sheet2 正在指导更改

我想用 Sheet2 的 B 列替换 Sheet1 的 A 列中的数据。基本上与 VLOOKUP 类似,但是,如果我使用 VLOOKUP,我将需要创建额外的列来使用该公式,这不是目标。

预期结果表 1:

在您的情况下,下面的示例脚本怎么样?

示例脚本:

请为 sheet1sheet2 设置 sheet 名称。在这种情况下,sheet1sheet2 分别是您问题中的 Sheet1 is the sheet that's changing-Sheet2 is directing the change

function myFunction() {
  const sheet1 = "Sheet1"; // Please set sheet name.
  const sheet2 = "Sheet2"; // Please set sheet name.

  const ss = SpreadsheetApp.getActiveSpreadsheet();
  const [s1, s2] = [sheet1, sheet2].map(s => ss.getSheetByName(s));
  const s1Range = s1.getRange("A1:A" + s1.getLastRow());
  const s2Obj = s2.getRange("A1:B" + s2.getLastRow()).getValues().reduce((o, [a, b]) => (o[a] = b, o), {});
  const res = s1Range.getValues().map(([a]) => [s2Obj[a] || a]);
  s1Range.setValues(res);
}
  • 当此脚本为 运行 时,sheet1" is modified using the values of columns "A" and "B" of sheet2` 的“A”列。

参考文献: