Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I make and connect a spreadsheet to my code using the Google Sheets API?

I'm writing an app that needs to record data in a Google Spreadsheet with Swift.

I'm just really uncertain how to actually get started! I set up the Google sheets API in the Google API console and in my cocoapods following the Quickstart provided by google. Google's libraries GoogleAPIClientForREST and GTMOAuth2 are in my app and are ready to be used.

I basically just need to read and write to an existing spreadsheet but I do not know how to create the spreadsheet and refer to it so I can read and write to it as needed.

Thanks!

like image 589
Amber Rebecca Howe Avatar asked Mar 22 '17 00:03

Amber Rebecca Howe


1 Answers

Whenever I want to read or write to a Google spreadsheet using the Sheets API, I refer to Reading & Writing Cell Values.

Basic Reading

The following spreadsheets.values.get request reads the values stored in the range Sheet1!A1:D5 and returns them in the response. Empty trailing rows and columns are omitted.

GET https://sheets.googleapis.com/v4/spreadsheets/spreadsheetId/values/Sheet1!A1:D5

Basic Writing

Starting with a new, blank spreadsheet, the following spreadsheets.values.update request will write the values to the range Sheet1!A1:D5. The ValueInputOption query parameter is required and determines if the values written will be parsed (for example, whether or not a string is converted into a date).

PUT https://sheets.googleapis.com/v4/spreadsheets/spreadsheetId/values/Sheet1!A1:D5?valueInputOption=USER_ENTERED

Here's the iOS Swift Quickstart for your code reference.

like image 99
noogui Avatar answered Sep 28 '22 10:09

noogui