Swift - Read Google Sheets

Viewed 1005

I'm working on an app that uses Google Sheets as a database, but I can't figure out how to get Swift to read from google sheets. I've looked through the API website and a few questions on here but I need some help just getting started. So far I have; private let scopes = [kGTLRAuthScopeSheetsSpreadsheets] private let service = GTLRSheetsService()

    var range = "Form Responses 1!A1:D3"
    let spreadsheetId = "p;;;;;;p;;;;;;;;1LqXa6v75JE8RQQDOI4Z_g8mUT8x0DhsEDwRIaxDN-DU" // Portfolio
    let query = GTLRSheetsQuery_SpreadsheetsValuesGet.query(withSpreadsheetId: spreadsheetId, range:range)
    service.executeQuery(query, delegate: self, didFinish: #selector(displayResultWithTicket(ticket:finishedWithObject:error:)))

Which I got from another question on here... But 'displayResultWithTicket' is an unresolved identifier. Currently it gets to the last line, and debugging I'm not even sure what to do with the 'query' value. I'm really not even sure if it's reading the correct spreadsheet. How can I tell?

Long story short, I'm lost.

2 Answers

I recently made an app that uses Google Sheets. But I used the CSV file for the database. This might help. If you want, I can share the details.

First you need to export the Google Sheets file in CSV format. I used Realm to read CSV. Using Realm Studio, you need to create the columns as in the CSV file. Then you can import the CSV file into Realm Studio. Your database is ready.

In didFinishLaunchingWithOptions to find the file created after installing Realm on your project

print(Realm.Configuration.defaultConfiguration.fileURL!.path)

Now we will use the database with Swift. For this, we must first add the Realm file that we prepared to the XCode project directory. Then we use it in ViewController this way to get data.

var realm : Realm!

For get the data in default.realm file

    let realmPath = Bundle.main.url(forResource: "default", withExtension: "realm")!
    let realmConfiguration = Realm.Configuration(fileURL: realmPath, readOnly: true)

    do{
        realm = try Realm(configuration: realmConfiguration)
    }
    catch {
        print("error \(error.localizedDescription)")
    }

You can export the result to an array. I hope it helps.

Related