【问题标题】:Filtering a Pivot Table in Google Scripts在 Google 脚本中过滤数据透视表
【发布时间】:2018-01-29 19:59:19
【问题描述】:

这是我在这里的第一篇文章,我是编码新手。我的任务是创建一个自动报告,该报告将向谷歌表单提交者发送一个图表,以帮助他们监控他们的生产与他们的日常目标。为此,我正在使用新的开发人员 Google 表格脚本来刷新数据透视表。我在网上找到了这段代码,效果很好,但是,我想添加一行,它将根据唯一提交者的数据进行过滤。这是我到目前为止的代码:

function updatePivotTable() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var pivotTableSheetName = "Lunch Chart";
  var pivotTableSheetId = ss.getSheetByName(pivotTableSheetName).getSheetId();
  var fields = "sheets(properties.sheetId,data.rowData.values.pivotTable)";
  var sheets = Sheets.Spreadsheets.get(ss.getId(), {fields: fields}).sheets; 
  for (var i in sheets) {
  if (sheets[i].properties.sheetId == pivotTableSheetId) {
  var pivotTableParams = sheets[i].data[0].rowData[0].values[0].pivotTable;
  break;
}
}

// Update source range:
pivotTableParams.source.endRowIndex = 40;

// Send back the updated params
var request = {
"updateCells": {
  "rows": {
    "values": [{
      "pivotTable": pivotTableParams
    }]
  },
  "start": {
    "sheetId": pivotTableSheetId
  },
  "fields": "pivotTable"
}
};

Sheets.Spreadsheets.batchUpdate({'requests': [request]}, ss.getId());
}

这可能吗?我应该在哪里添加过滤器?我在谷歌开发者网站上找到了这个,但我对编码很陌生,所以我真的不知道把它放在哪里或如何使它有条件。 https://developers.google.com/sheets/api/reference/rest/v4/FilterCriteria

谢谢!

【问题讨论】:

  • 您可以查看此post,该issue 提交了有关枢轴过滤条件的issue。您可能希望按照此更新。
  • 谢谢你的链接,我还没有制作我的过滤器代码,但似乎没关系,因为它无论如何都不能正常工作。希望比我更聪明的人能找到解决此问题的方法。

标签: google-apps-script google-sheets pivot-table google-sheets-api


【解决方案1】:

我不确定这是否仍然与您相关,但您可以使用以下代码添加过滤器-

"criteria": {
            <col_index>: {"visibleValues": <filter criteria>},
            <col_index>: {"visibleValues": <filter criteria>},

【讨论】:

  • 您可以为此添加对文档的引用吗?
【解决方案2】:

我遇到了同样的问题,但没有找到简单的解释或代码。这是我所做的并且它有效:

function updatePivotTable() {
  var ss = SpreadsheetApp.openById(SHEET_ID);
  var pivotTableSheetName = "Pivot";
  var pivotTableSheetId = ss.getSheetByName(pivotTableSheetName).getSheetId();
  
  var fields = "sheets(properties.sheetId,data.rowData.values.pivotTable)";
  var sheets = Sheets.Spreadsheets.get(ss.getId(), {fields: fields}).sheets; 
  for (var i in sheets) {
    if (sheets[i].properties.sheetId == pivotTableSheetId) {
      var pivotTableParams = sheets[i].data[0].rowData[0].values[0].pivotTable;
      break;
    }
  }
  
  // Update source range:
  pivotTableParams.source.endRowIndex = 111;
  pivotTableParams.criteria = { 3: {"visibleValues": ["foo"]}};
  
  // Send back the updated params
  var request = {
    "updateCells": {
      "rows": {
        "values": [{
          "pivotTable": pivotTableParams
        }]
      },
      "start": {
        "sheetId": pivotTableSheetId
      },
      "fields": "pivotTable",     
    }
  };
  
  Sheets.Spreadsheets.batchUpdate({'requests': [request]}, ss.getId());
}

因此,基本上您需要将条件作为 pivotTableParams 传递,其中对象的键是您要查询的列的索引,值应作为格式为 {"visibleValues": ["foo"]} 的另一个对象传递。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-31
    • 1970-01-01
    • 1970-01-01
    • 2012-08-25
    相关资源
    最近更新 更多