/** * Gets the {@link Worksheet} object from a {@link Spreadsheet} specified by a String in A1 * Notion. * * @param a1NotionPart * @param spreadsheet * @return */ private static Worksheet getWorksheet(String a1NotionPart, Spreadsheet spreadsheet) { Worksheet sheet = null; // If the first letter ist a letter, treat part as worksheet name, else // as index if (Character.isLetter(a1NotionPart.charAt(0))) { sheet = spreadsheet.getWorksheetFor(a1NotionPart); } else if (Character.isDigit(a1NotionPart.charAt(0))) { int sheetIndex = 1; try { sheetIndex = Integer.parseInt(a1NotionPart); } catch (NumberFormatException nfe) { nfe.printStackTrace(); } sheet = spreadsheet.getWorksheetAt(sheetIndex); } return sheet; }
/** * Gets the {@link IElement} instance which is described by the target string. * * <p>If the target string doesn't specify a worksheet the first one is taken. * * @param target * @param spreadsheet * @return * @throws ParseException */ public static IElement retrieveTarget(String target, Spreadsheet spreadsheet) throws ParseException { IElement element = null; String[] targetParts = target.split("\\."); AbstractAddress address = null; Worksheet sheet = null; // TODO resolve Range address switch (targetParts.length) { case 1: address = createCellAddress(targetParts[0]); if (spreadsheet == null) { throw new RuntimeException("Spreadsheet is null."); } if (spreadsheet.getWorksheets() == null) { throw new RuntimeException( "Spreadsheet " + spreadsheet.getName() + " has no worksheets."); } if (spreadsheet.getWorksheets().size() > 0) { address.setWorksheet(spreadsheet.getWorksheetAt(1)); } else { throw new RuntimeException("Spreadsheet has no worksheets."); } break; case 2: address = createCellAddress(targetParts[1]); sheet = getWorksheet(targetParts[0], spreadsheet); address.setWorksheet(sheet); break; case 3: throw new ParseException("References to other Spreadsheets are not supported", 0); // address = createCellAddress(targetParts[2]); // sheet = getWorksheet(targetParts[1], spreadsheet); // address.setWorksheet(sheet); default: break; } element = spreadsheet.getCellFor((CellAddress) address); return element; }