Python 使用google电子表格API仅删除单元格范围选择上的格式设置
我正在寻找一种方法,使用googlesheetapi和python,只删除单元格范围选择上的格式,而不删除其内容 目前,我唯一的解决方案是应用与普通格式相同的逻辑,并将样式设置为“无”。例如,当我将边框格式设置为特定范围时,我使用:Python 使用google电子表格API仅删除单元格范围选择上的格式设置,python,google-sheets-api,Python,Google Sheets Api,我正在寻找一种方法,使用googlesheetapi和python,只删除单元格范围选择上的格式,而不删除其内容 目前,我唯一的解决方案是应用与普通格式相同的逻辑,并将样式设置为“无”。例如,当我将边框格式设置为特定范围时,我使用: request_dict = {'requests': [{ "updateBorders": { "range": { "s
request_dict = {'requests': [{
"updateBorders": {
"range": {
"sheetId": sheetId,
"startRowIndex": 1,
"endRowIndex": raws,
"startColumnIndex": first_col,
"endColumnIndex": last_col},
"top": {
"style": "SOLID_MEDIUM",
"width": 1,
"color": {"blue": 0}},
"bottom": {
"style": "SOLID_MEDIUM",
"width": 1,
"color": {"blue": 0}},
"left": {
"style": "SOLID_MEDIUM",
"width": 1,
"color": {"blue": 0}},
"right": {
"style": "SOLID_MEDIUM",
"width": 1,
"color": {"blue": 0}},
"innerHorizontal": {
"style": "SOLID_MEDIUM",
"width": 1,
"color": {"blue": 0}},
"innerVertical": {
"style": "SOLID_MEDIUM",
"width": 1,
"color": {"blue": 0}}}}]}
body = {'requests': request_dict['requests']}
service.spreadsheets().batchUpdate(spreadsheetId=spreadsheetId,
body=body).execute()
如果我想删除它,我将“样式”字段替换为“无”,如下所示:
request_dict = {'requests': [{
"updateBorders": {
"range": {
"sheetId": sheetId,
"startRowIndex": 1,
"endRowIndex": raws,
"startColumnIndex": first_col,
"endColumnIndex": last_col},
"top": {
"style": "NONE",
"width": 1,
"color": {"blue": 0}},
"bottom": {
"style": "NONE",
"width": 1,
"color": {"blue": 0}},
"left": {
"style": "NONE",
"width": 1,
"color": {"blue": 0}},
"right": {
"style": "NONE",
"width": 1,
"color": {"blue": 0}},
"innerHorizontal": {
"style": "NONE",
"width": 1,
"color": {"blue": 0}},
"innerVertical": {
"style": "NONE",
"width": 1,
"color": {"blue": 0}}}}]}
body = {'requests': request_dict['requests']}
service.spreadsheets().batchUpdate(spreadsheetId=spreadsheetId,
body=body).execute()
但这意味着我需要定义一个函数来擦除我定义的每种格式的格式,这不是很实用。。。
第一步是找到一种方法来删除整个工作表上的格式,也许之后可以在我的工作表中的特定范围内执行此操作。似乎说明,如果在更新cells
请求中将字段设置为“userEnteredValue”
,则它将删除所有格式。我还没有测试过这个,但给你:
request_dict = {
"requests": [{
"updateCells": {
"range": {
"sheetId": sheetId,
"startRowIndex": 1,
"endRowIndex": raws,
"startColumnIndex": first_col,
"endColumnIndex": last_col
},
"fields": "userEnteredValue"
}
}]
}
我也有同样的问题,并且解决了。另一个答案很接近,但对于任何偶然发现这一点的人来说。在中,它们表示可以用于清除包含参数的格式。gridRange文档说
缺少索引表示该侧的范围是无限的
因此,请删除所有索引以影响整个工作表
要清除整个工作表,请执行以下操作:
body = {
"requests": [
{
"updateCells": {
"range": {
"sheetId": sheetId
},
"fields": "userEnteredFormat"
}
}
]
}
spreadsheet.batch_update(body)
要清除范围,请执行以下操作:
body = {
"requests": [
{
"updateCells": {
"range": {
"sheetId": sheetId,
"startRowIndex": 1,
"endRowIndex": raws,
"startColumnIndex": first_col,
"endColumnIndex": last_col
},
"fields": "userEnteredFormat"
}
}
]
}
spreadsheet.batch_update(body)
我知道这很旧,可能指的是v3,但我想我会补充一些对我有用的东西:
{
repeatCell: {
range: {
sheetId: sheetId,
startRowIndex: 0,
endRowIndex: <rows>,
startColumnIndex: 0,
endColumnIndex: <columns>
},
fields: "userEnteredFormat"
}
{
重复单元:{
范围:{
sheetId:sheetId,
startRowIndex:0,
endRowIndex:,
startColumnIndex:0,
endColumnIndex:
},
字段:“userEnteredFormat”
}
我不知道…但可能你可以从一个已知的、未格式化的单元格复制格式。我有一个编辑,似乎表明userEnteredValue
应该是userEnteredFormat
。。这很有意义,但我还没有测试过。如果有人测试它并在这里发表评论,我会更新我的答案。我使用的是字段:“我们”erEnteredFormat'
,但使用repeatCell
而不是updateCells
命令