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

蒂埃里·安德里亚茨瓦里森

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

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

最终目标是将每个结果数组的值存储在文本文件中,然后上传到 Drive 文件夹中。

在此先感谢您的帮助,

预期 Array1 作为 txt 文件中的示例

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

编辑,从@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")));
}

目前,预期的拆分文件结果如下:更新后的预期文件结果

Tanaike

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

示例脚本:

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.txt, Array2.txt,,,.
  • 从您的显示图像中,标题行被放置到每个文本文件中。如果您不想包含标题,请修改[header[0], ...e].join("\n")e.join("\n").

笔记:

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

参考:

添加:

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

你能再帮忙吗,我改变了电子表格中列的配置:docs.google.com/spreadsheets/d/…。我想在每个最终文件中包含 A 列和 C 列,并且 cumul 现在基于 E 列。最终文件将是一个 .TSV 文件。你能再帮忙吗?

示例脚本如下。

示例脚本:

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

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

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

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

从Google应用程序脚本中的dropdownlist值运行函数

Google应用脚本验证

Google应用脚本,Gmail插件获取TextInput值

根据熊猫中另一列的值在groupby之后应用lambda函数

根据来自另一列的值在新的dataframe列中应用函数

需要Google表格中的简单replaceInCell应用程序脚本

熊猫根据另一列中的值应用基于值的值

Google表格应用脚本:您可以将数组写到一行而不是一列吗?

应用脚本可强制Google表格中的特定行高

提高Google表格中应用脚本代码的加载效率

如何从Google表格的应用脚本中的过滤行中提取单个数组值

Google Web脚本应用中的循环问题

Google脚本:将列中的新值附加到另一个工作表

Google App脚本:根据列中的值将行(仅行中选定的列)移至另一个标签

替换Google脚本应用中的单元格值

Google表格应用脚本:条件隐藏或显示列

Google表格/应用脚本中奇怪的返回范围结果

使用Google查询作为数组公式,根据另一列中的信息对一列中的值求和

应用特定功能根据数据框中另一列的条件替换列的值

Google应用程序脚本中的JSON .concat()函数

将网络表单值返回到Google应用脚本

根据另一列分配列值(拆分应用组合)

在 Google Sheet 应用中运行应用脚本

返回空值的 Google 应用脚本 html 模板

用于按条件隐藏列的 Google 应用程序脚本

在 Google 应用程序脚本中执行节点 JS 代码

使用 Google 表格中的应用脚本获取网站数据

比较来自 Google Web 应用脚本中 url 的文本

如何使用 google app 脚本根据我在另一列上设置的条件从一列中获取值?