Integrating Google Sheets with iOS app using Drive API

Hey everyone! I’m working on an iPhone app and want to use a Google spreadsheet as my database. I’ve been messing around with the Drive API, but I’m stuck on how to work directly with spreadsheets.

I managed to upload a CSV file and convert it, but I really want to use the application/vnd.google-apps.spreadsheet MIME type. Can anyone help me figure out how to create a spreadsheet with three cells using the Drive API?

Here’s what I’ve got so far:

func createSpreadsheetWithThreeCells() {
    let cellData = "A1,B1,C1"
    
    // How do I turn cellData into a proper spreadsheet format?
    let spreadsheetContent: Data?
    
    let uploadParams = GTLRUploadParameters(data: spreadsheetContent, mimeType: "application/vnd.google-apps.spreadsheet")
    
    let query = GTLRDriveQuery_FilesCreate.query(withObject: driveFile, uploadParameters: uploadParams)
    
    driveService.executeQuery(query) { (ticket, file, error) in
        // Handle response
    }
}

Also, is the Drive API the best choice for this? I want to update specific cells later on. I’ve heard about other APIs, but Drive seems newest. Any advice?

Thanks for your help!

hey mate, i’ve actually done smth similar recently. the sheets API is way better for what ur trying to do. it’s made for spreadsheets and u can create n update cells easily.

here’s a quick tip: use the spreadsheets.create method to make ur sheet, then spreadsheets.values.update for changing stuff later. way simpler than messing with drive API tbh

As someone who’s integrated Google Sheets with iOS apps before, I can tell you that the Drive API isn’t ideal for what you’re trying to do. I’d strongly recommend switching to the Google Sheets API instead. It’s designed specifically for spreadsheet operations and will make your life much easier.

With the Sheets API, you can create a new spreadsheet and populate cells in one go. You don’t need to worry about converting CSV data or dealing with MIME types. Plus, updating specific cells later is a breeze.

Here’s a rough idea of how you might structure your request:

let spreadsheet = GTLRSheets_Spreadsheet()
spreadsheet.properties = GTLRSheets_SpreadsheetProperties()
spreadsheet.properties.title = "My Spreadsheet"

let request = GTLRSheetsQuery_SpreadsheetsCreate.query(withObject: spreadsheet)
sheetsService.executeQuery(request) { ... }

This approach will save you a lot of headaches down the line. Trust me, I’ve been there!

I’ve worked with Google Sheets integration in iOS apps before, and while the Drive API can work, it’s not the most efficient for your use case. For direct interaction with spreadsheets, I’d recommend using the Google Sheets API instead. It offers more granular control over cells and data manipulation.

To create a spreadsheet with three cells, you’d use the Sheets API’s spreadsheets.create method. Then, to update specific cells later, you can use spreadsheets.values.update. This approach is more straightforward and gives you precise control over your data.

If you’re set on using the Drive API, you’d need to create the spreadsheet content in the correct format (XLSX or ODS) before uploading. However, this method is more complex and less flexible for future updates.

For ongoing cell updates and real-time data, consider exploring the Google Sheets API with OAuth2 authentication. It’ll provide a smoother experience for your specific requirements.