Search in sources :

Example 1 with CTWorksheetSource

use of org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorksheetSource in project poi by apache.

the class XSSFPivotCacheDefinition method getPivotArea.

/**
     * Find the 2D base data area for the pivot table, either from its direct reference or named table/range.
     * @return AreaReference representing the current area defined by the pivot table
     * @throws IllegalArgumentException if the ref attribute is not contiguous or the name attribute is not found.
     */
@Beta
public AreaReference getPivotArea(Workbook wb) throws IllegalArgumentException {
    final CTWorksheetSource wsSource = ctPivotCacheDefinition.getCacheSource().getWorksheetSource();
    final String ref = wsSource.getRef();
    final String name = wsSource.getName();
    if (ref == null && name == null) {
        throw new IllegalArgumentException("Pivot cache must reference an area, named range, or table.");
    }
    // this is the XML format, so tell the reference that.
    if (ref != null) {
        return new AreaReference(ref, SpreadsheetVersion.EXCEL2007);
    }
    assert (name != null);
    // named range or table?
    final Name range = wb.getName(name);
    if (range != null) {
        return new AreaReference(range.getRefersToFormula(), SpreadsheetVersion.EXCEL2007);
    }
    // not a named range, check for a table.
    // do this second, as tables are sheet-specific, but named ranges are not, and may not have a sheet name given.
    final XSSFSheet sheet = (XSSFSheet) wb.getSheet(wsSource.getSheet());
    for (XSSFTable table : sheet.getTables()) {
        // TODO: case-sensitive?
        if (name.equals(table.getName())) {
            return new AreaReference(table.getStartCellReference(), table.getEndCellReference());
        }
    }
    throw new IllegalArgumentException("Name '" + name + "' was not found.");
}
Also used : AreaReference(org.apache.poi.ss.util.AreaReference) CTWorksheetSource(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorksheetSource) Name(org.apache.poi.ss.usermodel.Name) QName(javax.xml.namespace.QName) Beta(org.apache.poi.util.Beta)

Example 2 with CTWorksheetSource

use of org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorksheetSource in project poi by apache.

the class XSSFPivotTable method createSourceReferences.

/**
     * Creates cacheSource and workSheetSource for pivot table and sets the source reference as well assets the location of the pivot table
     * @param position Position for pivot table in sheet
     * @param sourceSheet Sheet where the source will be collected from
     * @param refConfig  an configurator that knows how to configure pivot table references
     */
@Beta
protected void createSourceReferences(CellReference position, Sheet sourceSheet, PivotTableReferenceConfigurator refConfig) {
    //Get cell one to the right and one down from position, add both to AreaReference and set pivot table location.
    AreaReference destination = new AreaReference(position, new CellReference(position.getRow() + 1, position.getCol() + 1));
    CTLocation location;
    if (pivotTableDefinition.getLocation() == null) {
        location = pivotTableDefinition.addNewLocation();
        location.setFirstDataCol(1);
        location.setFirstDataRow(1);
        location.setFirstHeaderRow(1);
    } else {
        location = pivotTableDefinition.getLocation();
    }
    location.setRef(destination.formatAsString());
    pivotTableDefinition.setLocation(location);
    //Set source for the pivot table
    CTPivotCacheDefinition cacheDef = getPivotCacheDefinition().getCTPivotCacheDefinition();
    CTCacheSource cacheSource = cacheDef.addNewCacheSource();
    cacheSource.setType(STSourceType.WORKSHEET);
    CTWorksheetSource worksheetSource = cacheSource.addNewWorksheetSource();
    worksheetSource.setSheet(sourceSheet.getSheetName());
    setDataSheet(sourceSheet);
    refConfig.configureReference(worksheetSource);
    if (worksheetSource.getName() == null && worksheetSource.getRef() == null)
        throw new IllegalArgumentException("Pivot table source area reference or name must be specified.");
}
Also used : CTLocation(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTLocation) CTPivotCacheDefinition(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTPivotCacheDefinition) AreaReference(org.apache.poi.ss.util.AreaReference) CTCacheSource(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCacheSource) CellReference(org.apache.poi.ss.util.CellReference) CTWorksheetSource(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorksheetSource) Beta(org.apache.poi.util.Beta)

Aggregations

AreaReference (org.apache.poi.ss.util.AreaReference)2 Beta (org.apache.poi.util.Beta)2 CTWorksheetSource (org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorksheetSource)2 QName (javax.xml.namespace.QName)1 Name (org.apache.poi.ss.usermodel.Name)1 CellReference (org.apache.poi.ss.util.CellReference)1 CTCacheSource (org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCacheSource)1 CTLocation (org.openxmlformats.schemas.spreadsheetml.x2006.main.CTLocation)1 CTPivotCacheDefinition (org.openxmlformats.schemas.spreadsheetml.x2006.main.CTPivotCacheDefinition)1