【问题标题】:How to insert row and change cell styles using batch_update in gspread?如何在 gspread 中使用 batch_update 插入行和更改单元格样式?
【发布时间】:2020-01-20 17:58:09
【问题描述】:

我正在使用 gspreadgspread_formatting 在 Google 中更新我的工作表。我的一些代码库已被重新设计为使用batch_update,因为我在另一个答案中找到了一些代码示例,可以用作参考。但是,我似乎无法转换其他两个操作。这是我的代码:

import gspread

from gspread_formatting import *
from oauth2client.service_account import ServiceAccountCredentials

def operate():
    scope = [
        'https://spreadsheets.google.com/feeds',
        'https://www.googleapis.com/auth/drive'
    ]
    credentials = ServiceAccountCredentials.from_json_keyfile_name(
        'creds.json',
        scope
    )
    gc = gspread.authorize(credentials)

    sh = gc.open('My spreadsheet')
    ws = sh.sheet1
    sheet_id = ws._properties['sheetId']

    # Setting the column sizes
    body = {
        'requests': [
            {
                'updateDimensionProperties': {
                    'range': {
                        'sheetId': sheet_id,
                        'dimension': 'COLUMNS',
                        'startIndex': 0,
                        'endIndex': 10
                    },
                    'properties': {
                        'pixelSize': 150
                    },
                    'fields': 'pixelSize'
                }
            },
            {
                'updateDimensionProperties': {
                    'range': {
                        'sheetId': sheet_id,
                        'dimension': 'COLUMNS',
                        'startIndex': 4,
                        'endIndex': 6
                    },
                    'properties': {
                        'pixelSize': 250
                    },
                    'fields': 'pixelSize'
                }
            }
        ]
    }
    res = sh.batch_update(body)

    # Request 1
    ws.insert_row(['One', 'Two', 'Three'], 1)

    # Request 2
    format_cell_range(
        ws, 'A1:Z7',
        cellFormat(
            wrapStrategy='WRAP',
            verticalAlignment='MIDDLE',
            backgroundColor=color(0.886, 0.945, 0.988),
            textFormat=textFormat(
                foregroundColor=color(0, 0.129, 0.443),
                fontFamily='Roboto',
                bold=True
            )
        )
    )

所以,我想做的是以某种方式将请求 1 和请求 2 添加到 bulk_update 方法中。是否可以使用bulk_update 插入一行并更改格式?如果是,我该怎么做?感谢您的帮助。

【问题讨论】:

    标签: python google-sheets gspread


    【解决方案1】:
    • 您想在第一行插入新行。
    • 您想将['One', 'Two', 'Three'] 的值放入插入的行中。
    • 您想使用 gspread 的batch_update() 的方法将以下单元格格式设置为“A1:Z7”的范围。

      wrapStrategy='WRAP',
      verticalAlignment='MIDDLE',
      backgroundColor=gsf.color(0.886, 0.945, 0.988),
      textFormat=gsf.textFormat(
          foregroundColor=gsf.color(0, 0.129, 0.443),
          fontFamily='Roboto',
          bold=True
      )
      
    • 您想使用 gspread 和 python 来实现这一点。

      • 您的目标是将以下脚本转换为batch_update()

        # Request 1
        ws.insert_row(['One', 'Two', 'Three'], 1)
        
        # Request 2
        format_cell_range(
            ws, 'A1:Z7',
            cellFormat(
                wrapStrategy='WRAP',
                verticalAlignment='MIDDLE',
                backgroundColor=color(0.886, 0.945, 0.988),
                textFormat=textFormat(
                    foregroundColor=color(0, 0.129, 0.443),
                    fontFamily='Roboto',
                    bold=True
                )
            )
        )
        
    • 您已经能够使用 Sheets API 获取和放置电子表格的值。

    如果我的理解是正确的,那么这个答案呢?请认为这只是几个可能的答案之一。

    流程:

    本例中batch_update()的请求体流程如下。

    1. insertDimension 在第一行插入新行。
    2. ['One', 'Two', 'Three']的值放到updateCells插入的行中。
    3. 将单元格格式设置为repeatCell的“A1:Z7”范围。

    修改脚本:

    spreadsheetId = "###"  # Please set the Spreadsheet ID.
    sheetName = "###"  # Please set the sheet name.
    
    sh = gc.open_by_key(spreadsheetId)
    ws = sh.worksheet(sheetName)
    sheetId = ws._properties['sheetId']
    requests = {
        "requests": [
            {
                "insertDimension": {
                    "range": {
                        "sheetId": sheetId,
                        "startIndex": 0,
                        "dimension": "ROWS",
                        "endIndex": 1
                    }
                }
            },
            {
                "updateCells": {
                    "range": {
                        "sheetId": sheetId,
                        "startRowIndex": 0,
                        "endRowIndex": 1
                    },
                    "rows": [
                        {
                            "values": [
                                {
                                    "userEnteredValue": {
                                        "stringValue": "One"
                                    }
                                },
                                {
                                    "userEnteredValue": {
                                        "stringValue": "Two"
                                    }
                                },
                                {
                                    "userEnteredValue": {
                                        "stringValue": "Three"
                                    }
                                }
                            ]
                        }
                    ],
                    "fields": "userEnteredValue"
                }
            },
            {
                "repeatCell": {
                    "range": {
                        "sheetId": sheetId,
                        "startRowIndex": 0,
                        "endRowIndex": 7,
                        "startColumnIndex": 0,
                        "endColumnIndex": 26
                    },
                    "cell": {
                        "userEnteredFormat": {
                            "wrapStrategy": "WRAP",
                            "verticalAlignment": "MIDDLE",
                            "backgroundColor": {
                                "red": 0.886,
                                "green": 0.945,
                                "blue": 0.988
                            },
                            "textFormat": {
                                "foregroundColor": {
                                    "red": 0,
                                    "green": 0.129,
                                    "blue": 0.443
                                },
                                "fontFamily": "Roboto",
                                "bold": True
                            }
                        }
                    },
                    "fields": "userEnteredFormat"
                }
            }
        ]
    }
    res = sh.batch_update(requests)
    

    注意:

    • 我知道bulk_updatebatch_update

    参考资料:

    如果我误解了您的问题并且这不是您想要的结果,我深表歉意。

    【讨论】:

    • 哇,这就是完美的答案。非常感谢!
    • @darkhorse 感谢您的回复。我很高兴你的问题得到了解决。也谢谢你。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-16
    • 2021-01-17
    • 1970-01-01
    • 2010-10-15
    • 1970-01-01
    相关资源
    最近更新 更多