0

I am trying to copy / paste format from once column to another using gspread. My sheet looks like this:

enter image description here

My result should look like this:

enter image description here

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)
Jonas Palačionis
  • 4,591
  • 4
  • 22
  • 55
  • Does this answer your question? [Python - GSPREAD - Copy text and format from one google sheet to another one](https://stackoverflow.com/questions/67042868/python-gspread-copy-text-and-format-from-one-google-sheet-to-another-one) – bwdm Apr 28 '22 at 13:44
  • I've read this one, tried to apply but this refers to normal pasting, I tried adjusting to format only but it did not work. – Jonas Palačionis Apr 28 '22 at 13:46
  • First, I deeply apologize that my answer was not useful for your situation. In order to correctly understand your situation, can you provide the sample Spreadsheet for replicating your current issue? Because, when I tested `"pasteType": "PASTE_FORMAT"`, the background colors and the number format are copied. So, I thought that I would like to confirm your current situation. – Tanaike Apr 29 '22 at 00:24

1 Answers1

0

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,
...
Lavigne958
  • 442
  • 5
  • 12