I am building an app where I need to put data into certain cells of a Google Sheet. Right now, my code only puts data in one continuous row, but I want to change that. I need to take text from fields in my app and drop them into exact cells. For example, the text from one input should go to cell C5 and the text from another should be placed in cell G11.
Below is a new example of how I might set this up:
NSString *inputOne = self.inputFieldOne.text;
NSString *inputTwo = self.inputFieldTwo.text;
GDataSpreadsheetCell *targetCellOne = [GDataSpreadsheetCell cellWithRow:5 column:3 inputString:inputOne numericValue:nil resultString:nil];
GDataSpreadsheetCell *targetCellTwo = [GDataSpreadsheetCell cellWithRow:11 column:7 inputString:inputTwo numericValue:nil resultString:nil];
// How can I update these target cells in my Google Sheet with the new inputs?
I would appreciate any advice on how to modify my code to achieve this cell-specific data placement.
hey sparklinggem, have u tried using the google sheets api directly? it lets u update specific cells. u’d need to set up authentication and use the sheets.spreadsheets.values.update method. might be easier than the GData library. good luck with ur app!
I’ve worked with Google Sheets integration before, and I can suggest an alternative approach using the Google Sheets API v4. It’s more robust for cell-specific updates than GData.
First, install the Google Sheets API v4 client library for iOS. Then, set up authentication using OAuth 2.0. Once that’s done, you can use GTLRSheetsService to interact with your spreadsheet.
Here’s a basic example of updating specific cells:
GTLRSheetsService *service = [[GTLRSheetsService alloc] init];
service.authorizer = // Your auth method
NSString *spreadsheetId = @"your-spreadsheet-id";
NSString *range = @"Sheet1!C5,G11";
GTLRSheets_ValueRange *valueRange = [[GTLRSheets_ValueRange alloc] init];
valueRange.values = @[@[self.inputFieldOne.text, self.inputFieldTwo.text]];
GTLRSheetsQuery_SpreadsheetsValuesUpdate *query =
[GTLRSheetsQuery_SpreadsheetsValuesUpdate queryWithObject:valueRange
spreadsheetId:spreadsheetId
range:range];
query.valueInputOption = @"USER_ENTERED";
[service executeQuery:query
completionHandler:^(GTLRServiceTicket *ticket,
GTLRSheets_UpdateValuesResponse *result,
NSError *error) {
// Handle response
}];
This method allows you to update multiple cells in one API call, which is more efficient.
I’ve actually tackled a similar challenge in one of my projects. Instead of using GData, I found success with the Google Sheets API v4. It offers more flexibility for cell-specific updates.
First, you’ll need to set up the Google Sheets API in your project. Once that’s done, you can use the GTLRSheetsService class to interact with your spreadsheet.
Here’s a rough example of how you might update specific cells:
GTLRSheetsService *service = [[GTLRSheetsService alloc] init];
service.authorizer = // Your auth method here
NSString *spreadsheetId = @\"your-spreadsheet-id\";
NSString *range = @\"Sheet1!C5:C5\";
GTLRSheets_ValueRange *valueRange = [[GTLRSheets_ValueRange alloc] init];
valueRange.values = @[@[self.inputFieldOne.text]];
GTLRSheetsQuery_SpreadsheetsValuesUpdate *query =
[GTLRSheetsQuery_SpreadsheetsValuesUpdate queryWithObject:valueRange
spreadsheetId:spreadsheetId
range:range];
query.valueInputOption = @\"USER_ENTERED\";
[service executeQuery:query
completionHandler:^(GTLRServiceTicket *ticket,
GTLRSheets_UpdateValuesResponse *result,
NSError *error) {
// Handle the response
}];
This approach gives you precise control over which cells you’re updating. Hope this helps!