我如何通过Google Sheets API进行基本写作?

在我用Swift学习Google Sheets API的方法中,我想写一个范围到电子表格。

看看快速入门iOS指南和基本写作的例子我带着这个代码:

func constructAndSendAQuery() { let baseUrl = "https://sheets.googleapis.com/v4/spreadsheets" let spreadsheetId = "1FhbBdEvpcyHzb3Akn1opmru0ire2fLVki5ZolMmZaRs" let range = "Sales!A31:C31" let url = baseUrl + "/" + spreadsheetId + "/values/" + range let params = ["valueInputOption": "RAW"] let fullUrl = GTLUtilities.URLWithString(url, queryParameters: params) let body = GTLObject() body.JSON = ["majorDimension":"ROWS", "values": ["One", "Two", "Three"]] service.fetchObjectByInsertingObject(body, forURL: fullUrl, delegate: self, didFinishSelector: #selector(ViewController.processTheResponse(_:finishedWithObject:error:))) } 

不知道我做错了什么,但我得到了错误信息:“在这个服务器上找不到请求的URL”。

作为@ fetchObjectByUpdatingObject提到我应该使用fetchObjectByUpdatingObject而不是fetchObjectByInsertUpdating

但是还有一个小错误/错字。 在构buildJSON时,我丢失了一对方括号,并且得到了关于无效值的错误。

工作代码如下所示:

 func constructAndSendARequest() { let baseUrl = "https://sheets.googleapis.com/v4/spreadsheets" let spreadsheetId = "1FhbBdEvpcyHzb3Akn1opmru0ire2fLVki5ZolMmZaRs" let range = "Sales!A3:D3" let url = baseUrl + "/" + spreadsheetId + "/values/" + range let params = ["valueInputOption": "RAW"] let fullUrl = GTLUtilities.URLWithString(url, queryParameters: params) let body = GTLObject() body.JSON = ["range": range, "majorDimension": "ROWS", "values": [["One", "Two", "Three"]]] service.fetchObjectByUpdatingObject(body, forURL: fullUrl, delegate: self, didFinishSelector: #selector(ViewController.processTheResponse(_:finishedWithObject:error:))) } 

也可以使用service.fetchObjectByUpdatingObject(body, forURL: fullUrl, completionHandler: nil)并且不处理响应。

根据GTLService代码 , fetchObjectByInsertUpdating执行一个POST ,而这个请求需要一个PUT 。 看来, fetchObjectByUpdatingObject应该做一个PUT ,所以试试。