I am trying to edit google sheets from within swift. I used the original Quickstart tutorial by google, but since that is for reading a spreadsheet, I had to slightly edit it for writing to a spreadsheet.
//Signing in as normal from the Google Quickstart
//This function is the one I edited, to write to the spreadsheet
func listMajors() {
output.text = "Getting sheet data..."
let spreadsheetId = "**********************"
let range = "Sheet 1!A2"
let valueRange = GTLRSheets_ValueRange.init()
valueRange.values = [["Hello" as Any]]
let query = GTLRSheetsQuery_SpreadsheetsValuesUpdate.query(withObject: valueRange, spreadsheetId: spreadsheetId, range: range)
query.valueInputOption = "USER_ENTERED"
service.executeQuery(query, delegate: self, didFinish: #selector(displayResultWithTicket(ticket:finishedWithObject:error:)))
}
func displayResultWithTicket(ticket: GTLRServiceTicket, finishedWithObject result : GTLRSheets_ValueRange, error : NSError?) {
if let error = error {
showAlert(title: "Error", message: error.localizedDescription)
return
}
print(result)
}
It seemed to work fine for the reading spreadsheet, but it is not working for writing to spreadsheet. The app launches, then i get an error alert with this:

Sorry to add solution in objective-C, but i think you will be able to manage conversion
I just have the same problem today. The solution is simple, when initializing the GIDSignIn
// Configure Google Sign-in.
GIDSignIn* signIn = [GIDSignIn sharedInstance];
signIn.delegate = self;
signIn.uiDelegate = self;
signIn.scopes = [NSArray arrayWithObjects:kGTLRAuthScopeSheetsSpreadsheets, nil];
[signIn signInSilently];
Note the scope value. In the quickStart tutorial, kGTLRAuthScopeSheetsSpreadsheetsReadonly is used.
Romain
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With