Overview of Google spreadsheet API for Java by Nazar Kostiv

7,268 views

Published on

Published in: Technology, Business
0 Comments
3 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
7,268
On SlideShare
0
From Embeds
0
Number of Embeds
148
Actions
Shares
0
Downloads
71
Comments
0
Likes
3
Embeds 0
No embeds

No notes for slide

Overview of Google spreadsheet API for Java by Nazar Kostiv

  1. 1. Overview of GoogleSpreadsheet API for Java Nazarii Kostiv
  2. 2. Agenda● Overview● Authorization;● Work with worksheet;● List-based feeds;● Cell-based feeds;● Questions.
  3. 3. What we need to start● jdk 1.5 or greater;● gdata-java-client jars;● java mail API 1.4 or greater;● javaBeans Activation Framework.
  4. 4. Overview Creating spreadsheet Set spreadsheet Authorizing service URL and name Get row or cell Get spreadsheet Get worksheet feed
  5. 5. AuthorizationSpreadsheetService service = new SpreadsheetService("service name"); - create spreadsheetserviceservice.setUserCredentials("username", "password"); - send usercredentials
  6. 6. Getting spreadsheet feedString SPREADSHEET_URL = "https://spreadsheets.google.com/feeds/spreadsheets/private/full";URL sheetListUrl = new URL(SPREADSHEET_URL);- set spreadsheer URL;SpreadsheetQuery query = new SpreadsheetQuery(sheetListUrl);query.setTitleQuery("Spreadsheet name"); - set spreadsheet name;SpreadsheetFeed feed = service.getFeed(query, SpreadsheetFeed.class); - get spreadsheetfeed with entries.
  7. 7. WorksheetList<SpreadsheetEntry> spreadsheetEntries = feed.getEntries();SpreadsheetEntry spreadsheet = spreadsheetEntries.get(0); - getspreadsheetList<WorksheetEntry> worksheets = spreadsheetEntry.getWorksheets();WorksheetEntry worksheet = worksheets.get(0); - get worksheet;
  8. 8. Worksheet infoList<WorksheetEntry> worksheets = SpreadsheetDemoUtil.getWorksheets(); for (WorksheetEntry worksheet : worksheets) { System.out.println("Worksheet name: " + worksheet.getTitle().getPlainText() + ", Cols: " + worksheet.getColCount() + ", Rows: " + worksheet.getRowCount()); }Worksheet name: Worksheet 1, Cols: 20, Rows: 93Worksheet name: Worksheet 2, Cols: 20, Rows: 50Worksheet name: New worksheet, Cols: 20, Rows: 100
  9. 9. Working with worksheetWorksheetEntry worksheet = new WorksheetEntry();URL worksheetFeedUrl = spreadsheetEntry.getWorksheetFeedUrl();insertedWorksheet = service.insert( worksheetFeedUrl, worksheet); - add new worksheet;worksheetETag = insertedWorksheet.getEtag();worksheet.setColCount(30);worksheet.update(); - update worksheet;
  10. 10. Working with worksheet title = worksheet.getTitle().getPlainText(); eTag = worksheet.getEtag(); if (title.equals("New worksheet") && eTag.equals(worksheetETag)) { try { worksheet.delete(); } catch .... - delete worksheet;
  11. 11. List-based feedsURL listFeedUrl = worksheetEntry.getListFeedUrl();ListFeed feed = service.getFeed(listFeedUrl, ListFeed.class); - get list-based feedwith list of rowsfor (ListEntry entry : feed.getEntries()) { System.out.println(entry.getTitle().getPlainText());}
  12. 12. List based workingURL listFeedUrl = new URI(null, null, worksheet.getListFeedUrl().toString(), "sq=width>25 and height<175", null).toURL();listFeed = service.getFeed(listFeedUrl, ListFeed.class);for (ListEntry row : listFeed.getEntries()) { System.out.println(row.get...);}
  13. 13. Add rowListFeed listFeed = service.getFeed(listFeedUrl, ListFeed.class);ListEntry row = new ListEntry(); row.getCustomElements().setValueLocal("Component", "Newcomponent"); row.getCustomElements().setValueLocal("width", "25"); row.getCustomElements().setValueLocal("height", "19");row = service.insert(listFeedUrl, row); - insert new row
  14. 14. Delete rowListFeed listFeed = service.getFeed(listFeedUrl, ListFeed.class);for (ListEntry row : listFeed.getEntries()) { if (...) { row.delete(); }
  15. 15. Cell-based feedsURL cellFeedUrl = worksheetEntry.getCellFeedUrl();CellFeed feed = service.getFeed(celFeedUrl, CellFeed.class); - get cell-based feedwith list of cellsfor (CellEntry cell : feed.getEntries()) { System.out.println(entry.getTitle().getPlainText());}
  16. 16. Cell based workingURL cellFeedUrl = new URI(null, null, worksheet.getCellFeedUrl().toString(), "?min-row=2&min-col=4&max-col=4",null).toURL();CellFeed cellFeed = service.getFeed(cellFeedUrl, CellFeed.class);for (CellEntry cell : cellFeed.getEntries()) { System.out.println(cell.getTitle().getPlainText();}
  17. 17. Cell based workingURL cellFeedUrl = mainWorksheet.getCellFeedUrl(); CellQuery cellQuery = new CellQuery(cellFeedUrl); cellQuery.setMinimumCol(2); cellQuery.setMaximumCol(4); cellQuery.setMinimumRow(4); cellQuery.setMaximumRow(4); CellFeed cellFeed = null; try { cellFeed = service.query(cellQuery, CellFeed.class); } catch(...)
  18. 18. Cell data manipulationif (cell.getTitle().getPlainText().equals("A1")){ cell.changeInputValueLocal("123"); - set value to cell cell.update();}..... cell.changeInputValueLocal("=SUM(A1:B1)"); - set cell formula
  19. 19. Questions?
  20. 20. Links● https://developers.google.com/google-apps/spreadsheets/● https://developers.google.com/google-apps/documents-list/● https://developers.google.com/apps-script/guide
  21. 21. Nazarii KostivEmail: nazar.kostiv@gmail.com

×