I'm trying to write to a Google Sheets document by following this google api, but I'm not having much success. I'm not even sure I'm following the right api, because it says "Appscript" and looks like Swift.
I've been able to read in data from the sheets using:
private let scopes = [kGTLRAuthScopeSheetsSpreadsheets]
private let service = GTLRSheetsService()
override func viewDidLoad() {
super.viewDidLoad()
self.service.authorizer = GIDSignIn.sharedInstance().currentUser.authentication.fetcherAuthorizer()
getData()
}
func getData() {
let spreadsheetId = "1OAbhzY7RlfCEre-I5e-Kcgz16eHz2cGpO9HZKOiizCs" // Portfolio
let query = GTLRSheetsQuery_SpreadsheetsValuesGet.query(withSpreadsheetId: spreadsheetId, range:range)
service.executeQuery(query, delegate: self, didFinish: #selector(displayResultWithTicket(ticket:finishedWithObject:error:)))
}
I tried following the section where the api says:
// This code uses the Sheets Advanced Service, but for most use cases
// the built-in method SpreadsheetApp.getActiveSpreadsheet()
// .getRange(range).setValues(values) is more appropriate.
var values = [
[
// Cell values ...
]
// Additional rows ...
];
var valueRange = Sheets.newValueRange();
valueRange.values = values;
var result = Sheets.Spreadsheets.Values.update(valueRange, spreadsheetId, range, {
valueInputOption: valueInputOption
});
But I'm very lost in what SpreadsheetsApp.getActiveSpreadsheet() is coming from. I don't really know what the SpreadsheetsApp or Sheets object/library even is, and it doesn't seem to exist when I try to use it in my code
Being utterly lost on how to do this, I tried:
func writeData() {
let spreadsheetId = "1OAbhzY7RlfCEre-I5e-Kcgz16eHz2cGpO9HZKOiizCs"
let range = "A1:E"
let query = GTLRSheetsQuery_SpreadsheetsValuesGet.query(withSpreadsheetId: spreadsheetId, range:range)
var values = [[]]
var valueRange = query.newValueRange()
valueRange.values = values
var result = query.values.update(valueRange, spreadsheetId, range, {
valueInputOption: valueInputOption
})
}
But, as expected, I get a lot of errors since query does not have a newValueRange method, or a values.update method or anything like that.
Can anyone help me please? I'm very lost
Here's an answer to your question using the GoogleAPIClientForREST
library that you referenced in your original question (not that using AlamoFire
is incorrect, but the Google Sheets APIs would be the more canonical way to do it):
func updateSheets() {
let range = "Sheet1!A3:C3" // Sheet titled "Sheet1", range A3 to C3
let updateValues = [["Foo", "Bar", "Baz"]]
let valueRange = GTLRSheets_ValueRange() // GTLRSheets_ValueRange holds the updated values and other params
valueRange.majorDimension = "ROWS" // Indicates horizontal row insert
valueRange.range = range
valueRange.values = updateValues
let query = GTLRSheetsQuery_SpreadsheetsValuesAppend.query(withObject: valueRange, spreadsheetId: kSpreadsheetID, range: range) // Use an append query to append at the first blank row
query.valueInputOption = "USER_ENTERED"
service.executeQuery(query) { ticket, object, error in
print("object")
} // `GTLRServiceCompletionHandler` closure containing the service ticket, `GTLRSheets_AppendValuesResponse`, and any error
}
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