如何在 google-api-python-client 中为 google 工作表使用 ValueRenderOption
How to use ValueRenderOption in google-api-python-client for google sheets
这是一个代码片段:
import ast
from oauth2client import file, client, tools
from oauth2client.service_account import ServiceAccountCredentials
from httplib2 import Http
from apiclient.discovery import build
from apiclient import discovery
SCOPE = 'https://www.googleapis.com/auth/spreadsheets'
CREDJSON = "some-cred-file-downloaded-from-dev-console.json"
def google_credentials(jsoncred=CREDJSON, scope=SCOPE):
return ServiceAccountCredentials.from_json_keyfile_name(jsoncred, scope)
def csv_arrays(creds, key):
(SHEETS, sheets) = sheets_fetch(creds, key)
wks = sheets
def create_filename(arg):
filename = key.prefix + "_" + arg.get("properties, {}).get("title","Sheet1")
filename = filename.replace(" ", "_")
print (filename)
return filename
return [(create_filename(ws), ast.literal_eval(repr(SHEETS.spreadsheets().values().get(spreadsheetId=key.key, range=(ws.get("properties", {}).get("title", "Sheet1"))).execute().get('values',[])))) for ws in wks] #.decode("utf-8")
def sheets_fetch(creds, key):
print('Now doing:', key)
SHEETS = build('sheets', 'v4', http=creds.authorize(Http()))
sheet_metadata = SHEETS.spreadsheets().get(spreadsheetId=key.key).execute()
sheets = sheet_metadata.get('sheets', '')
return (SHEETS, sheets)
我可以毫无问题地获取单元格的内容。我的问题是它们是格式化的(因为这是默认值)而我想要它们未格式化?
如何在 csv_array 函数的 return 语句的查询中设置选项?
https://developers.google.com/sheets/reference/rest/v4/ValueRenderOption
附带说明一下,有没有办法以其他方式获取单元格值,而不是将字符串 ast-ed 到列表?
要为 python 客户端设置 UNFORMATTED_VALUE 的 valueRenderOption,请使用:
SHEETS.spreadsheets().values().get(spreadsheetId=key.key, range=myRange, valueRenderOption='UNFORMATTED_VALUE').execute()
此外,当使用 UNFORMATTED_VALUE 时,响应将按表格中的原样键入(例如,数字为数字,布尔值为布尔值,字符串为字符串)。日期可以是字符串或数字,具体取决于 DateTimeRenderOption, see the Date/Time Serial Numbers Guide 以获取有关将日期作为数字处理的更多信息。
因为值已经正确键入,所以您不需要使用 ast 或 repr 进行任何额外的解析。
这是一个代码片段:
import ast
from oauth2client import file, client, tools
from oauth2client.service_account import ServiceAccountCredentials
from httplib2 import Http
from apiclient.discovery import build
from apiclient import discovery
SCOPE = 'https://www.googleapis.com/auth/spreadsheets'
CREDJSON = "some-cred-file-downloaded-from-dev-console.json"
def google_credentials(jsoncred=CREDJSON, scope=SCOPE):
return ServiceAccountCredentials.from_json_keyfile_name(jsoncred, scope)
def csv_arrays(creds, key):
(SHEETS, sheets) = sheets_fetch(creds, key)
wks = sheets
def create_filename(arg):
filename = key.prefix + "_" + arg.get("properties, {}).get("title","Sheet1")
filename = filename.replace(" ", "_")
print (filename)
return filename
return [(create_filename(ws), ast.literal_eval(repr(SHEETS.spreadsheets().values().get(spreadsheetId=key.key, range=(ws.get("properties", {}).get("title", "Sheet1"))).execute().get('values',[])))) for ws in wks] #.decode("utf-8")
def sheets_fetch(creds, key):
print('Now doing:', key)
SHEETS = build('sheets', 'v4', http=creds.authorize(Http()))
sheet_metadata = SHEETS.spreadsheets().get(spreadsheetId=key.key).execute()
sheets = sheet_metadata.get('sheets', '')
return (SHEETS, sheets)
我可以毫无问题地获取单元格的内容。我的问题是它们是格式化的(因为这是默认值)而我想要它们未格式化?
如何在 csv_array 函数的 return 语句的查询中设置选项?
https://developers.google.com/sheets/reference/rest/v4/ValueRenderOption
附带说明一下,有没有办法以其他方式获取单元格值,而不是将字符串 ast-ed 到列表?
要为 python 客户端设置 UNFORMATTED_VALUE 的 valueRenderOption,请使用:
SHEETS.spreadsheets().values().get(spreadsheetId=key.key, range=myRange, valueRenderOption='UNFORMATTED_VALUE').execute()
此外,当使用 UNFORMATTED_VALUE 时,响应将按表格中的原样键入(例如,数字为数字,布尔值为布尔值,字符串为字符串)。日期可以是字符串或数字,具体取决于 DateTimeRenderOption, see the Date/Time Serial Numbers Guide 以获取有关将日期作为数字处理的更多信息。
因为值已经正确键入,所以您不需要使用 ast 或 repr 进行任何额外的解析。