'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)
Solution 1:[1]
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,
...
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Lavigne958 |


