📄 listdemo.java
字号:
SpreadsheetEntry spreadsheet = (SpreadsheetEntry) spreadsheets .get(spreadsheetIndex); // Get the worksheet to load if (spreadsheet.getWorksheets().size() == 1) { listFeedUrl = spreadsheet.getWorksheets().get(0).getListFeedUrl(); } else { List worksheets = spreadsheet.getWorksheets(); int worksheetIndex = getIndexFromUser(reader, worksheets, "worksheet"); WorksheetEntry worksheet = (WorksheetEntry) worksheets .get(worksheetIndex); listFeedUrl = worksheet.getListFeedUrl(); } System.out.println("Sheet loaded."); } /** * Lists all rows in the spreadsheet. * * @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 listAllEntries() throws IOException, ServiceException { ListFeed feed = service.getFeed(listFeedUrl, ListFeed.class); for (ListEntry entry : feed.getEntries()) { printAndCacheEntry(entry); } } /** * Lists all rows in the spreadsheet in reverse order. * * @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 reverseAllEntries() throws IOException, ServiceException { ListQuery query = new ListQuery(listFeedUrl); query.setReverse(true); ListFeed feed = service.query(query, ListFeed.class); for (ListEntry entry : feed.getEntries()) { printAndCacheEntry(entry); } } /** * Searches rows with a full text search string, finding any rows that match * all the given words. * * @param fullTextSearchString a string like "Rosa 555" will look for the * substrings Rosa and 555 to appear anywhere in the row * @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 search(String fullTextSearchString) throws IOException, ServiceException { ListQuery query = new ListQuery(listFeedUrl); query.setFullTextQuery(fullTextSearchString); ListFeed feed = service.query(query, ListFeed.class); out.println("Results for [" + fullTextSearchString + "]"); for (ListEntry entry : feed.getEntries()) { printAndCacheEntry(entry); } } /** * Performs a full database-like query on the rows. * * @param structuredQuery a query like: name = "Bob" and phone != "555-1212" * @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 query(String structuredQuery) throws IOException, ServiceException { ListQuery query = new ListQuery(listFeedUrl); query.setSpreadsheetQuery(structuredQuery); ListFeed feed = service.query(query, ListFeed.class); out.println("Results for [" + structuredQuery + "]"); for (ListEntry entry : feed.getEntries()) { printAndCacheEntry(entry); } } /** * Deletes an entry by the ID. * * This looks up the old cached row so that the version ID is known. A version * ID is used by GData to avoid edit collisions, so that you know if someone * has changed the row before you delete it. * * For this reason, the cached version of the row, as you last saw it, is * kept, instead of querying the entry anew. * * @param idToDelete the ID of the row to delete such as "cph6n" * @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 delete(String idToDelete) throws IOException, ServiceException { ListEntry entry = entriesCached.get(idToDelete); // Find the entry to // delete if (entry != null) { entry.delete(); // This deletes the existing entry. out.println("Deleted!"); } else { out.println("I don't know that ID."); out.println("In GData, you must get an entry before deleting it,"); out.println("so that you have the version ID."); out.println("You might have to 'list' first."); } } /** * Updates an existing entry. * * See the comment in {@code delete} for why the entry is cached in a hash * map. * * @param id the ID of the row to update * @param nameValuePairs the name value pairs, such as "name=Rosa" to change * the row's name field to Rosa * @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 update(String id, String nameValuePairs) throws IOException, ServiceException { // The next line of code finds the entry to update. // See the javadoc on entriesCached. ListEntry entry = entriesCached.get(id); setEntryContentsFromString(entry, nameValuePairs); if (entry != null) { entry.update(); // This updates the existing entry. out.println("Updated!"); } else { out.println("I don't know that ID."); out.println("In GData, you must get an entry before deleting it."); out.println("You might have to 'list' first."); } } /** * Parses and executes a command. * * @param reader to read input from the keyboard * @return false if the user quits, true on exception */ public boolean executeCommand(BufferedReader reader) { for (String s : COMMAND_HELP_MESSAGE) { out.println(s); } System.err.print("Command: "); try { String command = reader.readLine(); String[] parts = command.trim().split(" ", 2); String name = parts[0]; String parameters = parts.length > 1 ? parts[1] : ""; if (name.equals("add")) { addNewEntry(parameters); } else if (name.equals("load")) { loadSheet(reader); } else if (name.equals("list")) { listAllEntries(); } else if (name.equals("reverse")) { reverseAllEntries(); } else if (name.equals("search")) { search(parameters); } else if (name.equals("query")) { query(parameters); } else if (name.equals("delete")) { delete(parameters); } else if (name.equals("update")) { String[] split = parameters.split(" ", 2); update(split[0], split[1]); } else if (name.startsWith("q") || name.startsWith("exit")) { return false; } else { out.println("Unknown command."); } } catch (ServiceException se) { // Show *exactly* what went wrong. se.printStackTrace(); } catch (IOException ioe) { // Show *exactly* what went wrong. ioe.printStackTrace(); } return true; } /** * Starts up the demo and prompts for commands. * * @param username name of user to authenticate (e.g. yourname@gmail.com) * @param password password to use for authentication * @throws AuthenticationException if the service is unable to validate the * username and password. */ public void run(String username, String password) throws AuthenticationException { for (String s : WELCOME_MESSAGE) { out.println(s); } BufferedReader reader = new BufferedReader( new InputStreamReader(System.in)); // Login and prompt the user to pick a sheet to use. login(username, password); try { loadSheet(reader); } catch (Exception e) { e.printStackTrace(); } while (executeCommand(reader)) { } } /** * Runs the demo. * * @param args the command-line arguments * @throws AuthenticationException if the service is unable to validate the * username and password. */ public static void main(String[] args) throws AuthenticationException { SimpleCommandLineParser parser = new SimpleCommandLineParser(args); String username = parser.getValue("username", "user", "u"); String password = parser.getValue("password", "pass", "p"); boolean help = parser.containsKey("help", "h"); if (help || username == null || password == null) { usage(); System.exit(1); } ListDemo demo = new ListDemo(new SpreadsheetService("List Demo"), System.out); demo.run(username, password); } /** * Prints out the usage. */ private static void usage() { for (String s : USAGE_MESSAGE) { System.out.println(s); } for (String s : WELCOME_MESSAGE) { System.out.println(s); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -