使用gdata-objectivec-client无法正常批量插入电子表格单元格

时间:2011-08-17 22:48:30

标签: objective-c gdata-api gdata

我正在尝试使用GData Objective-C客户端将一批单元格添加到Google电子表格中,如下所述:http://code.google.com/p/gdata-objectivec-client/wiki/GDataObjCIntroduction#Batch_requests

以下是感兴趣的代码:

                      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];

它不起作用。显然,fetchFeedWithBatchFeed没有引用我的GDataWorksheetEntry对象 - 所以它不会让我感到惊讶它不起作用。

我要离开的是什么?

提前致谢。

2 个答案:

答案 0 :(得分:3)

这是最后的答案。

您必须在批量更新之前执行查询以获取要更新的条目。回想起来听起来很明显。当然确实会为很多嵌套块做好准备。

                     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

                             }];
                        }];
                   }];

答案 1 :(得分:0)

我解决了这个问题并暴露了一个新问题。

首先,解决方案。有了GDataWorksheetEntry后,此代码将执行批处理操作 - 在本例中为insert:

                      NSURL *cellsFeedUrl = [[worksheetEntry cellsLink] URL];

                  // Get worksheet cells feed
                  [service fetchFeedWithURL:cellsFeedUrl completionHandler:
                   ^(GDataServiceTicket *ticket, GDataFeedBase *feed, NSError *error) {

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

                       NSMutableArray *cells = [NSMutableArray array];

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

                       GDataEntrySpreadsheetCell *cellEntry = [GDataEntrySpreadsheetCell spreadsheetCellEntryWithCell:cell];

                       static unsigned int staticID = 0;
                       NSString *batchID = [NSString stringWithFormat:@"batchID_%u", ++staticID];
                       [cellEntry setBatchIDWithString:batchID];

                       [cells addObject:cellEntry];                      
                       [batchFeed setEntriesWithEntries:cells];

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

                       [service fetchFeedWithBatchFeed:batchFeed forBatchFeedURL:batchUrl
                                     completionHandler:
                        ^(GDataServiceTicket *ticket, GDataFeedBase *feed, NSError *error) {

                            NSLog(@"FEED ENTRIES: %@",[feed entries]);
                            NSLog(@"ERROR: %@",error);

                        }];
                   }];

不幸的是,当您访问生成的批处理提要(在日志记录语句中完成)时,您将看到:

FEED ENTRIES: (
"GDataEntrySpreadsheetCell 0x5fcf510: {v:3.0 title:Error content:Insert not supported on batch. id:batchID_2 batchID:batchID_2 batchStatus:501}"

所以现在我需要弄清楚如何解决谷歌缺乏对批量插入的支持。

请注意,如果您更改要更新的操作,请执行以下操作:

FEED ENTRIES: (
"GDataEntrySpreadsheetCell 0xae73880: {v:3.0 title:Error content:Missing entry id id:batchID_2 batchID:batchID_2 batchStatus:400}"

缺少条目ID是因为我创建了单元格而没有引用现有单元格 - 所以我将专注于尝试提供该引用并提供批量更新调用而不是批量插入调用。