Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Sheets Api: Copy google spreadsheet

Trying to copy a whole spreadsheet but I guess there is no api to do so.

Basically, I am trying to do the following:

  • Have a spreadsheet which I would like to duplicate with minor changes.
  • Create a new spreadsheet, copy all sheets from a template into the new spreadsheet one by one (Spreadsheet copy would have been much more efficient)

Creating new spreadsheets works alright however, copying sheets from spreadsheet doesn't work.

Tried 2 ways:

Angular:

$http.post("https://sheets.googleapis.com/v4/spreadsheets/"+fromSpreadsheetId+"/sheets/"+fromSheetId,
                            {headers: {
                        'Authorization': 'Bearer ' + this.oauthToken
                     }},

Gives following error:

Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin'

Google Sheets Api call:

gapi.client.sheets.spreadsheets.sheets.copyTo({spreadsheetId: fromSpreadsheetId , sheetId: fromSheetId},{destinationSpreadsheetId: destinationSpreadsheetId});

Request goes through without any error. However, the newly created spreadsheet doesn't have the sheet copied.

like image 323
Shyamal Parikh Avatar asked Dec 20 '16 17:12

Shyamal Parikh


1 Answers

You probably want to ask a separate question specifically for the CORS issue, since that a separate problem.

With regards to "copy a spreadsheet", you have two options:

1) Use the Drive API's files.copy method. The fileId in the Drive API is equivalent to the spreadsheetId in the Sheets API.

2) Don't use a "template" spreadsheet that you copy. Instead, use the Sheet API's spreadsheets.create method. You can use spreadsheets.get to retrieve your "template" JSON, and can tweak that as necessary before creating your new spreadsheet.

like image 96
Sam Berlin Avatar answered Sep 22 '22 04:09

Sam Berlin