使用 gdata-objectivec-client 批量插入电子表格单元格不起作用

发布于 2024-11-30 04:00:03 字数 1369 浏览 0 评论 0原文

我正在尝试使用 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 对象 - 因此它不起作用并不令我感到惊讶。

我遗漏了什么?

提前致谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

暗喜 2024-12-07 04:00:03

这是最终的答案。

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

                     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

                             }];
                        }];
                   }];

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

                             }];
                        }];
                   }];
零度℉ 2024-12-07 04:00:03

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

首先,解决方案。一旦您有了 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 是因为我创建单元格时没有引用现有单元格 - 因此我将专注于尝试提供该引用并提供批量更新调用,而不是批量插入调用。

I solved this problem and exposed a new issue.

First, the solution. Once you have a GDataWorksheetEntry, this code will perform a batch operation -- in this case an 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);

                        }];
                   }];

Unfortunately, when you access the resulting batch feed (done in the logging statement) you will see this:

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

)

So now I need to figure out how to hack around Google's lack of support for batch insertion.

Note that if you change the operation to update you get this:

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

)

The missing entry id is because I created the cell without referencing an existing cell -- so I will focus on trying to provide that reference and providing a batch update call instead of a batch insert call.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文