如何从Swift中的函数返回Google Sheets API查询的结果?

时间:2019-07-17 18:26:35

标签: ios swift google-sheets-api

我正在尝试编写一个getCell方法,该方法返回以A1表示法指定的某个单元格的内容作为参数。闭包内部的代码显然应该返回void,这使我无法访问查询结果以将其返回。下面的代码演示了我的意图。有没有办法写这种方法让我返回查询结果?

func getCell(a1: String) -> String {
    let spreadsheetId = "INSERT ID HERE"
    let getQuery = GTLRSheetsQuery_SpreadsheetsValuesGet.query(withSpreadsheetId: spreadsheetId, range: a1)
    service.executeQuery(getQuery) { (ticket, result, error) in
        if let response = result {
            let rows = (response as! GTLRSheets_ValueRange).values!
            for row in rows {
                for item in row {
                    // I know this line doesn't work but this is the idea I want to achieve
                    return (item as! String)
                }
            }
        }
        else {
            print("Result was nil")
        }
    }
}

0 个答案:

没有答案