/clients/cs/src/gspreadsheet/spreadsheets.cs

http://google-gdata.googlecode.com/ · C# · 314 lines · 193 code · 43 blank · 78 comment · 25 complexity · 0e1bb2f17d8ff308c39dfad5f21d94ea MD5 · raw file

  1. /* Copyright (c) 2006 Google Inc.
  2. *
  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
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  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.
  14. */
  15. using Google.GData.Client;
  16. using Google.GData.Spreadsheets;
  17. using System.Collections.Generic;
  18. using System;
  19. namespace Google.Spreadsheets
  20. {
  21. //////////////////////////////////////////////////////////////////////
  22. /// <summary>the main object to access everything else with
  23. ///
  24. /// </summary>
  25. //////////////////////////////////////////////////////////////////////
  26. public class Application
  27. {
  28. /// <summary>
  29. /// the name of this application
  30. /// </summary>
  31. public static string Name = ".NETSDK Spreadsheets";
  32. private static SpreadsheetsService service;
  33. private static SpreadsheetEntry se;
  34. private static WorksheetEntry we;
  35. private static SpreadsheetFeed sf;
  36. //private static string currentSpreadsheet; //not used //TODO determine if this is a bug
  37. /// <summary>
  38. /// default constructor for client login.
  39. /// </summary>
  40. /// <param name="user">the username to user</param>
  41. /// <param name="password">the users password</param>
  42. /// <returns></returns>
  43. public Application(string user, string password)
  44. {
  45. Application.service = new SpreadsheetsService(Application.Name);
  46. Application.service.setUserCredentials(user, password);
  47. }
  48. /// <summary>
  49. /// constructor for webapplications. Obtain the token using the authsub
  50. /// helper methods
  51. /// </summary>
  52. /// <param name="token">Your authentication token</param>
  53. /// <returns></returns>
  54. public Application(string token)
  55. {
  56. GAuthSubRequestFactory authFactory =
  57. new GAuthSubRequestFactory("wise", Application.Name);
  58. authFactory.Token = token;
  59. Application.service = new SpreadsheetsService(authFactory.ApplicationName);
  60. Application.service.RequestFactory = authFactory;
  61. }
  62. /// <summary>
  63. /// this will reload the list of spreadsheets from the server and reset the
  64. /// currently active spreadsheet/worksheet
  65. /// </summary>
  66. /// <returns></returns>
  67. public void Refresh()
  68. {
  69. Application.sf = null;
  70. Application.se = null;
  71. Application.we = null;
  72. }
  73. /// <summary>
  74. /// this returns a list of strings, with the names of each spreadsheet
  75. /// this will do a roundtrip to the google servers to retrieve the list
  76. /// of spreadsheets if not done yet
  77. /// </summary>
  78. /// <returns>List of strings</returns>
  79. public List<string> Spreadsheets
  80. {
  81. get
  82. {
  83. List<string> results = new List<string>();
  84. EnsureSpreadsheetFeed();
  85. foreach (SpreadsheetEntry entry in Application.sf.Entries)
  86. {
  87. results.Add(entry.Title.Text);
  88. }
  89. return results;
  90. }
  91. }
  92. /// <summary>
  93. /// this will return the current spreadsheetname that is being worked on, or, if set
  94. /// see if there is a spreadsheet of that name, and if so, make this the current spreadsheet
  95. /// </summary>
  96. /// <returns></returns>
  97. public string CurrentSpreadsheet
  98. {
  99. get
  100. {
  101. if (Application.se != null)
  102. {
  103. return Application.se.Title.Text;
  104. }
  105. return null;
  106. }
  107. set
  108. {
  109. Application.se = null;
  110. if (value == null)
  111. {
  112. return;
  113. }
  114. EnsureSpreadsheetFeed();
  115. foreach (SpreadsheetEntry entry in Application.sf.Entries)
  116. {
  117. if (value.Equals(entry.Title.Text))
  118. {
  119. Application.se = entry;
  120. break;
  121. }
  122. }
  123. if (Application.se == null)
  124. {
  125. throw new ArgumentException(value + " was not a valid spreadsheet name");
  126. }
  127. }
  128. }
  129. /// <summary>
  130. /// returns a list of Worksheet names for the currently set spreadsheet,
  131. /// or NULL if no spreadsheet was set
  132. /// </summary>
  133. /// <returns>NULL for no spreadsheet, an empty list for no worksheets in the
  134. /// spreadsheet or a list of names of the worksheets in the current spreadsheet</returns>
  135. public List<string> WorkSheets
  136. {
  137. get
  138. {
  139. if (Application.se != null)
  140. {
  141. List<string> result = new List<string>();
  142. WorksheetFeed feed = Application.se.Worksheets;
  143. foreach (WorksheetEntry worksheet in feed.Entries)
  144. {
  145. result.Add(worksheet.Title.Text);
  146. }
  147. return result;
  148. }
  149. return null;
  150. }
  151. }
  152. /// <summary>
  153. /// this will return the current worksheetname that is being worked on, or, if set
  154. /// see if there is a worksheet of that name, and if so, make this the current worksheet
  155. /// Note that this requires that a current spreadsheet is set
  156. /// </summary>
  157. /// <returns></returns>
  158. public string CurrentWorksheet
  159. {
  160. get
  161. {
  162. if (Application.we != null)
  163. {
  164. return Application.we.Title.Text;
  165. }
  166. return null;
  167. }
  168. set
  169. {
  170. Application.we = null;
  171. if (value == null)
  172. {
  173. return;
  174. }
  175. if (Application.se != null)
  176. {
  177. WorksheetFeed feed = Application.se.Worksheets;
  178. foreach (WorksheetEntry worksheet in feed.Entries)
  179. {
  180. if (value.Equals(worksheet.Title.Text))
  181. {
  182. Application.we = worksheet;
  183. break;
  184. }
  185. }
  186. }
  187. else
  188. {
  189. throw new ArgumentException(value + " is invalid if no spreadsheet is set");
  190. }
  191. }
  192. }
  193. /// <summary>
  194. /// get the whole spreadsheet as a range, one line is one row, one cell is
  195. /// </summary>
  196. /// <returns></returns>
  197. public List<string> CompleteRange
  198. {
  199. get
  200. {
  201. if (this.CurrentWorksheet != null)
  202. {
  203. CellFeed cells = Application.we.QueryCellFeed(ReturnEmptyCells.yes);
  204. List<string> result = new List<string>();
  205. foreach (CellEntry curCell in cells.Entries)
  206. {
  207. uint r = curCell.Cell.Row, c = curCell.Cell.Column;
  208. result.Add("R" + r + "C" + c + " " + curCell.Cell.Value);
  209. }
  210. return result;
  211. }
  212. return null;
  213. }
  214. }
  215. private void EnsureSpreadsheetFeed()
  216. {
  217. if (Application.sf == null)
  218. {
  219. SpreadsheetQuery query = new SpreadsheetQuery();
  220. Application.sf = service.Query(query);
  221. }
  222. }
  223. ///
  224. /// Parse a range given as, e.g., A2:D4 into numerical
  225. /// coordinates. The letter indicates the column in use,
  226. /// the number the row
  227. ///
  228. public int[] ParseRangeString(String range)
  229. {
  230. int [] retArray = { -1, -1, -1, -1 };
  231. range = range.ToUpper();
  232. string []address = range.Split(':');
  233. int count = 0;
  234. foreach (string s in address )
  235. {
  236. string rowString=null;
  237. string colString=null;
  238. int rowStart = -1;
  239. for (int i=0; i< s.Length; i++)
  240. {
  241. if (Char.IsDigit(s, i))
  242. {
  243. rowStart = i;
  244. break;
  245. }
  246. }
  247. if (rowStart > 0)
  248. {
  249. rowString = s.Substring(rowStart);
  250. colString = s.Substring(0, rowStart);
  251. }
  252. // rowstring should be a numer, so that is easy
  253. retArray[count+1] = Int32.Parse(rowString);
  254. // colstring is a tad more complicated. a column addressed
  255. // with DE for example is 26 * 4 + 5 ... and ZFE would be
  256. // 26 * 26 * 26 + 6 * 26 + 5
  257. // so we go from right to left
  258. int colValue = 0;
  259. for (int y=0, x = colString.Length-1; x >= 0; x--, y++)
  260. {
  261. char c = colString[x];
  262. colValue += (c - 'A' + 1) * (int) Math.Pow(26, y);
  263. }
  264. retArray[count] = colValue;
  265. count+=2;
  266. }
  267. return retArray;
  268. }
  269. }
  270. //end of public class Application
  271. }