[英]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.data
的fields
的值,以便返回单元格数据。 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.