如何使用Google Script展开Google SpreadSheet的所有数据透视表?

huangapple go评论60阅读模式
英文:

How can expand all PivotTable of Google SpreadSheet by Google Script?

问题

我想通过脚本展开所有在电子表格中折叠的行:

我有这段代码:

var pivotTables = sheet.getPivotTables();

for (var i = 0; i < pivotTables.length; i++) {
  var pivotTable = pivotTables[i];
  var rows = pivotTable.getRowGroups();
  for (var j = 0; j < rows.length; j++) {
    rows[j].expand();
  }
}

但出现错误:TypeError: rows[j].expand is not a function

如何通过Google Script展开Google电子表格的所有透视表?

英文:

I want expand all rows had Collapse in a SpreadSheet by Script:

I had this code:

var pivotTables = sheet.getPivotTables(); 
  
  for (var i = 0; i &lt; pivotTables.length; i++) {
    var pivotTable = pivotTables[i];
    var rows = pivotTable.getRowGroups();
    for (var j = 0; j &lt; rows.length; j++) {
      rows[j].expand();
    }
  }

But occur error: TypeError: rows[j].expand is not a function

How can expand all PivotTable of Google SpreadSheet by Google Script?

答案1

得分: 2

修改点:

  • 不幸的是,似乎 SpreadsheetApp.PivotGroup 没有 expand 方法。链接
  • 我之前在寻找展开和折叠行列组的方法,但不幸的是,我找不到。但幸运的是,我确认了当使用 Sheets API 时,可以实现展开和折叠数据透视表的行列组。

基于以上情况,我想建议使用 Sheets API 来实现。示例脚本如下。

示例脚本:

请复制并粘贴以下脚本到电子表格的脚本编辑器中。在使用此脚本之前,请启用高级 Google 服务中的 Sheets API

并且,请设置您的工作表名称并保存脚本。

function myFunction() {
  var sheetName = "Sheet1"; // 请设置您的工作表名称。

  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var ssId = ss.getId();
  var sheet = ss.getSheetByName(sheetName);
  var sheetId = sheet.getSheetId();
  
  // 使用 Sheets API 检索数据透视表。
  var obj = Sheets.Spreadsheets.get(ssId, { ranges: [sheetName], fields: "sheets(data(rowData(values(pivotTable))))" }).sheets[0];

  // 通过展开行组创建请求主体。
  var requests = obj.data[0].rowData.reduce((ar, r, i) => {
    if (r.values) {
      r.values.forEach((c, j) => {
        if (c.pivotTable) {
          var pivotTable = c.pivotTable;
          ["rows"].forEach(e => { // 如果要展开行和列,请使用 ["rows", "columns"].forEach(e => {
            if (pivotTable[e]) {
              pivotTable[e].forEach(pr => {
                if (pr.valueMetadata) {
                  pr.valueMetadata.forEach(vm => vm.collapsed = false);
                }
              });
              ar.push({
                updateCells: {
                  range: { sheetId, startRowIndex: i, endRowIndex: i + 1, startColumnIndex: j, endColumnIndex: j + 1 },
                  rows: [{ values: [{ pivotTable }] }],
                  fields: "pivotTable"
                }
              });
            }
          });
        }
      });
    }
    return ar;
  }, []);
  if (requests.length == 0) return;
  
  // 请求 Sheets API 的 batchUpdate 方法。
  Sheets.Spreadsheets.batchUpdate({ requests }, ssId);
}
  • 运行此脚本时,将展开所有数据透视表的行组。
  • 如果您想展开行组和列组,请将 [&quot;rows&quot;].forEach(e =&gt; { 修改为 [&quot;rows&quot;, &quot;columns&quot;].forEach(e =&gt; {
  • 如果您想折叠所有行组,请将 pr.valueMetadata.forEach(vm =&gt; vm.collapsed = false); 修改为 pr.valueMetadata.forEach(vm =&gt; vm.collapsed = true);

参考:

英文:

Modification points:

  • Unfortunately, it seems that SpreadsheetApp.PivotGroup has no method of expand. Res
  • I was looking for the method for expanding and collapsing the groups of rows and columns before. But, unfortunately, I couldn't find it. But, fortunately, I confirmed that when Sheets API is used, expanding and collapsing the groups of rows and columns of the pivot table can be achieved.

From the above situation, in this answer, I would like to propose using Sheets API. The sample script is as follows.

Sample script:

Please copy and paste the following script to the script editor of Spreadsheet. Before you use this script, please enable Sheets API at Advanced Google services.

And, please set your sheet name and save the script.

function myFunction() {
  var sheetName = &quot;Sheet1&quot;; // Please set your sheet name.

  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var ssId = ss.getId();
  var sheet = ss.getSheetByName(sheetName);
  var sheetId = sheet.getSheetId();
  
  // Retrieve pivot table using Sheets API.
  var obj = Sheets.Spreadsheets.get(ssId, { ranges: [sheetName], fields: &quot;sheets(data(rowData(values(pivotTable))))&quot; }).sheets[0];

  // Create request body by expanding groups of rows.
  var requests = obj.data[0].rowData.reduce((ar, r, i) =&gt; {
    if (r.values) {
      r.values.forEach((c, j) =&gt; {
        if (c.pivotTable) {
          var pivotTable = c.pivotTable;
          [&quot;rows&quot;].forEach(e =&gt; { // If you want to expand both rows and columns, please use [&quot;rows&quot;, &quot;columns&quot;].forEach(e =&gt; {
            if (pivotTable[e]) {
              pivotTable[e].forEach(pr =&gt; {
                if (pr.valueMetadata) {
                  pr.valueMetadata.forEach(vm =&gt; vm.collapsed = false);
                }
              });
              ar.push({
                updateCells: {
                  range: { sheetId, startRowIndex: i, endRowIndex: i + 1, startColumnIndex: j, endColumnIndex: j + 1 },
                  rows: [{ values: [{ pivotTable }] }],
                  fields: &quot;pivotTable&quot;
                }
              });
            }
          });
        }
      });
    }
    return ar;
  }, []);
  if (requests.length == 0) return;
  
  // Request the batchUpdate method of Sheets API.
  Sheets.Spreadsheets.batchUpdate({ requests }, ssId);
}
  • When this script is run, all row groups of all pivot tables are expanded.
  • When you want to expand both row groups and column groups, please modify [&quot;rows&quot;].forEach(e =&gt; { to [&quot;rows&quot;, &quot;columns&quot;].forEach(e =&gt; {.
  • If you want to collapse all row groups, please modify pr.valueMetadata.forEach(vm =&gt; vm.collapsed = false); to pr.valueMetadata.forEach(vm =&gt; vm.collapsed = true);.

References:

huangapple
  • 本文由 发表于 2023年5月11日 18:00:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/76226400.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定