如何根据“名称”列值将一个选项卡中的表格数据拆分为多个单独的选项卡?

问题描述 投票:0回答:1

假设我有以下名为“table_raw”的示例 Google Sheets 表:

姓名 日期 价值 Wtd
吉姆 2023年6月11日 3 0.75 44
吉姆 2023年6月11日 2 1 2
2023年6月11日 5 1.3 37
2023 年 11 月 13 日 6 1 28
2023 年 11 月 13 日 8 0.63 1
2023 年 11 月 13 日 0 1.1 13
克洛伊 2023年12月14日 2 0.7 40
克洛伊 2023年12月14日 4 1.9 33
克洛伊 2023年12月14日 1 2.2 2

我想根据“名称”列将此数据拆分为单独的选项卡(在同一 Google 表格工作簿内)。具体来说,我的 Google 表格中有 3 个选项卡,分别名为 Jim、Dan 和 Chloe。我想要:

  • 删除这 3 个现有选项卡中的所有数据
  • 用相应名称的“table_raw”数据填充每个选项卡

举个例子,Jim 选项卡现在看起来像这样:

| Name  | Date       | Value | Wtd  | Count |
|-------|------------|-------|------|------:|
| Jim   |  11/6/2023 |     3 | 0.75 |    44 |
| Jim   |  11/6/2023 |     2 |    1 |     2 |

Chloe Google Sheet 选项卡将如下所示:

| Name  | Date       | Value | Wtd  | Count |
| Chloe | 12/14/2023 |     2 |  0.7 |    40 |
| Chloe | 12/14/2023 |     4 |  1.9 |    33 |
| Chloe | 12/14/2023 |     1 |  2.2 |     2 |

根据我的理解,这是我可以用 AppsScript 和 JS 来做的事情?在网上搜索后,老实说我不知道该怎么做。任何建议将不胜感激。

google-apps-script google-sheets split tabs
1个回答
0
投票

按名称选择工作表并在每个工作表中加载相应的行

function sortof() {
  const ss = SpreadsheetApp.getActive();
  const sh = ss.getSheetByName("Sheet0");
  const [hs,...vs] = sh.getRange(1, 1, sh.getLastRow() - 1, sh.getLastColumn()).getValues();
  vs.reduce((a,r,i) => {
    a[r[0]].push(r);
    return a;
  },{sA:["Jim","Dan","Chloe"],"Jim":[],"Dan":[],"Chloe":[],"loadData":function(){this.sA.forEach(name => {
    let sh = ss.getSheetByName(name);
    sh.clearContents();
    this[name].unshift(hs);
    sh.getRange(1,1,this[name].length,this[name][0].length).setValues(this[name]);
  })}}).loadData()
}
© www.soinside.com 2019 - 2024. All rights reserved.