我正在尝试为当前的电子表格设置数据验证规则。有一点可以帮助我能够从我已经设置的数据验证规则中查看JSON中的规则(在电子表格UI中或在API调用中)。
实施例
request = {
"requests": [
{
"setDataValidation": {
"range": {
"sheetId": SHEET_ID,
"startRowIndex": 1,
"startColumnIndex": 0,
"endColumnIndex":1
},
"rule": {
"condition": {
"type": "BOOLEAN"},
"inputMessage": "Value MUST BE BOOLEAN",
"strict": "True"
}
}
}
]
}
service.spreadsheets().batchUpdate(spreadsheetId=SPREADSHEET_ID body=request).execute()
但是,我使用什么API调用来查看这些单元格范围内的数据验证?如果我在电子表格中设置数据验证规则并且我想看看谷歌如何解释它们,这非常有用。我在通过API设置复杂的Datavalidations时遇到了很多麻烦。
谢谢
答案 0 :(得分:1)
我想我找到了答案。 IncludeGridData=True
spreadsheet().get
from pprint import pprint
response = service.spreadsheets().get(
spreadsheetId=SPREADSHEETID, fields='*',
ranges='InputWorking!A2:A',includeGridData=True).execute()
你得到一个怪物数据结构。因此,要查看您所在范围内的第一批数据。
pprint(response['sheets'][0]['data'][0]['rowData'][0]['values'][0]['dataValidation'])
{'condition': {'type': 'BOOLEAN'},
'inputMessage': 'Value MUST BE BOOLEAN',
'strict': True}
答案 1 :(得分:1)
要仅获取给定电子表格的“数据验证”组件,只需在对spreadsheets.get
的调用中请求适当的字段:
service = get_authed_sheets_service_somehow()
params = {
spreadsheetId: 'your ssid',
#range: 'some range',
fields: 'sheets(data/rowData/values/dataValidation,properties(sheetId,title))' }
request = service.spreadsheets().get(**params)
response = request.execute()
# Example print code (not tested :p )
for sheet in response['sheets']:
for range in sheet['data']:
for r, row in enumerate(range['rowData']):
for c, col in enumerate(row['values']):
if 'dataValidation' in col:
# print "Sheet1!R1C1" & associated data validation object.
# Assumes whole grid was requested (add appropriate indices if not).
print(f'\'{sheet["properties"]["title"]}\'!R{r}C{c}', col['dataValidation'])
通过指定字段,不需要includeGridData
来获取每个单元格中您请求的范围内的数据。通过不提供范围,我们将整个文件作为目标。对于电子表格中的每个工作表,此特定字段规范都要求rowData.values.dataValidation
对象以及sheetId
对象的title
和properties
。
您可以使用Google APIs Explorer交互式确定适当的有效“字段”规范,并另外检查响应: https://developers.google.com/apis-explorer/#p/sheets/v4/sheets.spreadsheets.get
有关“字段”说明符如何工作的更多信息,请阅读文档:https://developers.google.com/sheets/api/guides/concepts#partial_responses
(对于某些写请求,字段规范不是可选的,因此确定如何有效使用它们符合您的最大利益。)