根据另一列中的累积值拆分数组值 + Google 应用程序脚本

Split array values based on cumulative values in another column + Google app script

当我们在 B 列中达到累积值 = 50 时,我正在尝试使用 Google App Script 来拆分此电子表格的 A 列。如果cumulative 不是完全匹配,我们需要在 cumulative 值低于 50 的最后一行停止每次拆分。 我在 C 列中添加了预期的拆分数组结果。

这是示例电子表格:https://docs.google.com/spreadsheets/d/1_8ZRTxd64qbxCHrhwDoo4ugWHy7jG1VIKv8hHjtp3Bw/edit#gid=0

最终目标是将每个结果数组的值存储在文本文件中,并上传到云端硬盘文件夹中。

在此先感谢您的帮助,

expected Array1 as example in a txt file

==========================================

编辑,从@Tanaike 脚本,我更新了脚本如下但没有运气:

function test2() {
  var raw_values = SpreadsheetApp.getActive().getSheetByName("Sheet1").getRange("A2:J").getValues();
  var values = raw_values.map((x) => [x[0], x[2], x[4]])
  var destFolderID = "1Qq52QRpYYG_T2AxNWDz0rykZbAGhdpoe";
  var fileName = "sample";
  createTsv_new(values, destFolderID, fileName)
}

function createTsv_new(values, destFolderID, fileName) {
  const folderId = destFolderID; // Please set the folder ID you want to put the created files.
  const { res } = values.reduce((o, [s, a, b], i, v) => {
    o.tempC += b;
    o.tempAr.push([s, a]);
    if (o.tempC + (v[i + 1] ? v[i + 1][4] : 0) > 50 || i == v.length - 1) {
      o.res.push(o.tempAr);
      o.tempAr = [];
      o.tempC = 0;
    }
    return o;
  }, { res: [], tempAr: [], tempC: 0 });
  if (res.length == 0) return;
  res.forEach((e, i) => DriveApp.getFolderById(folderId).createFile(fileName + (i + 1) + ".tsv", e.join("\t")));
}

目前,预期的拆分文件结果如下:updated expected file result

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

示例脚本:

function myFunction1() {
  const folderId = "root"; // Please set the folder ID you want to put the created files.

  // 1. Retrieve values from Spreadsheet.
  const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1");
  const range = sheet.getRange("A1:B" + sheet.getLastRow());
  const [header, ...values] = range.getValues();
  
  // 2. Create an array including the separated rows.
  const { res } = values.reduce((o, [a, b], i, v) => {
    o.tempC += b;
    o.tempAr.push(a);
    
    // Here, the rows are separated.
    if (o.tempC + (v[i + 1] ? v[i + 1][1] : 0) > 50 || i == v.length - 1) {
      o.res.push(o.tempAr);
      o.tempAr = [];
      o.tempC = 0;
    }
    return o;
  }, { res: [], tempAr: [], tempC: 0 });
  
  // 3. Create text files using the created array.
  if (res.length == 0) return;
  res.forEach((e, i) => DriveApp.getFolderById(folderId).createFile(`Array${i + 1}.txt`, [header[0], ...e].join("\n")));
}
  • 当此脚本为您提供的电子表格 运行 时,将从“A”和“B”列中检索值。并且,创建一个包含分隔行的数组。并且,使用数组创建文本文件。
  • 从您的展示图片来看,文件名类似于 Array1.txtArray2.txt、、、.
  • 从您的显示图像中,header 行被放入每个文本文件。如果您不想包含 header,请将 [header[0], ...e].join("\n") 修改为 e.join("\n")

注:

  • 此示例脚本适用于您提供的电子表格。因此,当您更改电子表格时,此脚本可能无法使用。请注意这一点。

参考文献:

已添加:

关于您接下来的第二个新问题,

can you please help again, I change a bit the disposition of columns in the spreadsheet : docs.google.com/spreadsheets/d/…. I would like to include column A and column C in each final file, and the cumul is now based on column E. The final file would be a .TSV file. Can you please help again?

示例脚本如下

示例脚本:

function myFunction2() {
  const folderId = "root"; // Please set the folder ID you want to put the created files.

  // 1. Retrieve values from Spreadsheet.
  const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1");
  const range = sheet.getRange("A1:E" + sheet.getLastRow());
  const [header, ...values] = range.getValues();

  // 2. Create an array including the separated rows.
  const { res } = values.reduce((o, [a, , c, , e], i, v) => {
    o.tempC += e;
    o.tempAr.push([a, c].join("\t"));

    // Here, the rows are separated.
    if (o.tempC + (v[i + 1] ? v[i + 1][4] : 0) > 50 || i == v.length - 1) {
      o.res.push(o.tempAr);
      o.tempAr = [];
      o.tempC = 0;
    }
    return o;
  }, { res: [], tempAr: [], tempC: 0 });

  // 3. Create text files using the created array.
  if (res.length == 0) return;
  res.forEach((e, i) => DriveApp.getFolderById(folderId).createFile(`Array${i + 1}.txt`, [[header[0], header[2]].join("\t"), ...e].join("\n")));
}