简体   繁体   中英

Copy / Paste format using gspread

I am trying to copy / paste format from once column to another using 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.

Here:

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

Or it should be:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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