0

I have been trying to implement a basic iOS app that will write to a Google spreadsheet when an event occurs. I started with Google dev docs here: https://developers.google.com/sheets/quickstart/ios and was able to get the things working till adding data and updating it. But, I am not able to find a way to append data(insert a new row).

I have used [self.service fetchObjectByUpdatingObject:... method to accomplish above but [self.service fetchObjectByInsertingObject:... method refuses to work on the same lines.

Reference documentation talks about an append method for this purpose but I am not finding a way to call it in iOS. I have tried almost all the Fetch Methods provided to us in the GTLService class.

Update: I am looking for the iOS counterpart for executing the following as done in Java: service.spreadsheets().values().append

Any guidance on how to append data to a spreadsheet is much appreciated.

PS: I have also tried the standard REST approach but that always returned Unauthenticated error. So, I ditched that and took the above approach which worked for add, update. Let me know if someone needs me to post the code for add, update implementation.

Frost_Mourne
  • 149
  • 10
  • Try checking if you have [Authorize Requests](https://developers.google.com/sheets/guides/authorizing) properly. For private data : [Oauth 2.0](https://developers.google.com/sheets/guides/authorizing#OAuth2Authorizing), for public data : an identifier, such as an [API key](https://developers.google.com/sheets/guides/authorizing#APIKey) is needed. Every request your application sends to the Google Sheets API needs to identify your application to Google. – Mr.Rebot Sep 15 '16 at 15:21
  • You might also want to check the answer on this [SO question](http://stackoverflow.com/a/37325295/5995040) about `Unauthorized` request for Google Sheet API v4. Hope this helps! – Mr.Rebot Sep 15 '16 at 15:21
  • Well, I have already given it a go(and will do again), if that is the only way to do it. In the first part of my question, I got it working for Add and Update without using REST services(by using GLTService class). Is there no way to achieve append using the same approach? Thanks – Frost_Mourne Sep 15 '16 at 21:42
  • I mean is there no way to do the append using the GoogleAPIClient library which I used for doing Add and Update? – Frost_Mourne Sep 16 '16 at 01:51

1 Answers1

7

The Sheets API iOS Quickstart has been updated to use a newer version of the Objective-C client library that includes generated classes for the Google Sheets API. Using that new library you can make an append call using the following code:

    let spreadsheetId = "..."
    let range = "Sheet1"
    let valueRange = GTLRSheets_ValueRange.init();
    valueRange.values = [
        ["Hello", "World"]
    ]
   let query = GTLRSheetsQuery_SpreadsheetsValuesAppend
            .query(withObject: valueRange, spreadsheetId:spreadsheetId, range:range)
        query.valueInputOption = "USER_ENTERED"
        service.executeQuery(query,
                             delegate: self,
                             didFinish: #selector(displayResultWithTicket(ticket:finishedWithObject:error:)))
Rizwan Mehboob
  • 1,333
  • 17
  • 19
Eric Koleda
  • 12,420
  • 1
  • 33
  • 51