我使用下面的代码通过引用工作表id自动创建透视表。我想知道如何使用“总计”列对透视表进行排序。当我进入电子表格时,我可以手动完成,但我想自动完成。我查看了googlesheets文档HERE,但没能弄清楚。也许这和创造一个桶有关,但不确定。在
这是我当前没有值部分排序的工作代码:
{
"requests": [
{
"updateCells": {
"rows": {
"values": [
{
"pivotTable": {
"source": {
"sheetId": source_sheet_id,
"startRowIndex": 0,
"startColumnIndex": 0,
"endRowIndex": max_row_count,
"endColumnIndex": 3 #total columns
},
"rows": [
{
"sourceColumnOffset": code_index,
"showTotals": True,
"sortOrder": "ASCENDING",
"valueBucket": {
"buckets": [
{
"stringValue": "West"
}
]
}
}
],
"columns": [
{
"sourceColumnOffset": month_index,
"sortOrder": "ASCENDING",
"showTotals": True,
"valueBucket": {
"buckets": [
{
"stringValue": "Panel"
}
]
},
}
],
"values": [
{
"summarizeFunction": "SUM",
"sourceColumnOffset": counts_index
}
],
"valueLayout": "HORIZONTAL"
}
}
]
},
"start": {
"sheetId": destination_sheet_id,
"rowIndex": 0,
"columnIndex": 0
},
"fields": "pivotTable"
}
}
]
}
向以下url发送POST请求(填写电子表格ID):https://sheets.googleapis.com/v4/spreadsheets/spreadsheetId:batchUpdate
包含以下有效负载(为总计列号插入实际值):
请参阅此处获取完整的文档:https://developers.google.com/sheets/api/samples/data(最下面的示例)
我也遇到过同样的问题,经过长时间的研究,我解决了这个问题。在
诀窍是在每行中添加一个空的“valueBucket”键,如下所示:
然后,这些行将按总计列排序。在
我在这里找到了答案:https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets#SortOrder
它说:“例如,在一个包含一个行组和两个列组的透视表中,行组最多可以列出两个值。第一个值对应于第一列组中的值,第二个值对应于第二列组中的值。如果未列出任何值,则表示应根据列组上的“总计”对行进行排序。如果只列出一个值,则相当于使用该存储桶的“总计”
在您的例子中,数据是从这里获取的:https://developers.google.com/sheets/api/samples/pivot-tables
检查此表示例:https://docs.google.com/spreadsheets/d/1f5s03qLAVaLfUfkyvjZtllVNz0FxAmn2jEmL9wYVGh8/edit?usp=sharing
在本例中,此函数基于您的代码,使用按Grand Total列排序的值创建了pivot表。在
^{pr2}$希望这有帮助!在
相关问题 更多 >
编程相关推荐