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

Lou*_*NNE 15 python google-spreadsheet-api

我正在寻找一种方法,只使用带有python的Google表格API来删除单元格范围选择的格式,而不是其内容.

目前,我唯一的解决方案是应用与普通格式相同的逻辑并将样式设置为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()
Run Code Online (Sandbox Code Playgroud)

如果我想删除它,我将'style'字段替换为'NONE',如下所示:

    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()
Run Code Online (Sandbox Code Playgroud)

但这意味着我需要定义一个函数来擦除我定义的每种格式的格式,这不是很实用......第一步是找到一种方法来擦除整张纸上的格式,也许之后是能够在我的工作表中的特定范围内执行此操作.

小智 9

有同样的问题并弄清楚了。另一个答案很接近,但对于其他偶然发现这一点的人来说。在api 示例中,它们表明您可以使用updateCells来清除采用gridRange参数的格式。gridRange 文档说

缺少索引表示该侧的范围是无界的。

因此,请保留影响整个工作表的所有索引。

要清除整个工作表:

body = {
    "requests": [
        {
            "updateCells": {
                "range": {
                    "sheetId": sheetId
                },
                "fields": "userEnteredFormat"
            }
        }
    ]
}
spreadsheet.batch_update(body)
Run Code Online (Sandbox Code Playgroud)

要清除范围:

body = {
    "requests": [
        {
            "updateCells": {
                "range": {
                    "sheetId": sheetId,
                    "startRowIndex": 1,
                    "endRowIndex": raws,
                    "startColumnIndex": first_col,
                    "endColumnIndex": last_col
                },
                "fields": "userEnteredFormat"
            }
        }
    ]
}
spreadsheet.batch_update(body)
Run Code Online (Sandbox Code Playgroud)


sou*_*ipe 5

该文档似乎指出,如果您在请求中设置fields为,它将删除所有格式。我还没有测试过这个,但是你去吧:"userEnteredValue"updateCells

request_dict = {
    "requests": [{
        "updateCells": {
              "range": {
                  "sheetId": sheetId,
                  "startRowIndex": 1,
                  "endRowIndex": raws,
                  "startColumnIndex": first_col,
                  "endColumnIndex": last_col
             },
             "fields": "userEnteredValue"
         }
     }]
}
Run Code Online (Sandbox Code Playgroud)