I’m building an iPhone app that needs to use a Google spreadsheet as its database. I’ve looked at the Drive API docs and tried modifying an example for text files to work with spreadsheets instead. But I’m stuck on how to directly create and edit Google Sheets files.
Here’s what I’m trying to do:
func createSpreadsheetWithThreeColumns() {
let sheetData = "Column1,Column2,Column3"
// How do I turn this into a proper Google Sheets file?
let sheetContent: Data = ?
let uploadParams = GTLRUploadParameters(data: sheetContent, mimeType: "application/vnd.google-apps.spreadsheet")
let query = GTLRDriveQuery_FilesCreate.query(withObject: driveFile, uploadParameters: uploadParams)
driveService.executeQuery(query) { _, _, error in
// Handle response
}
}
Is the Drive API the best choice here? I eventually want to update specific cells rather than whole files. Are there better APIs for working with Google Sheets in iOS apps? Any tips or examples would be super helpful!
I’ve actually integrated Google Sheets with an iOS app before, and I can tell you the Drive API isn’t the best choice for what you’re trying to do. You’ll want to use the Google Sheets API instead - it’s much more suited for spreadsheet-specific operations.
For your use case, I’d recommend looking into the GTLRSheetsService class. It provides methods for creating and modifying spreadsheets directly. Here’s a rough outline of how you might create a new sheet:
let service = GTLRSheetsService()
service.authorizer = // Your auth method here
let spreadsheet = GTLRSheets_Spreadsheet()
spreadsheet.properties = GTLRSheets_SpreadsheetProperties()
spreadsheet.properties.title = "My New Spreadsheet"
let query = GTLRSheetsQuery_SpreadsheetsCreate.query(withObject: spreadsheet)
service.executeQuery(query) { (ticket, result, error) in
// Handle response
}
This approach will give you much more control over spreadsheet-specific features. You’ll be able to add sheets, update cell values, and format cells more easily than with the Drive API. The Sheets API documentation has plenty of examples to get you started with more complex operations.
hey there! i’ve done somethin similar before. the sheets api is def the way to go for what ur tryin to do. it’ll make your life way easier when it comes to workin with spreadsheets.
check out the GTLRSheetsService class. it’s got all the good stuff for makin and editin sheets. way better than messin with the drive api for this kinda thing.
While the Drive API can work for basic file operations, it’s not ideal for manipulating spreadsheet data. I’d strongly recommend using the Google Sheets API instead. It provides much more granular control over spreadsheet content and structure.
To create a new spreadsheet with three columns, you could use something like this:
let service = GTLRSheetsService()
service.authorizer = yourAuthorizationMethod
let spreadsheet = GTLRSheets_Spreadsheet()
spreadsheet.properties = GTLRSheets_SpreadsheetProperties()
spreadsheet.properties.title = "My Spreadsheet"
let sheet = GTLRSheets_Sheet()
sheet.properties = GTLRSheets_SheetProperties()
sheet.properties.title = "Sheet1"
spreadsheet.sheets = [sheet]
let query = GTLRSheetsQuery_SpreadsheetsCreate.query(withObject: spreadsheet)
service.executeQuery(query) { (ticket, result, error) in
// Handle the response
}
This approach gives you more flexibility for future operations like updating specific cells or adding formulas. The Sheets API documentation has extensive examples for various spreadsheet manipulations you might need.