How can I make basic writing via Google Sheets API?

On my way of learning Google Sheets API with Swift I wanted to write a single range to a spreadsheet.

Looking into Quickstart iOS Guide and Basic Writing examples I came with this code:

  • Local Notification in Phonegap
  • 'Use of Unresolved Identifier' in Swift
  • Errors while calling images from dictionary
  • subnormal IEEE 754 floating point numbers support on iOS ARM devices (iPhone 4)
  • How to obtain Client Secret in Youtube API for iOS client?
  • Pods-resources.sh Permission denied in iOS Project
  • 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:)))
    }
    

    Don’t know what I did wrong but I got the error message: “The requested URL wasn’t found on this server”.

    2 Solutions Collect From Internet About “How can I make basic writing via Google Sheets API?”

    As @sam-berlin mentioned I should have used fetchObjectByUpdatingObject instead of fetchObjectByInsertUpdating.

    But there was one more little mistake/typo. In constructing JSON I lost a pair of square brackets and got an error about invalid value.

    The working code looks like this:

    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:)))   
    }
    

    It’s also possible to use service.fetchObjectByUpdatingObject(body, forURL: fullUrl, completionHandler: nil) and do not process the response.

    According to the GTLService code, fetchObjectByInsertUpdating does a POST, whereas this request requires a PUT. It seems that fetchObjectByUpdatingObject should do a PUT, so try that instead.