代码之家  ›  专栏  ›  技术社区  ›  Jonas Palačionis

使用gspread复制/粘贴格式

  •  0
  • Jonas Palačionis  · 技术社区  · 2 年前

    我正在尝试使用 gspread 。我的工作表如下所示:

    enter image description here

    我的结果应该是这样的:

    enter image description here

    我试过:

    但由于某些原因,这并没有复制格式,我不确定我的错误在哪里。

    GSHEETS_SCOPES = [
        "https://www.googleapis.com/auth/spreadsheets",
        "https://www.googleapis.com/auth/drive.file",
        "https://www.googleapis.com/auth/drive"
    ]
    
    
    
    CLIENT_SECRET_GOOGLE_SHEETS = r"folder/file.json"
    creds = ServiceAccountCredentials.from_json_keyfile_name(CLIENT_SECRET_GOOGLE_SHEETS, GSHEETS_SCOPES)
    client = gspread.authorize(creds)
    sheet = client.open("My Sheet")
    
    body = {
        "requests": [
            {
                "copyPaste": {
                    "source": {
                        "sheetId": sheet.id,
                        "startRowIndex": 1,
                        "endRowIndex": 30,
                        "startColumnIndex": 0,
                        "endColumnIndex": 1
                    },
                    "destination": {
                        "sheetId": sheet,
                        "startRowIndex": 1,
                        "endRowIndex": 30,
                        "startColumnIndex": 1,
                        "endColumnIndex": 2
                    },
                    "pasteType": "PASTE_FORMAT"
                }
            },
            
            }
        ]
    }
    res = sheet.batch_update(body)
    
    0 回复  |  直到 2 年前
        1
  •  0
  •   Lavigne958    2 年前

    您的问题位于请求正文中。

    您应该提供要复制格式的目标工作表的工作表ID,但您提供 Worksheet 而不是python对象。

    在这里:

    ...
    "destination": {
      "sheetId": sheet,
    ...
    

    或者应该是:

    ...
    "destination": {
      "sheetId": sheet.id,
    ...