我正在尝试使用Sheets API(使用GTLRSheetsQuery_SpreadsheetsValuesGet.query函数)从Google表格文档中读取数据 . 它检索罚款和一切 .
但是,我遇到了一个问题,因为我从工作表中读取的信息是财务数据(Google表格文档中的单元格使用GoogleFinance) . 因此,我相信当我的iOS应用程序开始阅读数据时,Google Sheet会从GoogleFinance检索数据的时间很短,并且该单元格暂时将“正在加载...”作为其值 . 这个“正在加载...”的值是swift实际读入的数据,并且它从未向我提供我想要的实际财务数据 .
我还看到this google description of the "Loading..." error,它只是说不在自定义函数中使用它(我假设这同样适用于我的场景),但我仍然需要一种方法来获取财务数据 .
我无法弄清楚这个“正在加载...”错误,因为我无法在读取数据之前加载Google表格 . 有人知道解决方案或解决方法吗?
import UIKit
import GoogleSignIn
import GoogleAPIClientForREST
import Foundation
class testController: UIViewController {
private let scopes = [kGTLRAuthScopeSheetsSpreadsheetsReadonly]
private let service = GTLRSheetsService()
override func viewDidLoad() {
super.viewDidLoad()
//login was done at before this
self.service.authorizer = GIDSignIn.sharedInstance().currentUser.authentication.fetcherAuthorizer()
getData()
}
@objc func getData() {
let spreadsheetId = "1OAbhzY7RlfCEre-I5e-Kcgz16eHz2cGpO9HZKOiizCs" // Example stock data
let range = "A1:E"
let query = GTLRSheetsQuery_SpreadsheetsValuesGet.query(withSpreadsheetId: spreadsheetId, range:range)
service.executeQuery(query, delegate: self, didFinish:#selector(displayResultWithTicket(ticket:finishedWithObject:error:)))
}
@objc func displayResultWithTicket(ticket: GTLRServiceTicket, finishedWithObject result : GTLRSheets_ValueRange, error : NSError?) {
if let error = error {
showAlert(title: "Error", message: error.localizedDescription)
return
}
let data = result.values!
for row in data {
print("\(row[0] as! String) : \(row[1] as! String) : \(row[2] as! String) : \(row[3] as! String)")
}
// this line prints "SKX : Loading..."
// occasionally will work (not sure why) but often fails
}
func showAlert(title : String, message: String) {
let alert = UIAlertController(
title: title,
message: message,
preferredStyle: UIAlertControllerStyle.alert
)
let ok = UIAlertAction(
title: "OK",
style: UIAlertActionStyle.default,
handler: nil
)
alert.addAction(ok)
present(alert, animated: true, completion: nil)
}
}