1

I'm trying to duplicate a sheet using the Google Sheets API. I've been successful reading and writing variables, but I'm stuck on the DuplicateSheetRequest with batchUpdate() code. My code looks like this:

requests = []
requests.append({
    "DuplicateSheetRequest": {
        "sourceSheetId": sheet_id_source,
        "insertSheetIndex": target_index,
        "newSheetId": 123456,
        "newSheetName": destination_sheet_name
    }
})
body = {
    'requests': requests
}
response = service.spreadsheets().batchUpdate(spreadsheetId=spreadsheetId,
                                           body=body).execute()

When I execute the code, I get an error:

https://sheets.googleapis.com/v4/spreadsheets/[sheet ID redacted]:batchUpdate?alt=json returned "Invalid JSON payload received. Unknown name "duplicate_sheet_request" at 'requests[0]': Cannot find field.">

What's really odd is that the error says "duplicate_sheet_request" whereas my code says DuplicateSheetRequest. (I also tested something I knew was wrong, e.g. "DuplicateSheetRequestsss", which became "duplicate_sheet_requestsss"). I don't know why the variable name is chasing, though I'm inclined to believe this is a red herring.

The closest thing I could find is this Google Groups issue that suggests that perhaps the method I'm trying to use isn't allowed with a service account, but I may be misreading that thread.

1 Answer 1

1

The problem was that the batch update method I was looking for should have been duplicateSheet, not DuplicateSheetRequest:

requests.append({
    "duplicateSheet": {
        "sourceSheetId": sheet_id_source,
        "insertSheetIndex": target_index,
        #"newSheetId": 123456,
        "newSheetName": destination_sheet_name
    }
})

Quack quack.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.