Advertisement
Guest User

Untitled

a guest
May 2nd, 2017
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 17.71 KB | None | 0 0
  1.  
  2.  
  3. 1. Imports System
  4. 2. Imports System.Collections
  5. 3. Imports System.Text
  6. 4. Imports Google.GData.Client
  7. 5. Imports Google.GData.Extensions
  8. 6. Imports Google.GData.Spreadsheets
  9. 7.
  10. 8. Namespace Spreadsheets
  11. 9. ''' <summary>
  12. 10. ''' SpreadsheetDemo is a simple console-based application
  13. 11. ''' to demonstrate the operations supported by the Google
  14. 12. ''' Spreadsheets Data API. It requires authentication in
  15. 13. ''' the form of your Google Docs & Spreadsheets username
  16. 14. ''' and password, and performs a number of operations on
  17. 15. ''' a worksheet of your choice.
  18. 16. ''' </summary>
  19. 17. Class SpreadsheetDemo
  20. 18. Private Shared userName As String, password As String
  21. 19. Private Shared allWorksheets As New ArrayList()
  22. 20.
  23. 21. ''' <summary>
  24. 22. ''' Prints a list of all worksheets in the specified spreadsheet.
  25. 23. ''' </summary>
  26. 24. ''' <param name="service">an authenticated SpreadsheetsService object</param>
  27. 25. ''' <param name="entry">the spreadsheet whose worksheets are to be retrieved</param>
  28. 26. Private Shared Sub PrintAllWorksheets(ByVal service As SpreadsheetsService, ByVal entry As SpreadsheetEntry)
  29. 27. Dim link As AtomLink = entry.Links.FindService(GDataSpreadsheetsNameTable.WorksheetRel, Nothing)
  30. 28.
  31. 29. Dim query As New WorksheetQuery(link.HRef.ToString())
  32. 30. Dim feed As WorksheetFeed = service.Query(query)
  33. 31.
  34. 32. Console.WriteLine("Worksheets in " & entry.Title.Text & ":")
  35. 33. For Each worksheet As WorksheetEntry In feed.Entries
  36. 34. allWorksheets.Add(worksheet)
  37. 35. Console.WriteLine(" " & worksheet.Title.Text)
  38. 36. Next
  39. 37. Console.WriteLine()
  40. 38. End Sub
  41. 39.
  42. 40. ''' <summary>
  43. 41. ''' Prints a list of all the user's spreadsheets, and the
  44. 42. ''' list of worksheets that each spreadsheet contains.
  45. 43. ''' </summary>
  46. 44. ''' <param name="service">an authenticated SpreadsheetsService object</param>
  47. 45. Private Shared Sub PrintAllSpreadsheetsAndWorksheets(ByVal service As SpreadsheetsService)
  48. 46. Dim query As New SpreadsheetQuery()
  49. 47. Dim feed As SpreadsheetFeed = service.Query(query)
  50. 48.
  51. 49. Console.WriteLine("Your spreadsheets:")
  52. 50. For Each entry As SpreadsheetEntry In feed.Entries
  53. 51. Console.WriteLine("Spreadsheet: {0}", entry.Title.Text)
  54. 52. PrintAllWorksheets(service, entry)
  55. 53. Next
  56. 54. End Sub
  57. 55.
  58. 56. ''' <summary>
  59. 57. ''' Retrieves and prints a list feed of the specified worksheet.
  60. 58. ''' </summary>
  61. 59. ''' <param name="service">an authenticated SpreadsheetsService object</param>
  62. 60. ''' <param name="entry">the worksheet to retrieve</param>
  63. 61. ''' <param name="reverseRows">true if the rows in the worksheet should
  64. 62. ''' be reversed when returned from the server</param>
  65. 63. Private Shared Sub RetrieveListFeed(ByVal service As SpreadsheetsService, ByVal entry As WorksheetEntry, ByVal reverseRows As Boolean)
  66. 64. Dim listFeedLink As AtomLink = entry.Links.FindService(GDataSpreadsheetsNameTable.ListRel, Nothing)
  67. 65.
  68. 66. Console.WriteLine()
  69. 67. Console.WriteLine("This worksheet's list feed URL is:")
  70. 68. Console.WriteLine(listFeedLink.HRef)
  71. 69.
  72. 70. Dim query As New ListQuery(listFeedLink.HRef.ToString())
  73. 71. If reverseRows Then
  74. 72. query.OrderByPosition = True
  75. 73. query.Reverse = True
  76. 74. End If
  77. 75. Dim feed As ListFeed = service.Query(query)
  78. 76.
  79. 77. Console.WriteLine()
  80. 78. Console.WriteLine("Worksheet has {0} rows:", feed.Entries.Count)
  81. 79. For Each worksheetRow As ListEntry In feed.Entries
  82. 80. Dim elements As ListEntry.CustomElementCollection = worksheetRow.Elements
  83. 81. For Each element As ListEntry.Custom In elements
  84. 82. Console.Write(element.Value & vbTab)
  85. 83. Next
  86. 84. Console.WriteLine()
  87. 85. Next
  88. 86. End Sub
  89. 87.
  90. 88. ''' <summary>
  91. 89. ''' Executes a structured query against the list feed of
  92. 90. ''' the specified worksheet.
  93. 91. ''' </summary>
  94. 92. ''' <param name="service">an authenticated SpreadsheetsService object</param>
  95. 93. ''' <param name="entry">the worksheet to query</param>
  96. 94. ''' <param name="queryText">the structured query</param>
  97. 95. Private Shared Sub StructuredQuery(ByVal service As SpreadsheetsService, ByVal entry As WorksheetEntry, ByVal queryText As String)
  98. 96. Dim listFeedLink As AtomLink = entry.Links.FindService(GDataSpreadsheetsNameTable.ListRel, Nothing)
  99. 97.
  100. 98. Dim query As New ListQuery(listFeedLink.HRef.ToString())
  101. 99. query.SpreadsheetQuery = queryText
  102. 100. Dim feed As ListFeed = service.Query(query)
  103. 101.
  104. 102. Console.WriteLine()
  105. 103. Console.WriteLine("{0} rows matched your query:", feed.Entries.Count)
  106. 104. For Each worksheetRow As ListEntry In feed.Entries
  107. 105. Dim elements As ListEntry.CustomElementCollection = worksheetRow.Elements
  108. 106. For Each element As ListEntry.Custom In elements
  109. 107. Console.Write(element.Value & vbTab)
  110. 108. Next
  111. 109. Console.WriteLine()
  112. 110. Next
  113. 111. End Sub
  114. 112.
  115. 113. ''' <summary>
  116. 114. ''' Inserts a new row in the specified worksheet.
  117. 115. ''' </summary>
  118. 116. ''' <param name="service">an authenticated SpreadsheetsService object</param>
  119. 117. ''' <param name="entry">the worksheet into which the row will be inserted</param>
  120. 118. ''' <returns>the inserted ListEntry object, representing the new row</returns>
  121. 119. Private Shared Function InsertRow(ByVal service As SpreadsheetsService, ByVal entry As WorksheetEntry) As ListEntry
  122. 120. Dim listFeedLink As AtomLink = entry.Links.FindService(GDataSpreadsheetsNameTable.ListRel, Nothing)
  123. 121.
  124. 122. Dim query As New ListQuery(listFeedLink.HRef.ToString())
  125. 123. Dim feed As ListFeed = service.Query(query)
  126. 124.
  127. 125. Dim firstRow As ListEntry = TryCast(feed.Entries(0), ListEntry)
  128. 126. Dim newRow As New ListEntry()
  129. 127.
  130. 128. Console.WriteLine()
  131. 129. Console.WriteLine("Inserting a new row...")
  132. 130. For Each element As ListEntry.Custom In firstRow.Elements
  133. 131. Console.Write("Enter the value of column ""{0}"": ", element.LocalName)
  134. 132. Dim elementValue As [String] = Console.ReadLine()
  135. 133.
  136. 134. Dim curElement As New ListEntry.Custom()
  137. 135. curElement.LocalName = element.LocalName
  138. 136. curElement.Value = elementValue
  139. 137.
  140. 138. newRow.Elements.Add(curElement)
  141. 139. Next
  142. 140.
  143. 141. Dim insertedRow As ListEntry = feed.Insert(newRow)
  144. 142. Console.WriteLine("Successfully inserted new row: ""{0}""", insertedRow.Content.Content)
  145. 143.
  146. 144. Return insertedRow
  147. 145. End Function
  148. 146.
  149. 147. ''' <summary>
  150. 148. ''' Updates the value of a cell in a single worksheet row.
  151. 149. ''' </summary>
  152. 150. ''' <param name="service">an authenticated SpreadsheetsService object</param>
  153. 151. ''' <param name="entry">the ListEntry representing the row to update</param>
  154. 152. ''' <returns>the updated ListEntry object</returns>
  155. 153. Private Shared Function UpdateRow(ByVal service As SpreadsheetsService, ByVal entry As ListEntry) As ListEntry
  156. 154. Dim firstColumn As ListEntry.Custom = entry.Elements(0)
  157. 155.
  158. 156. Console.WriteLine()
  159. 157. Console.Write("Enter a new value for ""{0}"" (currently ""{1}""): ", firstColumn.LocalName, firstColumn.Value)
  160. 158. Dim newValue As [String] = Console.ReadLine()
  161. 159.
  162. 160. firstColumn.Value = newValue
  163. 161.
  164. 162. Dim updatedRow As ListEntry = TryCast(entry.Update(), ListEntry)
  165. 163.
  166. 164. Console.WriteLine("Successfully updated ""{0}"": ""{1}""", updatedRow.Elements(0).LocalName, updatedRow.Elements(0).Value)
  167. 165.
  168. 166. Return updatedRow
  169. 167. End Function
  170. 168.
  171. 169. ''' <summary>
  172. 170. ''' Demonstrates retrieving and printing the cell feed for a
  173. 171. ''' worksheet.
  174. 172. ''' </summary>
  175. 173. ''' <param name="service">an authenticated SpreadsheetsService object</param>
  176. 174. ''' <param name="entry">the worksheet whose cell feed is to be retrieved</param>
  177. 175. Private Shared Sub RetrieveCellFeed(ByVal service As SpreadsheetsService, ByVal entry As WorksheetEntry)
  178. 176. Dim cellFeedLink As AtomLink = entry.Links.FindService(GDataSpreadsheetsNameTable.CellRel, Nothing)
  179. 177.
  180. 178. Console.WriteLine()
  181. 179. Console.WriteLine("This worksheet's cells feed URL is:")
  182. 180. Console.WriteLine(cellFeedLink.HRef)
  183. 181.
  184. 182. Dim query As New CellQuery(cellFeedLink.HRef.ToString())
  185. 183. Dim feed As CellFeed = service.Query(query)
  186. 184.
  187. 185. Console.WriteLine()
  188. 186. Console.WriteLine("Cells in this worksheet:")
  189. 187. For Each curCell As CellEntry In feed.Entries
  190. 188. Console.WriteLine("Row {0}, column {1}: {2}", curCell.Cell.Row, curCell.Cell.Column, curCell.Cell.Value)
  191. 189. Next
  192. 190. End Sub
  193. 191.
  194. 192. ''' <summary>
  195. 193. ''' Performs a cell range query on the specified worksheet to
  196. 194. ''' retrieve only the cells in the first column.
  197. 195. ''' </summary>
  198. 196. ''' <param name="service">an authenticated SpreadsheetsService object</param>
  199. 197. ''' <param name="entry">the worksheet to retrieve</param>
  200. 198. Private Shared Sub CellRangeQuery(ByVal service As SpreadsheetsService, ByVal entry As WorksheetEntry)
  201. 199. Dim cellFeedLink As AtomLink = entry.Links.FindService(GDataSpreadsheetsNameTable.CellRel, Nothing)
  202. 200.
  203. 201. Dim query As New CellQuery(cellFeedLink.HRef.ToString())
  204. 202. query.MinimumColumn = 1
  205. 203. query.MaximumColumn = 1
  206. 204. query.MinimumRow = 2
  207. 205.
  208. 206. Dim feed As CellFeed = service.Query(query)
  209. 207. Console.WriteLine()
  210. 208. Console.WriteLine("Cells in column 1:")
  211. 209. For Each curCell As CellEntry In feed.Entries
  212. 210. Console.WriteLine("Row {0}: {1}", curCell.Cell.Row, curCell.Cell.Value)
  213. 211. Next
  214. 212. End Sub
  215. 213.
  216. 214. ''' <summary>
  217. 215. ''' Updates a single cell in the specified worksheet.
  218. 216. ''' </summary>
  219. 217. ''' <param name="service">an authenticated SpreadsheetsService object</param>
  220. 218. ''' <param name="entry">the worksheet to update</param>
  221. 219. Private Shared Sub UpdateCell(ByVal service As SpreadsheetsService, ByVal entry As WorksheetEntry)
  222. 220. Dim cellFeedLink As AtomLink = entry.Links.FindService(GDataSpreadsheetsNameTable.CellRel, Nothing)
  223. 221. Dim query As New CellQuery(cellFeedLink.HRef.ToString())
  224. 222. Console.WriteLine()
  225. 223.
  226. 224. Console.Write("Row of cell to update? ")
  227. 225. Dim row As String = Console.ReadLine()
  228. 226.
  229. 227. Console.Write("Column of cell to update? ")
  230. 228. Dim column As String = Console.ReadLine()
  231. 229.
  232. 230. query.MinimumRow = InlineAssignHelper(query.MaximumRow, UInteger.Parse(row))
  233. 231. query.MinimumColumn = InlineAssignHelper(query.MaximumColumn, UInteger.Parse(column))
  234. 232.
  235. 233. Dim feed As CellFeed = service.Query(query)
  236. 234. Dim cell As CellEntry = TryCast(feed.Entries(0), CellEntry)
  237. 235.
  238. 236. Console.WriteLine()
  239. 237. Console.WriteLine("Current cell value: {0}", cell.Cell.Value)
  240. 238. Console.Write("Enter a new value: ")
  241. 239. Dim newValue As String = Console.ReadLine()
  242. 240.
  243. 241. cell.Cell.InputValue = newValue
  244. 242. Dim updatedCell As AtomEntry = cell.Update()
  245. 243.
  246. 244. Console.WriteLine("Successfully updated cell: {0}", updatedCell.Content.Content)
  247. 245. End Sub
  248. 246.
  249. 247. ''' <summary>
  250. 248. ''' Creates a new SpreadsheetsService with the user's specified
  251. 249. ''' authentication credentials and runs all of the Spreadsheets
  252. 250. ''' operations above.
  253. 251. ''' </summary>
  254. 252. Private Shared Sub RunSample()
  255. 253. Dim service As New SpreadsheetsService("exampleCo-exampleApp-1")
  256. 254. service.setUserCredentials(userName, password)
  257. 255.
  258. 256. ' Demonstrate printing all spreadsheets and worksheets.
  259. 257. PrintAllSpreadsheetsAndWorksheets(service)
  260. 258.
  261. 259. ' Demonstrate retrieving the list feed for a single worksheet,
  262. 260. ' with the rows (ordered by position) reversed.
  263. 261. Dim userChoice As Integer = GetUserWorksheetChoice()
  264. 262. Dim entry As WorksheetEntry = TryCast(allWorksheets(userChoice), WorksheetEntry)
  265. 263.
  266. 264. RetrieveListFeed(service, entry, True)
  267. 265.
  268. 266. ' Demonstrate sending a structured query.
  269. 267. Console.Write("Enter a structured query to execute: ")
  270. 268. Dim queryText As String = Console.ReadLine()
  271. 269. StructuredQuery(service, entry, queryText)
  272. 270.
  273. 271. ' Demonstrate inserting a new row in the worksheet.
  274. 272. Dim insertedEntry As ListEntry = InsertRow(service, entry)
  275. 273.
  276. 274. ' Demonstrate updating the inserted row.
  277. 275. UpdateRow(service, insertedEntry)
  278. 276.
  279. 277. ' Demonstrate deleting the entry.
  280. 278. insertedEntry.Delete()
  281. 279.
  282. 280. ' Demonstrate retrieving a cell feed for a worksheet.
  283. 281. RetrieveCellFeed(service, entry)
  284. 282.
  285. 283. ' Demonstrate a cell range query.
  286. 284. CellRangeQuery(service, entry)
  287. 285.
  288. 286. ' Demonstrate updating a single cell.
  289. 287. UpdateCell(service, entry)
  290. 288. End Sub
  291. 289.
  292. 290. ''' <summary>
  293. 291. ''' Prompts the user for a number representing one of their
  294. 292. ''' worksheets; this worksheet will then be used to demonstrate
  295. 293. ''' the various Spreadsheets operations above.
  296. 294. ''' </summary>
  297. 295. ''' <returns>the number of the worksheet chosen by the user</returns>
  298. 296. Private Shared Function GetUserWorksheetChoice() As Integer
  299. 297. Console.WriteLine("Select the worksheet on which to demonstrate")
  300. 298. Console.WriteLine("add/edit/delete operations by entering its number:")
  301. 299. Console.WriteLine()
  302. 300. For i As Integer = 0 To allWorksheets.Count - 1
  303. 301. Dim entry As WorksheetEntry = TryCast(allWorksheets(i), WorksheetEntry)
  304. 302. Console.WriteLine("{0}: {1}", i + 1, entry.Title.Text)
  305. 303. Next
  306. 304.
  307. 305. Console.WriteLine()
  308. 306. Console.Write("Your choice: ")
  309. 307. Dim userResponse As [String] = Console.ReadLine()
  310. 308.
  311. 309. Return Integer.Parse(userResponse) - 1
  312. 310. End Function
  313. 311.
  314. 312. ''' <summary>
  315. 313. ''' Program entry point.
  316. 314. ''' </summary>
  317. 315. ''' <param name="args">the username and password used to log
  318. 316. ''' in to Google Docs & Spreadsheets. For example:
  319. 317. '''
  320. 318. ''' SpreadsheetDemo jdoe@gmail.com mypassword
  321. 319. ''' </param>
  322. 320. Private Shared Sub Main(ByVal args As String())
  323. 321. If args.Length < 2 Then
  324. 322. Console.[Error].WriteLine("Syntax: SpreadsheetDemo <username> <password>")
  325. 323. Exit Sub
  326. 324. Else
  327. 325. userName = args(0)
  328. 326. password = args(1)
  329. 327.
  330. 328. RunSample()
  331. 329. End If
  332. 330. End Sub
  333. 331. Private Shared Function InlineAssignHelper(Of T)(ByRef target As T, ByVal value As T) As T
  334. 332. target = value
  335. 333. Return value
  336. 334. End Function
  337. 335. End Class
  338. 336. End Namespace
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement