1
votes

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

1 Answers

1
votes

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.