Search in sources :

Example 1 with CellRangeAddress

use of org.apache.poi.ss.util.CellRangeAddress in project Robot-Scouter by SUPERCILEX.

the class SpreadsheetExporter method setupRow.

private void setupRow(Row row, TeamHelper teamHelper, Metric metric) {
    Cell headerCell = row.getCell(0, MissingCellPolicy.CREATE_NULL_AS_BLANK);
    mCache.putKeyMetric(headerCell, metric);
    if (metric.getType() == HEADER) {
        headerCell.setCellStyle(mCache.getHeaderMetricRowHeaderStyle());
        int numOfScouts = mScouts.get(teamHelper).size();
        if (numOfScouts > SINGLE_ITEM) {
            int rowNum = row.getRowNum();
            row.getSheet().addMergedRegion(new CellRangeAddress(rowNum, rowNum, 1, numOfScouts));
        }
    } else {
        headerCell.setCellStyle(mCache.getRowHeaderStyle());
    }
}
Also used : CellRangeAddress(org.apache.poi.ss.util.CellRangeAddress) SpreadsheetUtils.getCellRangeAddress(com.supercilex.robotscouter.data.client.spreadsheet.SpreadsheetUtils.getCellRangeAddress) Cell(org.apache.poi.ss.usermodel.Cell) SpreadsheetUtils.getStringForCell(com.supercilex.robotscouter.data.client.spreadsheet.SpreadsheetUtils.getStringForCell) PreferencesUtilsKt.setShouldShowExportHint(com.supercilex.robotscouter.util.PreferencesUtilsKt.setShouldShowExportHint) PreferencesUtilsKt.shouldShowExportHint(com.supercilex.robotscouter.util.PreferencesUtilsKt.shouldShowExportHint)

Example 2 with CellRangeAddress

use of org.apache.poi.ss.util.CellRangeAddress in project Robot-Scouter by SUPERCILEX.

the class SpreadsheetExporter method buildAverageCharts.

@AddTrace(name = "buildAverageCharts")
private void buildAverageCharts(Sheet sheet) {
    if (isUnsupportedDevice())
        return;
    Drawing drawing = sheet.createDrawingPatriarch();
    List<Cell> headerCells = getAdjustedList(sheet.getRow(0));
    for (Cell cell : headerCells) {
        int columnIndex = cell.getColumnIndex();
        String headerName = cell.getStringCellValue();
        ClientAnchor anchor = createChartAnchor(drawing, sheet.getLastRowNum() + 3, columnIndex, columnIndex + 1);
        anchor.setRow2(anchor.getRow2() + 30);
        Chart chart = drawing.createChart(anchor);
        chart.getOrCreateLegend().setPosition(LegendPosition.BOTTOM);
        ChartDataSource<String> categorySource = DataSources.fromArray(new String[] { headerName });
        ScatterChartData data = chart.getChartDataFactory().createScatterChartData();
        List<Row> dataRows = getAdjustedList(sheet);
        for (Row row : dataRows) {
            data.addSerie(categorySource, DataSources.fromNumericCellRange(sheet, new CellRangeAddress(row.getRowNum(), row.getRowNum(), columnIndex, columnIndex))).setTitle(row.getCell(0).getStringCellValue());
        }
        ChartAxis bottomAxis = chart.getChartAxisFactory().createCategoryAxis(AxisPosition.BOTTOM);
        ValueAxis leftAxis = chart.getChartAxisFactory().createValueAxis(AxisPosition.LEFT);
        leftAxis.setCrosses(AxisCrosses.AUTO_ZERO);
        chart.plot(data, bottomAxis, leftAxis);
        if (chart instanceof XSSFChart) {
            CTPlotArea plotArea = ((XSSFChart) chart).getCTChart().getPlotArea();
            setChartAxisTitle(plotArea.getValAxArray(0).addNewTitle(), "Values");
            setChartAxisTitle(plotArea.getCatAxArray(0).addNewTitle(), headerName);
        }
    }
}
Also used : Drawing(org.apache.poi.ss.usermodel.Drawing) CTPlotArea(org.openxmlformats.schemas.drawingml.x2006.chart.CTPlotArea) RichTextString(org.apache.poi.ss.usermodel.RichTextString) ScatterChartData(org.apache.poi.ss.usermodel.charts.ScatterChartData) PreferencesUtilsKt.setShouldShowExportHint(com.supercilex.robotscouter.util.PreferencesUtilsKt.setShouldShowExportHint) PreferencesUtilsKt.shouldShowExportHint(com.supercilex.robotscouter.util.PreferencesUtilsKt.shouldShowExportHint) XSSFChart(org.apache.poi.xssf.usermodel.XSSFChart) ClientAnchor(org.apache.poi.ss.usermodel.ClientAnchor) ChartAxis(org.apache.poi.ss.usermodel.charts.ChartAxis) ValueAxis(org.apache.poi.ss.usermodel.charts.ValueAxis) Row(org.apache.poi.ss.usermodel.Row) CellRangeAddress(org.apache.poi.ss.util.CellRangeAddress) SpreadsheetUtils.getCellRangeAddress(com.supercilex.robotscouter.data.client.spreadsheet.SpreadsheetUtils.getCellRangeAddress) Cell(org.apache.poi.ss.usermodel.Cell) SpreadsheetUtils.getStringForCell(com.supercilex.robotscouter.data.client.spreadsheet.SpreadsheetUtils.getStringForCell) XSSFChart(org.apache.poi.xssf.usermodel.XSSFChart) Chart(org.apache.poi.ss.usermodel.Chart) CTChart(org.openxmlformats.schemas.drawingml.x2006.chart.CTChart) SpreadsheetUtils.getMetricForChart(com.supercilex.robotscouter.data.client.spreadsheet.SpreadsheetUtils.getMetricForChart) AddTrace(com.google.firebase.perf.metrics.AddTrace)

Example 3 with CellRangeAddress

use of org.apache.poi.ss.util.CellRangeAddress in project Robot-Scouter by SUPERCILEX.

the class SpreadsheetExporter method buildAverageColumn.

@AddTrace(name = "buildAverageColumn")
private void buildAverageColumn(Sheet sheet, TeamHelper teamHelper) {
    int farthestColumn = 0;
    for (Row row : sheet) {
        int last = row.getLastCellNum();
        if (last > farthestColumn)
            farthestColumn = last;
    }
    Map<Chart, Pair<LineChartData, List<ChartAxis>>> chartData = new HashMap<>();
    Map<Metric<Void>, Chart> chartPool = new HashMap<>();
    Iterator<Row> rowIterator = sheet.rowIterator();
    for (int i = 0; rowIterator.hasNext(); i++) {
        Row row = rowIterator.next();
        Cell cell = row.createCell(farthestColumn);
        if (i == 0) {
            cell.setCellValue(getString(R.string.average));
            cell.setCellStyle(mCache.getColumnHeaderStyle());
            continue;
        }
        Cell first = row.getCell(1, MissingCellPolicy.CREATE_NULL_AS_BLANK);
        cell.setCellStyle(first.getCellStyle());
        int type = getMetricForScouts(mScouts.get(teamHelper), mCache.getMetricKey(row)).getType();
        String rangeAddress = getCellRangeAddress(first, row.getCell(cell.getColumnIndex() - 1, MissingCellPolicy.CREATE_NULL_AS_BLANK));
        switch(type) {
            case BOOLEAN:
                cell.setCellFormula("COUNTIF(" + rangeAddress + ", TRUE) / COUNTA(" + rangeAddress + ")");
                mCache.setCellFormat(cell, "0.00%");
                break;
            case NUMBER:
                cell.setCellFormula("SUM(" + rangeAddress + ")" + " / " + "COUNT(" + rangeAddress + ")");
                buildTeamChart(row, teamHelper, chartData, chartPool);
                break;
            case STOPWATCH:
                String excludeZeros = "\"<>0\"";
                cell.setCellFormula("IF(COUNTIF(" + rangeAddress + ", " + excludeZeros + ") = 0, 0, AVERAGEIF(" + rangeAddress + ", " + excludeZeros + "))");
                buildTeamChart(row, teamHelper, chartData, chartPool);
                break;
            case LIST:
                sheet.setArrayFormula("INDEX(" + rangeAddress + ", " + "MATCH(" + "MAX(" + "COUNTIF(" + rangeAddress + ", " + rangeAddress + ")" + "), " + "COUNTIF(" + rangeAddress + ", " + rangeAddress + ")" + ", 0))", new CellRangeAddress(cell.getRowIndex(), cell.getRowIndex(), cell.getColumnIndex(), cell.getColumnIndex()));
                break;
            case HEADER:
            case TEXT:
                // Nothing to average
                break;
            default:
                throw new IllegalStateException();
        }
    }
    for (Chart chart : chartData.keySet()) {
        Pair<LineChartData, List<ChartAxis>> data = chartData.get(chart);
        chart.plot(data.first, data.second.toArray(new ChartAxis[data.second.size()]));
        if (chart instanceof XSSFChart) {
            XSSFChart xChart = (XSSFChart) chart;
            CTChart ctChart = xChart.getCTChart();
            CTPlotArea plotArea = ctChart.getPlotArea();
            setChartAxisTitle(plotArea.getValAxArray(0).addNewTitle(), "Values");
            setChartAxisTitle(plotArea.getCatAxArray(0).addNewTitle(), "Scouts");
            String name = getMetricForChart(xChart, chartPool).getName();
            if (!TextUtils.isEmpty(name))
                xChart.setTitle(name);
        }
    }
}
Also used : HashMap(java.util.HashMap) CTPlotArea(org.openxmlformats.schemas.drawingml.x2006.chart.CTPlotArea) RichTextString(org.apache.poi.ss.usermodel.RichTextString) PreferencesUtilsKt.setShouldShowExportHint(com.supercilex.robotscouter.util.PreferencesUtilsKt.setShouldShowExportHint) PreferencesUtilsKt.shouldShowExportHint(com.supercilex.robotscouter.util.PreferencesUtilsKt.shouldShowExportHint) XSSFChart(org.apache.poi.xssf.usermodel.XSSFChart) CTChart(org.openxmlformats.schemas.drawingml.x2006.chart.CTChart) ChartAxis(org.apache.poi.ss.usermodel.charts.ChartAxis) LineChartData(org.apache.poi.ss.usermodel.charts.LineChartData) Metric(com.supercilex.robotscouter.data.model.Metric) ArrayList(java.util.ArrayList) SpreadsheetUtils.getAdjustedList(com.supercilex.robotscouter.data.client.spreadsheet.SpreadsheetUtils.getAdjustedList) List(java.util.List) Row(org.apache.poi.ss.usermodel.Row) CellRangeAddress(org.apache.poi.ss.util.CellRangeAddress) SpreadsheetUtils.getCellRangeAddress(com.supercilex.robotscouter.data.client.spreadsheet.SpreadsheetUtils.getCellRangeAddress) Cell(org.apache.poi.ss.usermodel.Cell) SpreadsheetUtils.getStringForCell(com.supercilex.robotscouter.data.client.spreadsheet.SpreadsheetUtils.getStringForCell) XSSFChart(org.apache.poi.xssf.usermodel.XSSFChart) Chart(org.apache.poi.ss.usermodel.Chart) CTChart(org.openxmlformats.schemas.drawingml.x2006.chart.CTChart) SpreadsheetUtils.getMetricForChart(com.supercilex.robotscouter.data.client.spreadsheet.SpreadsheetUtils.getMetricForChart) Pair(android.util.Pair) AddTrace(com.google.firebase.perf.metrics.AddTrace)

Example 4 with CellRangeAddress

use of org.apache.poi.ss.util.CellRangeAddress in project poi by apache.

the class MergedCells method main.

public static void main(String[] args) throws IOException {
    HSSFWorkbook wb = new HSSFWorkbook();
    HSSFSheet sheet = wb.createSheet("new sheet");
    HSSFRow row = sheet.createRow(1);
    HSSFCell cell = row.createCell(1);
    cell.setCellValue("This is a test of merging");
    sheet.addMergedRegion(new CellRangeAddress(1, 1, 1, 2));
    // Write the output to a file
    FileOutputStream fileOut = new FileOutputStream("workbook.xls");
    wb.write(fileOut);
    fileOut.close();
    wb.close();
}
Also used : HSSFCell(org.apache.poi.hssf.usermodel.HSSFCell) FileOutputStream(java.io.FileOutputStream) HSSFRow(org.apache.poi.hssf.usermodel.HSSFRow) HSSFSheet(org.apache.poi.hssf.usermodel.HSSFSheet) CellRangeAddress(org.apache.poi.ss.util.CellRangeAddress) HSSFWorkbook(org.apache.poi.hssf.usermodel.HSSFWorkbook)

Example 5 with CellRangeAddress

use of org.apache.poi.ss.util.CellRangeAddress in project poi by apache.

the class CFRecordsAggregate method shiftRange.

private static CellRangeAddress shiftRange(FormulaShifter shifter, CellRangeAddress cra, int currentExternSheetIx) {
    // FormulaShifter works well in terms of Ptgs - so convert CellRangeAddress to AreaPtg (and back) here
    AreaPtg aptg = new AreaPtg(cra.getFirstRow(), cra.getLastRow(), cra.getFirstColumn(), cra.getLastColumn(), false, false, false, false);
    Ptg[] ptgs = { aptg };
    if (!shifter.adjustFormula(ptgs, currentExternSheetIx)) {
        return cra;
    }
    Ptg ptg0 = ptgs[0];
    if (ptg0 instanceof AreaPtg) {
        AreaPtg bptg = (AreaPtg) ptg0;
        return new CellRangeAddress(bptg.getFirstRow(), bptg.getLastRow(), bptg.getFirstColumn(), bptg.getLastColumn());
    }
    if (ptg0 instanceof AreaErrPtg) {
        return null;
    }
    throw new IllegalStateException("Unexpected shifted ptg class (" + ptg0.getClass().getName() + ")");
}
Also used : Ptg(org.apache.poi.ss.formula.ptg.Ptg) AreaErrPtg(org.apache.poi.ss.formula.ptg.AreaErrPtg) AreaPtg(org.apache.poi.ss.formula.ptg.AreaPtg) AreaErrPtg(org.apache.poi.ss.formula.ptg.AreaErrPtg) CellRangeAddress(org.apache.poi.ss.util.CellRangeAddress) AreaPtg(org.apache.poi.ss.formula.ptg.AreaPtg)

Aggregations

CellRangeAddress (org.apache.poi.ss.util.CellRangeAddress)246 Test (org.junit.Test)74 Row (org.apache.poi.ss.usermodel.Row)53 Cell (org.apache.poi.ss.usermodel.Cell)50 Sheet (org.apache.poi.ss.usermodel.Sheet)30 ArrayList (java.util.ArrayList)26 HSSFWorkbook (org.apache.poi.hssf.usermodel.HSSFWorkbook)26 Workbook (org.apache.poi.ss.usermodel.Workbook)24 XSSFWorkbook (org.apache.poi.xssf.usermodel.XSSFWorkbook)19 FileOutputStream (java.io.FileOutputStream)17 SXSSFWorkbook (org.apache.poi.xssf.streaming.SXSSFWorkbook)17 XSSFCellStyle (org.apache.poi.xssf.usermodel.XSSFCellStyle)17 HSSFConditionalFormattingRule (org.apache.poi.hssf.usermodel.HSSFConditionalFormattingRule)16 ConditionalFormattingRule (org.apache.poi.ss.usermodel.ConditionalFormattingRule)16 SheetConditionalFormatting (org.apache.poi.ss.usermodel.SheetConditionalFormatting)15 XSSFFont (org.apache.poi.xssf.usermodel.XSSFFont)15 CellReference (org.apache.poi.ss.util.CellReference)14 XSSFColor (org.apache.poi.xssf.usermodel.XSSFColor)13 HSSFConditionalFormatting (org.apache.poi.hssf.usermodel.HSSFConditionalFormatting)12 HSSFSheet (org.apache.poi.hssf.usermodel.HSSFSheet)10