public ImportClient(String username, String password, int itemsPerBatch, String spreadsheetName) throws Exception { ITEMS_PER_BATCH = itemsPerBatch; factory = FeedURLFactory.getDefault(); service = new SpreadsheetService("Props-2-Xls"); service.setUserCredentials(username, password); service.setProtocolVersion( SpreadsheetService.Versions .V1); // bug workaround! http://code.google.com/p/gdata-java-client/issues/detail?id=103 spreadsheet = getSpreadsheet(spreadsheetName); backingEntry = spreadsheet.getDefaultWorksheet(); CellQuery cellQuery = new CellQuery(backingEntry.getCellFeedUrl()); cellQuery.setReturnEmpty(true); CellFeed cellFeed = service.getFeed(cellQuery, CellFeed.class); }
/** * Gets the SpreadsheetEntry for the first spreadsheet with that name retrieved in the feed. * * @param spreadsheet the name of the spreadsheet * @return the first SpreadsheetEntry in the returned feed, so latest spreadsheet with the * specified name * @throws Exception if error is encountered, such as no spreadsheets with the name */ public SpreadsheetEntry getSpreadsheet(String spreadsheet) throws Exception { SpreadsheetQuery spreadsheetQuery = new SpreadsheetQuery(factory.getSpreadsheetsFeedUrl()); spreadsheetQuery.setTitleQuery(spreadsheet); SpreadsheetFeed spreadsheetFeed = service.query(spreadsheetQuery, SpreadsheetFeed.class); List<SpreadsheetEntry> spreadsheets = spreadsheetFeed.getEntries(); if (spreadsheets.isEmpty()) { throw new Exception("No spreadsheets with that name"); } return spreadsheets.get(0); }
/** * Get the WorksheetEntry for the worksheet in the spreadsheet with the specified name. * * @param spreadsheetName the name of the spreadsheet * @param worksheetName the name of the worksheet in the spreadsheet * @return worksheet with the specified name in the spreadsheet with the specified name * @throws Exception if error is encountered, such as no spreadsheets with the name, or no * worksheet wiht the name in the spreadsheet */ public Worksheet getWorksheet(String spreadsheetName, String worksheetName) throws Exception { SpreadsheetEntry spreadsheetEntry = getSpreadsheet(spreadsheetName); WorksheetQuery worksheetQuery = new WorksheetQuery(spreadsheetEntry.getWorksheetFeedUrl()); worksheetQuery.setTitleQuery(worksheetName); WorksheetFeed worksheetFeed = service.query(worksheetQuery, WorksheetFeed.class); List<WorksheetEntry> worksheets = worksheetFeed.getEntries(); if (worksheets.isEmpty()) { throw new Exception( "No worksheets with that name in spreadhsheet " + spreadsheetEntry.getTitle().getPlainText()); } return new Worksheet(worksheets.get(0), service); }
/** Presents the given cell feed as a map from row, column pair to CellEntry. */ private void refreshCachedData() throws IOException, ServiceException { CellQuery cellQuery = new CellQuery(backingEntry.getCellFeedUrl()); cellQuery.setReturnEmpty(true); this.cellFeed = spreadsheetService.getFeed(cellQuery, CellFeed.class); // A subtlety: Spreadsheets row,col numbers are 1-based whereas the // cellEntries array is 0-based. Rather than wasting an extra row and // column worth of cells in memory, we adjust accesses by subtracting // 1 from each row or column number. cellEntries = new CellEntry[rows][columns]; for (CellEntry cellEntry : cellFeed.getEntries()) { Cell cell = cellEntry.getCell(); cellEntries[cell.getRow() - 1][cell.getCol() - 1] = cellEntry; } }