public static void main(String[] args) { SpreadsheetService service = new SpreadsheetService("Sheet1"); try { String sheetUrl = "https://spreadsheets.google.com/feeds/list/1BuEN5wHRrQJFgTj9bXN6lXEAcb1lcJ-QYg4Lya-3lCo/default/public/values"; URL url = new URL(sheetUrl); // Get Feed of Spreadsheet url ListFeed lf = service.getFeed(url, ListFeed.class); // Iterate over feed to get cell value for (ListEntry le : lf.getEntries()) { CustomElementCollection cec = le.getCustomElements(); // Pass column name to access it's cell values String val = cec.getValue("CustomerName"); System.out.println(val); String val2 = cec.getValue("Customeremailid"); System.out.println(val2); } } catch (IOException e) { e.printStackTrace(); } catch (ServiceException e) { e.printStackTrace(); } }
/** * Writes the content of a map into the Google spreadsheet associated with this object. * * @param data a set of key value pairs, whose keys have to correspond with the headers of the * spreadsheet. * @throws ServiceException * @throws IOException */ public void addRow(Map<String, Object> data) throws ServiceException, IOException { ListEntry rowToAdd = new ListEntry(); CustomElementCollection keyValuePairs = rowToAdd.getCustomElements(); // copy all values from the data map into the row for (String key : data.keySet()) { keyValuePairs.setValueLocal(key, data.get(key).toString()); } // write the data into Google spreadsheet URL listFeedUrl = getWorksheet().getListFeedUrl(); getService().insert(listFeedUrl, rowToAdd); }
@BeforeMethod public void setUp() throws IOException, ServiceException { spreadsheetService = mock(SpreadsheetService.class); listFeed = mock(ListFeed.class); TextConstruct textConstruct = when(mock(TextConstruct.class).getPlainText()).thenReturn("name").getMock(); when(listFeed.getTitle()).thenReturn(textConstruct); List<ListEntry> entries = new ArrayList<ListEntry>(); ListEntry entry = mock(ListEntry.class); CustomElementCollection customElements = mock(CustomElementCollection.class); when(customElements.getTags()) .thenReturn(new LinkedHashSet<String>(Arrays.asList("col1", "col2", "col3"))); when(customElements.getValue("col1")).thenReturn("val1"); when(customElements.getValue("col2")).thenReturn("val2"); when(customElements.getValue("col3")).thenReturn("val3"); when(entry.getCustomElements()).thenReturn(customElements); entries.add(entry); when(listFeed.getEntries()).thenReturn(entries); cellFeed = mock(CellFeed.class); List<CellEntry> cells = new ArrayList<CellEntry>(); Cell cell1 = mock(Cell.class); when(cell1.getRow()).thenReturn(1); when(cell1.getValue()).thenReturn("col1"); Cell cell2 = mock(Cell.class); when(cell2.getRow()).thenReturn(1); when(cell2.getValue()).thenReturn("col2"); Cell cell3 = mock(Cell.class); when(cell3.getRow()).thenReturn(1); when(cell3.getValue()).thenReturn("col3"); CellEntry entry1 = when(mock(CellEntry.class).getCell()).thenReturn(cell1).getMock(); CellEntry entry2 = when(mock(CellEntry.class).getCell()).thenReturn(cell2).getMock(); CellEntry entry3 = when(mock(CellEntry.class).getCell()).thenReturn(cell3).getMock(); cells.add(entry1); cells.add(entry2); cells.add(entry3); when(cellFeed.getEntries()).thenReturn(cells); when(cellFeed.getTitle()).thenReturn(textConstruct); spreadsheetRepository = new GoogleSpreadsheetRepository(spreadsheetService, "key", "id"); }