简体   繁体   English

如何从Google Sheets API v4中的单元格获取格式信息的Python示例?

[英]Python example of how to get formatting information from a cell in Google Sheets API v4?

I've been trying to write my own Google Sheets wrapper, and it's been a frustrating experience so far. 我一直在尝试编写自己的Google表格包装,到目前为止,这种经历令人沮丧。 The thing I'm stuck on at the moment is how to get a symmetrical in / out format of sheet data. 我目前遇到的问题是如何获取工作表数据的对称输入/输出格式。

Basically, I want to call values().get(), alter the resulting hash, and send that same hash back up to update(). 基本上,我想调用values()。get(),更改生成的哈希,然后将相同的哈希发送回update()。

I'm happy to write my own solution to process or coerce the output of values().get() to the structure that batchUpdate() needs, but I need the formatting information of each of the cells to do that . 我很高兴编写自己的解决方案来将values()。get()的输出处理或强制转换为batchUpdate()所需的结构,但是我需要每个单元格的格式设置信息来做到这一点

batchUpdate() expects formatting information like this: batchUpdate()需要这样的格式信息:

bod = {
    'updateCells': {
        'start': {
            'sheetId': 0,
            'rowIndex': 7,
            'columnIndex': 0
        },
        'rows': [
            {
                'values': [
                    {
                        "userEnteredValue": {
                            'stringValue': 'LOL'
                            }, 
                        "userEnteredFormat": {
                            'backgroundColor': {
                                'red': .2,
                                'blue': .75,
                                'green': .75
                            }
                        }
                    },
                    {
                        "userEnteredValue": {
                            'stringValue': 'LOL2'
                            }, 
                        "userEnteredFormat": {
                            'backgroundColor': {
                                'red': .2,
                                'blue': 1,
                                'green': .75
                            }
                        }
                    },
                    {
                        "userEnteredValue": {
                            'stringValue': 'LOL3'
                            }, 
                        "userEnteredFormat": {
                            'backgroundColor': {
                                'red': .2,
                                'blue': 1,
                                'green': 1
                            }
                        }
                    }                    
                ]
            }
        ],
        'fields': 'userEnteredValue,userEnteredFormat.backgroundColor'
    }
}

How I'm retrieving values currently looks something like this: 我现在如何检索值看起来像这样:

import requests
import json
from oauth2client.service_account import ServiceAccountCredentials
from apiclient.discovery import build

#Set up credentials object
auth_key_url = "<JSON CREDENTIALS FILE>"
file_contents = requests.get(auth_key_url).content
key_dict = json.loads(file_contents)
creds = ServiceAccountCredentials.from_json_keyfile_dict(key_dict, ['https://spreadsheets.google.com/feeds'])

#Now build the API object
discoveryUrl = "https://sheets.googleapis.com/$discovery/rest?version=v4"
gsheet = build('sheets', 'v4', discoveryServiceUrl=discoveryUrl, credentials=creds)

result = gsheet.spreadsheets().values().get(spreadsheetId="<A SHEET ID>", range="Sheet1!A1:ZZ").execute()    

This produces "results", which is a dictionary with 2 keys, "range" and "values", and "values" is a list of lists of the values of the spreadsheet. 这将产生“结果”,它是具有2个键的字典,“范围”和“值”,“值”是电子表格的值列表的列表。 These lists do not contain formatting data - just the values in the cells. 这些列表不包含格式数据-仅包含单元格中的值。

Can someone show me, in Python, how I can get cell value, background color, alignment, and other cell formatting information from spreadsheets().values() or from the spreadsheet? 有人可以用Python向我展示如何从电子表格().values()或电子表格中获取单元格值,背景色,对齐方式和其他单元格格式信息吗?

The spreadsheets.values.get endpoint only returns the values. spreadsheets.values.get端点仅返回值。 If you want a more complete picture of the spreadsheet (formatting, etc) then you need to use the spreadsheets.get endpoint: 如果您想要电子表格的更完整图片(格式等),则需要使用spreadsheets.get .get端点:

https://developers.google.com/sheets/reference/rest/v4/spreadsheets/get https://developers.google.com/sheets/reference/rest/v4/spreadsheets/get

Make sure to pass either includeGridData=true or pass a value for the fields that includes sheets.data so that the cell data is returned. 确保传递includeGridData=true或传递包含sheets.datafields的值,以便返回单元格数据。 Pass a value in the range parameter to limit the results to only a specific range. range参数中传递一个值以将结果限制为仅特定范围。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python Google表格API v4:如何批量获取和批量更新Google表格? - Python Google Sheets API v4: How to do Batch Get and Batch Update Google sheets? 使用Google API V4按值查找单元格(Google表格) - Find cell by value with google api v4 (google sheets) 从google Sheet api V4中的单元格获取超链接 - Get hyperlink from a cell in google Sheet api V4 使用Python在Google Sheets API v4中编写 - Writing in Google Sheets API v4 using Python 在 Python 中获取 google 电子表格 api v4 中的工作表和最新工作表列表 - Get list of sheets and latest sheet in google spreadsheet api v4 in Python 如何从Google Analytics Reporting API v4 Python获取前50个会话 - How to get Top 50 Sessions from Google Analytics Reporting API v4 Python 如何在没有 Google API 的情况下从 Colab(python)中的 Google 表格文档获取单元格的颜色? - How do I get a cell's color from a Google Sheets doc in Colab (python) without Google API? 使用 Python (API v4) 从 Google 表格中读取单元格格式 - Read cell format from Google sheet using Python (API v4) 如何通过python在单元格中使用Google sheet API V4插入注释 - How to use Google sheet API V4 insert note in cell by python 谷歌表格 API v4 方法:电子表格.values.append 只更新一个单元格 - Google Sheets API v4 Method: spreadsheets.values.append only updates one cell
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM