【问题标题】:Python Google Spreadsheet API sorting not effectivePython Google电子表格API排序无效
【发布时间】:2021-01-26 13:53:45
【问题描述】:

我使用 google 示例 https://developers.google.com/sheets/api/samples/charts(只是为了测试目的切换第 3 行和第 4 行)用 Python v3.7.8 测试 Google Spreadshhet API v4:

  • 柱形图的创建
  • 对“A”列进行排序

数据存储在我创建柱形图的谷歌驱动器中的电子表格中。

柱形图已创建,但“A”列未排序(升序)。以下代码:

from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request

...

body = {'requests': [
    {'addChart': 
     {'chart': 
      {'spec': 
       {'title': 'Model Q1 Sales', 
        'titleTextPosition': {'horizontalAlignment': 'CENTER'}, 
        'basicChart': 
        {'domains': [
            {'domain': 
             {'sourceRange': 
              {'sources': [
                  {'sheetId': 909072886, 
                   'startRowIndex': 0, 
                   'endRowIndex': 7, 
                   'startColumnIndex': 0, 
                   'endColumnIndex': 1}]}}}], 
         'series': [
             {'series': 
              {'sourceRange': 
               {'sources': [
                   {'sheetId': 909072886, 
                    'startRowIndex': 0, 
                    'endRowIndex': 7, 
                    'startColumnIndex': 1, 
                    'endColumnIndex': 2}]}}, 
              'targetAxis': 'LEFT_AXIS'}, 
             {'series': 
              {'sourceRange': 
               {'sources': [
                   {'sheetId': 909072886, 
                    'startRowIndex': 0, 
                    'endRowIndex': 7, 
                    'startColumnIndex': 2, 
                    'endColumnIndex': 3}]}}, 
              'targetAxis': 'LEFT_AXIS'},   
             {'series': 
              {'sourceRange': 
               {'sources': [
                   {'sheetId': 909072886, 
                    'startRowIndex': 0, 
                    'endRowIndex': 7, 
                    'startColumnIndex': 3, 
                    'endColumnIndex': 4}]}}, 
              'targetAxis': 'LEFT_AXIS'}], 
         'legendPosition': 'BOTTOM_LEGEND', 
         'axis': [
             {'position': 'BOTTOM_AXIS', 
              'title': 'Model Numbers', 
              'titleTextPosition': {'horizontalAlignment': 'CENTER'}}, 
             {'position': 'LEFT_AXIS', 
              'title': 'Sales', 
              'titleTextPosition': {'horizontalAlignment': 'CENTER'}}], 
         'headerCount': 1, 
         'chartType': 'COLUMN',  
         'stackedType': 'STACKED'}, 
        'sortSpecs': [
            {'dimensionIndex': 0, 
             'sortOrder': 'ASCENDING'}]}, 
       'position': 
       {'overlayPosition': 
        {'anchorCell': 
         {'sheetId': 909072886, 
          'rowIndex': 7, 
          'columnIndex': 6}
        }
       }
      }
     }
    }
]
}

response = service.spreadsheets().batchUpdate(spreadsheetId=file_id, body=body).execute()

我应该得到以下信息:

但我明白了:排序部分没有影响。

备注:我也试过sortSpecs没有成功

'dataSourceColumnReference': {'name': 'A'}

代替

'dimensionIndex': 0

不要犹豫,支持我!提前谢谢你。

【问题讨论】:

    标签: python api sorting google-sheets google-sheets-api


    【解决方案1】:

    问题:

    添加图表时,sortSpecs 仅支持来自外部数据源的数据。来自API Reference

    sortSpecs[]:图表数据的排序顺序。仅支持单个排序规范。 仅支持数据源图表

    解决办法:

    您应该对数据本身进行排序。您可以通过将sortRange 请求添加到您的batchUpdate 以编程方式执行此操作:

    body = {
        'requests': [
            {
                'sortRange': {
                    'range': {
                        'sheetId': 909072886,
                        'startRowIndex': 1,
                        'endRowIndex': 7,
                        'startColumnIndex': 0,
                        'endColumnIndex': 5
                    },
                    'sortSpecs': [
                        {
                            'sortOrder': 'ASCENDING',
                            'dimensionIndex': 0
                        }
                    ]
                }
            },
            {
                'addChart': {
                    # Add chart request body
                }
            }
        ]
    }
    

    参考:

    【讨论】:

    • @lamblichus,感谢您的快速回答。我会测试你的代码。但是,我想知道如何在图表本身中执行此操作,因为它在 [参考](developers.google.com/sheets/api/reference/rest/v4/spreadsheets/…) 中进行了解释。可能我没看懂……
    • @hganger 我不认为你可以只在图表中排序,而不是在相应的数据范围内。 sortSpecs 仅适用于外部数据源,如我提供的参考资料中所述。
    • @lamblichus。我已经测试过,但是 service.spreadsheets().batchUpdate(spreadsheetId=file_id, body=body).execute() 会产生错误:HttpError 400 when requesting sheets.googleapis.com/v4/spreadsheets******* ************************:batchUpdate?alt=json 返回“'requests[0]' (oneof) 处的值无效,oneof 字段 'kind' 已设置. 无法设置 'addChart'"。详细信息:“[{'@type': 'type.googleapis.com/google.rpc.BadRequest', 'fieldViolations': [{'field': 'requests[0]', 'description': “无效值在' requests[0]' (oneof),oneof 字段 'kind' 已设置。无法设置'addChart'"}]}]">
    • @hganger 我无法重现这个。使用它时我没有收到任何错误,请求工作成功。您需要添加与addChart请求对应的代码(您共享的代码,但不需要sortSpecs部分)。你这样做了吗?
    • @hganger 很抱歉,在将我的代码复制到答案时,我搞砸了 { } 的开头/结尾。我想我现在修好了,让我知道它是否有效。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-04-18
    • 2023-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-04
    • 1970-01-01
    相关资源
    最近更新 更多