Getting to the google sheets data from query in Swift

240 views
Skip to first unread message

Jim Kardach

unread,
Apr 19, 2021, 9:07:46 PM4/19/21
to Google APIs Client Library for Objective-C

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.

Jim Kardach

unread,
Apr 20, 2021, 6:27:45 PM4/20/21
to Google APIs Client Library for Objective-C
I finally figured this out.  You need to cast the GTLObject to the expected return type for the call.  In this case, I'm doing an GTLRSheetsQuery_SpreadsheetsValueGet where the expected type back would be a GTLRSheets_ValueRange.  So I needed to cast the GTLR Object as this type.
As I'm returning a bunch of spreadsheet rows, it is returning an array of strings.
The let data = result as? GTLRSheets_ValueRange

converts the type.  And then I can access the array as the values property (and I cast this as a double array of strings).

    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")

            }

        }

    }


Reply all
Reply to author
Forward
0 new messages