简体   繁体   English

使用 gspread 复制/粘贴格式

[英]Copy / Paste format using gspread

I am trying to copy / paste format from once column to another using gspread .我正在尝试使用gspread将格式从一个列复制/粘贴到另一个列。 My sheet looks like this:我的工作表看起来像这样:

在此处输入图像描述

My result should look like this:我的结果应该是这样的:

在此处输入图像描述

I tried:我试过了:

But for some reason this does not copy the format and I am not sure where is my mistake.但出于某种原因,这不会复制格式,我不确定我的错误在哪里。

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)

your issue is located in the body of the request.您的问题位于请求的正文中。

You are supposed to provide the sheet ID of the destination sheet where to copy the format but you provide the Worksheet python object instead.您应该提供要复制格式的目标工作表的工作表 ID,但您提供的是Worksheet python object。

Here:这里:

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

Or it should be:或者它应该是:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM