使用google电子表格API只删除单元格区域选择的格式

2024-10-01 05:07:28 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在寻找一种方法,使用googlesheetapi和python只删除单元格区域选择上的格式,而不是它们的内容。在

目前,我唯一的解决方案是应用与普通格式相同的逻辑,并将样式设置为NONE。例如,当我将边框格式设置为特定范围时,我使用:

    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()

如果我想删除它,我将“样式”字段替换为“无”,如下所示:

^{pr2}$

但这意味着我需要定义一个函数来删除我定义的每种格式的格式,这不是很实用。。。 第一步是找到一种方法来删除整个工作表上的格式,也许之后就可以在我的工作表的特定范围内这样做。在


Tags: 方法stylerequest格式body样式colblue
1条回答
网友
1楼 · 发布于 2024-10-01 05:07:28

This documentation似乎声明,如果在updateCells请求中将fields设置为"userEnteredValue",则它将删除所有格式。我还没有测试过,但是你可以:

request_dict = {
    "requests": [{
        "updateCells": {
              "range": {
                  "sheetId": sheetId,
                  "startRowIndex": 1,
                  "endRowIndex": raws,
                  "startColumnIndex": first_col,
                  "endColumnIndex": last_col
             },
             "fields": "userEnteredValue"
         }
     }]
}

相关问题 更多 >