private Map<Integer, PoiProxyName> makeNames(Workbook wb, EvaluationWorkbook ewb) { Map<Integer, PoiProxyName> names = new HashMap<>(); for (int nIdx = 0; nIdx < wb.getNumberOfNames(); nIdx++) { Name wbName = wb.getNameAt(nIdx); Ptg[] ptgs; String refersToFormula; if (!wbName.isFunctionName() && wbName.getRefersToFormula() != null) { //NOPMD refersToFormula = wbName.getRefersToFormula(); ptgs = FormulaParser.parse(refersToFormula, (FormulaParsingWorkbook) ewb, FormulaType.NAMEDRANGE, 0 /*TODO: sheet index*/); } else { ptgs = null; refersToFormula = null; } names.put(nIdx, new PoiProxyName(wbName.getNameName(), wbName.isFunctionName(), refersToFormula != null, ptgs, wbName.isFunctionName(), nIdx)); } return names; }
protected void createName(HandlerState state, String bookmark, int row1, int col1, int row2, int col2 ) { CellReference crFirst = new CellReference( state.currentSheet.getSheetName(), row1, col1, true, true ); CellReference crLast = new CellReference( row2, col2, true, true ); String formula = crFirst.formatAsString() + ":" + crLast.formatAsString(); Name name = state.currentSheet.getWorkbook().getName(bookmark); if( name == null ) { name = state.currentSheet.getWorkbook().createName(); name.setNameName( bookmark ); name.setRefersToFormula( formula ); } else { String existingFormula = name.getRefersToFormula(); try { name.setRefersToFormula(existingFormula + "," + formula); } catch( FormulaParseException ex ) { log.warn( 0, "Unable to add \"" + formula + "\" to name (\"" + bookmark + "\") with existing formula: " + existingFormula, ex ); } } }
private void validateNamedRange( Workbook workbook, int index, String name, int sheetIndex, int row1, int col1, int row2, int col2 ) { Name namedRange = workbook.getNameAt(index); assertEquals( name,namedRange.getNameName() ); assertEquals( sheetIndex, namedRange.getSheetIndex() ); AreaReference ref = new AreaReference( namedRange.getRefersToFormula() ); if( ( row1 == row2 ) && ( col1 == col2 ) ) { assertTrue( ref.isSingleCell() ); assertEquals( row1, ref.getFirstCell().getRow() ); assertEquals( col1, ref.getFirstCell().getCol() ); } else { assertTrue( AreaReference.isContiguous( namedRange.getRefersToFormula() ) ); assertEquals( row1, Math.min( ref.getFirstCell().getRow(), ref.getLastCell().getRow() ) ); assertEquals( col1, Math.min( ref.getFirstCell().getCol(), ref.getLastCell().getCol() ) ); assertEquals( row2, Math.max( ref.getFirstCell().getRow(), ref.getLastCell().getRow() ) ); assertEquals( col2, Math.max( ref.getFirstCell().getCol(), ref.getLastCell().getCol() ) ); } }
/** * 指定した範囲の名前を登録する。 * <p>POI-3.7以上が必要。 * <p>指定した名前が既に存在する場合は、新しい範囲に書き換える。 * @param sheet シート * @param name 名前 * @param startPosition 設定するセルの開始位置 * @param endPosition 設定するセルの終了位置 * @return */ public static Name defineName(final Sheet sheet, final String name, final Point startPosition, final Point endPosition) { ArgUtils.notNull(sheet, "sheet"); ArgUtils.notEmpty(name, "name"); ArgUtils.notNull(startPosition, "startPosition"); ArgUtils.notNull(endPosition, "endPosition"); final Workbook workbook = sheet.getWorkbook(); Name nameObj = workbook.getName(name); if(nameObj == null) { nameObj = workbook.createName(); nameObj.setNameName(name); } final AreaReference areaRef = buildNameArea(sheet.getSheetName(), startPosition, endPosition); nameObj.setRefersToFormula(areaRef.formatAsString()); return nameObj; }
@Override public void run() { synchronized(lock) { try { names = new ArrayList<String>(); Workbook wb = WorkbookFactory.create(file); for(Name name : ExcelUtil.getReferenceNames(wb)) names.add(name.getNameName()); } catch (Exception ex2) { this.ex = ex2; } } }
public static Map<String, String> getWorkbookNames(Workbook workbook) { Map<String, String> result = new HashMap<>(workbook.getNumberOfNames()); for (int i = 0 ; i < workbook.getNumberOfNames() ; i++) { Name name = workbook.getNameAt(i); result.put(name.getNameName(), name.getRefersToFormula()); } return result; }
public boolean isOverlapped(Name name) { String ref = name.getRefersToFormula(); int idx = ref.indexOf('!'); if (idx != -1) { ref = ref.substring(idx + 1); } AreaReference area = new AreaReference(ref); CellReference topLeft = area.getFirstCell(); CellReference bottomRight = area.getLastCell(); CellRangeAddress cra = new CellRangeAddress( topLeft.getRow(), bottomRight.getRow(), topLeft.getCol(), bottomRight.getCol() ); return isOverlapped(cra); }
@Override public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) { super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus); if(value instanceof Name) setText(((Name)value).getNameName()); return this; }
public static CellReference getCellReference(Workbook wb, String ref) { Name name = wb.getName(ref); if(name != null) ref = name.getRefersToFormula(); if(ref.indexOf(':') > 0) { AreaReference aRef = new AreaReference(ref); return aRef.getFirstCell(); } else { return new CellReference(ref); } }
public static List<Name> getReferenceNames(Workbook wb) { int size = wb.getNumberOfNames(); List<Name> result = new ArrayList<Name>(size); for(int i = 0; i < size; i++) { Name name = wb.getNameAt(i); if(isReferenceName(wb, name)) result.add(name); } return result; }
/** * For given {@link Workbook} does convert everything to new {@link DataModel} structure. * Does copy all supported fields (for supported fields see {@link DataModel} class. */ static IDataModel toDataModel(final Workbook workbook) { if (workbook == null) { return null; } //add custom functions information workbook.addToolPack(getUdfFinder()); Sheet s = workbook.getSheetAt(0); //TODO: only one sheet is supported if (s == null) { return null; } IDataModel dm = new DataModel(s.getSheetName()); for (int i = s.getFirstRowNum(); i <= s.getLastRowNum(); i++) { Row r = s.getRow(i); if (r == null) { continue; } DmRow row = new DmRow(i); dm.setRow(i, row); for (int j = r.getFirstCellNum(); j < r.getLastCellNum(); j++) { Cell c = r.getCell(j); if (c == null) { continue; } DmCell cell = new DmCell(); row.setCell(j, cell); cell.setAddress(new CellAddress(dm.getDataModelId(), A1Address.fromRowColumn(i, j))); cell.setContent(ConverterUtils.resolveCellValue(c)); } } EvaluationWorkbook evaluationWbook = ConverterUtils.newEvaluationWorkbook(workbook); for (int nIdx = 0; nIdx < workbook.getNumberOfNames(); nIdx++) { Name name = workbook.getNameAt(nIdx); String reference = name.getRefersToFormula(); if (reference == null) { continue; } if (A1Address.isAddress(removeSheetFromNameRef(reference))) { dm.setNamedAddress(name.getNameName(), A1Address.fromA1Address(removeSheetFromNameRef(reference))); } else if (isFormula(reference, evaluationWbook)) { dm.setNamedValue(name.getNameName(), new CellValue(FORMULA_PREFIX + reference)); } else { dm.setNamedValue(name.getNameName(), CellValue.from(reference)); } } return dm; }
/** * Not supported */ @Override public Name getName(String name) { throw new UnsupportedOperationException(); }
/** * Not supported */ @Override public List<? extends Name> getNames(String s) { throw new UnsupportedOperationException(); }
/** * Not supported */ @Override public List<? extends Name> getAllNames() { throw new UnsupportedOperationException(); }
/** * Not supported */ @Override public Name getNameAt(int nameIndex) { throw new UnsupportedOperationException(); }
/** * Not supported */ @Override public Name createName() { throw new UnsupportedOperationException(); }
/** * Not supported */ @Override public void removeName(Name name) { throw new UnsupportedOperationException(); }
public TempName(Name name, AreaReference area, int mrCount) { this.name = name; this.area = area; this.relatedMergedRegion = new BitSet(mrCount); }
public NamedCellInfo(Name name, List<CellReference> list) { this.name = name; this.list = list; }
public static boolean isReferenceName(Workbook wb, Name name) { return !name.isFunctionName() && !name.isDeleted() && wb.getSheetIndex(name.getNameName()) < 0; }
public Name getPoiName() { return this.name;}