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

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

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 (https://stackoverflow.com/questions/70078195/how-to-split-strings-in-multiple-columns-into-multiple-rows/70081177?noredirect=1#comment123884616_70081177)

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

Tanaike

在您的情况下,我认为可能需要使用电子表格服务 (SpreadsheetApp) 和/或 Sheets API 而不是自定义函数来放置这些值。那么,下面的示例脚本怎么样?

模式一:

在此模式中,使用电子表格服务 (SpreadsheetApp) 放置值。请将以下脚本复制并粘贴到电子表格的脚本编辑器中。并且,请设置源表和目标表的表名。并且,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);
}

模式二:

在此模式中,使用 Sheets API 放置值。请将以下脚本复制并粘贴到电子表格的脚本编辑器中。并且,请设置源表和目标表的表名。并且,请在 Advanced Google services 中启用 Sheets APIsample1使用脚本编辑器运行。

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" });
}

参考:

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章