I'm new to swift, and I was able to read a google sheet in objective-c, and then put the resulting objects data into a NSARRAY and access that data.
- (void)readSheet {
GTLRSheetsQuery_SpreadsheetsValuesGet *query =
[GTLRSheetsQuery_SpreadsheetsValuesGet queryWithSpreadsheetId:ACT_SSHEET_ID
range:@"Accounts!A4:AQ130"];
[self.service executeQuery:query
completionHandler:^(GTLRServiceTicket *ticket,
GTLRSheets_ValueRange *result,
NSError *error) {
if (error == nil) {
NSArray *rows = result.values;
}
}
However now I'm converting everything into swift, and I get the same object back, but I can no longer access the array. My code looks as follows:
func readSheet() {
let query = GTLRSheetsQuery_SpreadsheetsValuesGet.query(withSpreadsheetId: K.ACT_SSHEET_ID, range: "Accounts!A3:AQ")
sheetService.executeQuery(query) { (ticket:GTLRServiceTicket, result:Any?, error:Error?) in
if let error = error {
print("Error", error.localizedDescription)
} else {
// how to access result data in swift.
}
}
}
In swift if I try to directly access the array it gives an error. How do I get the rows data out of the GTLRObject in swift?
thanks for any help.
func readSheet() {
let query = GTLRSheetsQuery_SpreadsheetsValuesGet.query(withSpreadsheetId: K.ACT_SSHEET_ID, range: "Accounts!A3:AQ")
sheetService.executeQuery(query) { (ticket:GTLRServiceTicket, result:Any?, error:Error?) in
if let error = error {
print("Error", error.localizedDescription)
} else {
let data = result as? GTLRSheets_ValueRange
let rows = data?.values as? [[String]] ?? [[""]]
for row in rows {
if row.count < 40 {
continue
}
print("row: ", row)
}
print(data!)
print("success")
}
}
}