1
/* Copyright (c) 2006 Google Inc.
3
* Licensed under the Apache License, Version 2.0 (the "License");
4
* you may not use this file except in compliance with the License.
5
* You may obtain a copy of the License at
7
* http://www.apache.org/licenses/LICENSE-2.0
9
* Unless required by applicable law or agreed to in writing, software
10
* distributed under the License is distributed on an "AS IS" BASIS,
11
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
* See the License for the specific language governing permissions and
13
* limitations under the License.
17
using System.Collections;
21
using Google.GData.Client;
22
using Google.GData.Extensions;
24
namespace Google.GData.Spreadsheets
27
/// Feed API customization class for defining a Cells feed.
29
public class CellFeed : AbstractFeed
35
/// <param name="uriBase">The uri for this cells feed.</param>
36
/// <param name="iService">The Spreadsheets service.</param>
37
public CellFeed(Uri uriBase, IService iService) : base(uriBase, iService)
39
this.AddExtension(new ColCountElement());
40
this.AddExtension(new RowCountElement());
44
/// The number of rows in this cell feed, as a RowCountElement
46
public RowCountElement RowCount
50
return FindExtension(GDataSpreadsheetsNameTable.XmlRowCountElement,
51
GDataSpreadsheetsNameTable.NSGSpreadsheets) as RowCountElement;
56
/// The number of columns in this cell feed, as a ColCountElement
58
public ColCountElement ColCount
62
return FindExtension(GDataSpreadsheetsNameTable.XmlColCountElement,
63
GDataSpreadsheetsNameTable.NSGSpreadsheets) as ColCountElement;
68
/// <summary>checks if this is a namespace
69
/// decl that we already added</summary>
70
/// <param name="node">XmlNode to check</param>
71
/// <returns>true if this node should be skipped </returns>
72
protected override bool SkipNode(XmlNode node)
74
if (base.SkipNode(node) == true)
79
return node.NodeType == XmlNodeType.Attribute
80
&& (node.Name.StartsWith("xmlns") == true)
81
&& (String.Compare(node.Value, GDataSpreadsheetsNameTable.NSGSpreadsheets) == 0);
85
/// creates our cellfeed type entry
87
/// <returns>AtomEntry</returns>
88
public override AtomEntry CreateFeedEntry()
90
return new CellEntry();
94
/// returns an update URI for a given row/column combination
95
/// in general that URI is based on the feeds POST feed plus the
96
/// cell address in RnCn notation:
97
/// http://spreadsheets.google.com/feeds/cells/key/worksheetId/private/full/cell
99
/// <param name="row"></param>
100
/// <param name="column"></param>
101
/// <returns>string</returns>
102
[CLSCompliant(false)]
103
public string CellUri(uint row, uint column)
105
string target = this.Post;
106
if (!target.EndsWith("/"))
110
target += "R" + row.ToString() + "C" + column.ToString();
115
/// returns the given CellEntry object. Note that the getter will go to the server
116
/// to get CellEntries that are NOT yet on the client
118
/// <returns>CellEntry</returns>
119
[CLSCompliant(false)]
120
public CellEntry this[uint row, uint column]
124
// let's find the cell
125
foreach (CellEntry entry in this.Entries )
127
CellEntry.CellElement cell = entry.Cell;
128
if (cell.Row == row && cell.Column == column)
133
// if we are here, we need to get the entry from the service
134
string url = CellUri(row, column);
135
CellQuery query = new CellQuery(url);
137
CellFeed feed = this.Service.Query(query) as CellFeed;
138
CellEntry newEntry = feed.Entries[0] as CellEntry;
139
this.Entries.Add(newEntry);
140
// we don't want this one to show up in the batch feed on it's own
141
newEntry.Dirty = false;
148
/// deletes a cell by using row and column addressing
150
/// <param name="row"></param>
151
/// <param name="column"></param>
152
[CLSCompliant(false)]
153
public void Delete(uint row, uint column)
155
// now we need to create a new guy
156
string url = CellUri(row, column);
157
this.Service.Delete(new Uri(url));
160
//////////////////////////////////////////////////////////////////////
161
/// <summary>uses GData batch to batchupdate the cell feed. If the returned
162
/// batch result set contained an error, it will throw a GDataRequestBatchException</summary>
163
/// <returns> </returns>
164
//////////////////////////////////////////////////////////////////////
165
public override void Publish()
167
if (this.Batch == null)
169
throw new InvalidOperationException("This feed has no batch URI");
172
AtomFeed batchFeed = CreateBatchFeed(GDataBatchOperationType.update);
173
if (batchFeed != null)
175
AtomFeed resultFeed = this.Service.Batch(batchFeed, new Uri(this.Batch));
176
foreach (AtomEntry resultEntry in resultFeed.Entries )
178
GDataBatchEntryData data = resultEntry.BatchData;
179
if (data.Status.Code != (int) HttpStatusCode.OK)
181
throw new GDataBatchRequestException(resultFeed);
185
// if we get here, everything is fine. So update the edit URIs in the original feed,
186
// because those might have changed.
187
foreach (AtomEntry resultEntry in resultFeed.Entries )
189
AtomEntry originalEntry = this.Entries.FindById(resultEntry.Id);
190
if (originalEntry == null)
192
throw new GDataBatchRequestException(resultFeed);
194
if (originalEntry != null)
196
originalEntry.EditUri = resultEntry.EditUri;
203
/////////////////////////////////////////////////////////////////////////////