在表格中将多列中的字符串拆分为多行 - Google Apps 脚本

Split strings in multiple columns into multiple rows in Sheets - Google Apps Script

我正在尝试重新使用过去在我的 sheet 中运行良好的脚本,但当脚本被触发到 运行 时出现错误。错误消息是“自定义函数参数太大”。

const result = range =>
  range.flatMap(([a, b, ...v]) => {
    const { vv, len } = v.reduce((o, c) => {
      const t = typeof c != "string" ? c.toString().split(", ") : c.split(", ");
      o.vv.push(t);
      o.len = o.len < t.length ? t.length : o.len;
      return o;
    }, { vv: [], len: 0 });
    const temp = vv.map(e => e.concat(Array(len - e.length).fill("")));
    return temp[0].map((_, i) => [...(i == 0 ? [a,] : Array(1).fill("")), b, ...temp.map(r => isNaN(r[i].trim()) ? r[i].trim() : r[i].trim() && Number(r[i]))]);
  });

  // Credits: Tanaike (

似乎错误发生是因为我有太多数据行(数据行是 Google 表单响应,到目前为止我有接近 20000 个响应)。有没有人对我可以尝试解决这个问题有什么建议?我实际上不熟悉 Google Apps 脚本,如果有人有任何提示,我将不胜感激,谢谢!

在您的情况下,我认为可能需要使用 Spreadsheet 服务(SpreadsheetApp)and/or Sheets API 来放置这些值的自定义功能。那么,下面的示例脚本怎么样?

模式一:

在此模式中,使用 Spreadsheet 服务 (SpreadsheetApp) 放置值。请将以下脚本复制并粘贴到 Spreadsheet 的脚本编辑器中。并且,请设置 sheet 源名称和目标名称 sheet。并且,运行 sample1 使用脚本编辑器。

function sample1() {
  const ss = SpreadsheetApp.getActiveSpreadsheet();
  const srcSheet = ss.getSheetByName("Sheet1"); // Please set the source sheet name.
  const dstSheet = ss.getSheetByName("Sheet2"); // Please set the destination sheet name.
  const values = srcSheet.getDataRange().getValues();
  const res = values.flatMap(([a, b, c, d, e, f, g, ...v]) => {
    const { vv, len } = v.reduce((o, c) => {
      const t = typeof c != "string" ? c.toString().split(",") : c.split(",");
      o.vv.push(t);
      o.len = o.len < t.length ? t.length : o.len;
      return o;
    }, { vv: [], len: 0 });
    const temp = vv.map(e => e.concat(Array(len - e.length).fill("")));
    return temp[0].map((_, i) => [...(i == 0 ? [a, b, c, d] : Array(4).fill("")), e, f, g, ...temp.map(r => isNaN(r[i].trim()) ? r[i].trim() : r[i].trim() && Number(r[i]))]);
  });
  dstSheet.getRange(1, 1, res.length, res[0].length).setValues(res);
}

模式二:

在此模式中,值是使用工作表 API 放置的。请将以下脚本复制并粘贴到 Spreadsheet 的脚本编辑器中。并且,请设置 sheet 源名称和目标名称 sheet。而且,please enable Sheets API at Advanced Google services。 运行 sample1 使用脚本编辑器。

function sample2() {
  const srcSheet = "Sheet1"; // Please set the source sheet name.
  const dstSheet = "Sheet2"; // Please set the destination sheet name.

  const ss = SpreadsheetApp.getActiveSpreadsheet();
  const ssId = ss.getId();
  const values = Sheets.Spreadsheets.Values.get(ssId, srcSheet).values;
  const res = values.flatMap(([a, b, c, d, e, f, g, ...v]) => {
    const { vv, len } = v.reduce((o, c) => {
      const t = typeof c != "string" ? c.toString().split(",") : c.split(",");
      o.vv.push(t);
      o.len = o.len < t.length ? t.length : o.len;
      return o;
    }, { vv: [], len: 0 });
    const temp = vv.map(e => e.concat(Array(len - e.length).fill("")));
    return temp[0].map((_, i) => [...(i == 0 ? [a, b, c, d] : Array(4).fill("")), e, f, g, ...temp.map(r => isNaN(r[i].trim()) ? r[i].trim() : r[i].trim() && Number(r[i]))]);
  });
  Sheets.Spreadsheets.Values.update({ values: res }, ssId, dstSheet, { valueInputOption: "USER_ENTERED" });
}

参考文献: