【问题标题】:Batch insertion of spreadsheet cells using gdata-objectivec-client not working使用 gdata-objectivec-client 批量插入电子表格单元格不起作用
【发布时间】:2011-08-17 22:48:29
【问题描述】:

我正在尝试使用 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 对象的引用——所以它不起作用并不让我感到惊讶。

我遗漏了什么?

提前致谢。

【问题讨论】:

  • 正如我所提到的,我认为问题在于我没有提供对我的 GDataEntryWorksheet 对象的引用。我怀疑批处理提要以某种方式必须从该对象派生,但我没有看到从我的工作表条目对象提供单元格提要的方法。

标签: objective-c gdata-api gdata


【解决方案1】:

这是最终的答案。

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

                     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

                             }];
                        }];
                   }];

【讨论】:

    【解决方案2】:

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

    首先,解决方案。一旦你有一个 GDataWorksheetEntry,这段代码将执行一个批处理操作——在这种情况下是一个插入:

                          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}"
    

    )

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

    请注意,如果您将操作更改为更新,则会得到以下信息:

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

    )

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

    【讨论】:

    • 嗨。你找到这个问题的答案了吗?
    猜你喜欢
    • 2012-07-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-11
    • 1970-01-01
    • 2011-02-04
    • 1970-01-01
    • 2011-03-13
    相关资源
    最近更新 更多