Search code examples
objective-cgdata-apigdata

Batch insertion of spreadsheet cells using gdata-objectivec-client not working


I am trying to add a batch of cells to a Google Spreadsheet using the GData Objective-C Client as described here: http://code.google.com/p/gdata-objectivec-client/wiki/GDataObjCIntroduction#Batch_requests.

Here is the code of interest:

                      GDataFeedSpreadsheetCell *batchFeed = [GDataFeedSpreadsheetCell spreadsheetCellFeed];
                  NSURL *batchUrl = [[batchFeed batchLink] URL];

                  NSMutableArray *cells = [NSMutableArray array];

                  GDataSpreadsheetCell *cell = [GDataSpreadsheetCell cellWithRow:1 column:1 inputString:@"test" numericValue:nil resultString:nil];

                  GDataEntrySpreadsheetCell *cellEntry = [GDataEntrySpreadsheetCell spreadsheetCellEntryWithCell:cell];
                  [cells addObject:cellEntry];                      
                  [batchFeed setEntries:cells];

                  GDataBatchOperation *op;
                  op = [GDataBatchOperation batchOperationWithType:kGDataBatchOperationInsert];
                  [batchFeed setBatchOperation:op];

                  [service fetchFeedWithBatchFeed:batchFeed forBatchFeedURL:batchUrl completionHandler:nil];

It doesn't work. Clearly, the fetchFeedWithBatchFeed has no reference to my GDataWorksheetEntry object -- so it doesn't surprise me that it isn't working.

What am I leaving out?

Thanks in advance.


Solution

  • And here is the final answer.

    You must perform a query before the batch update to get the entries you are going to update. Sounds obvious in retrospect. Sure does make for a lot of nested blocks though.

                         NSURL *cellsFeedUrl = [[worksheetEntry cellsLink] URL];
    
                      GDataQuerySpreadsheet *querySpreadsheet = [GDataQuerySpreadsheet spreadsheetQueryWithFeedURL:cellsFeedUrl];
                      [querySpreadsheet setMinimumRow:1];
                      [querySpreadsheet setMaximumRow:1];
                      [querySpreadsheet setMinimumColumn:1];
                      [querySpreadsheet setMaximumColumn:7];
                      [querySpreadsheet setShouldReturnEmpty:TRUE];
    
                      [service fetchFeedWithQuery:querySpreadsheet completionHandler:
                       ^(GDataServiceTicket *ticket, GDataFeedBase *feed, NSError *error) {
    
                           // Update one of these cells as a test.
                           GDataEntrySpreadsheetCell *spreadsheetCellEntry = [[feed entries] lastObject];
                           [[spreadsheetCellEntry cell] setInputString:@"test"];
    
                           NSArray *updatedEntries = [feed entries];
                           NSString *eTag = feed.ETag;
    
                           // Get worksheet cells feed
                           [service fetchFeedWithURL:cellsFeedUrl completionHandler:
                            ^(GDataServiceTicket *ticket, GDataFeedBase *feed, NSError *error) {
    
                                NSURL *batchUrl = [[feed batchLink] URL];
                                GDataFeedSpreadsheetCell *batchFeed = [GDataFeedSpreadsheetCell spreadsheetCellFeed];
    
                                [batchFeed setEntriesWithEntries:updatedEntries];
    
                                GDataBatchOperation *op;
                                op = [GDataBatchOperation batchOperationWithType:kGDataBatchOperationUpdate];
                                [batchFeed setBatchOperation:op];
                                [batchFeed setETag:eTag];
    
                                // Perform batch update
                                [service fetchFeedWithBatchFeed:batchFeed forBatchFeedURL:batchUrl
                                              completionHandler:
                                 ^(GDataServiceTicket *ticket, GDataFeedBase *feed, NSError *error) {
    
                                     // no op
    
                                 }];
                            }];
                       }];