/** * 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); }
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); }
/** * Uses the user's credentials to get a list of spreadsheets. Then asks the user which spreadsheet * to load. If the selected spreadsheet has multiple worksheets then the user will also be * prompted to select what sheet to use. * * @param reader to read input from the keyboard * @throws ServiceException when the request causes an error in the Google Spreadsheets service. * @throws IOException when an error occurs in communication with the Google Spreadsheets service. */ public void loadSheet(BufferedReader reader) throws IOException, ServiceException { // Get the spreadsheet to load SpreadsheetFeed feed = service.getFeed(factory.getSpreadsheetsFeedUrl(), SpreadsheetFeed.class); List spreadsheets = feed.getEntries(); int spreadsheetIndex = getIndexFromUser(reader, spreadsheets, "spreadsheet"); SpreadsheetEntry spreadsheet = feed.getEntries().get(spreadsheetIndex); // Get the worksheet to load if (spreadsheet.getWorksheets().size() == 1) { cellFeedUrl = spreadsheet.getWorksheets().get(0).getCellFeedUrl(); } else { List worksheets = spreadsheet.getWorksheets(); int worksheetIndex = getIndexFromUser(reader, worksheets, "worksheet"); WorksheetEntry worksheet = (WorksheetEntry) worksheets.get(worksheetIndex); cellFeedUrl = worksheet.getCellFeedUrl(); } System.out.println("Sheet loaded."); }
/** * Constructs a cell demo from the specified spreadsheet service, which is used to authenticate to * and access Google Spreadsheets. * * @param service the connection to the Google Spradsheets service. * @param outputStream a handle for stdout. */ public CellDemo(SpreadsheetService service, PrintStream outputStream) { this.service = service; this.out = outputStream; this.factory = FeedURLFactory.getDefault(); }