Search in sources :

Example 16 with CellReference

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

the class ForkedEvaluationSheet method getOrCreateUpdatableCell.

public ForkedEvaluationCell getOrCreateUpdatableCell(int rowIndex, int columnIndex) {
    RowColKey key = new RowColKey(rowIndex, columnIndex);
    ForkedEvaluationCell result = _sharedCellsByRowCol.get(key);
    if (result == null) {
        EvaluationCell mcell = _masterSheet.getCell(rowIndex, columnIndex);
        if (mcell == null) {
            CellReference cr = new CellReference(rowIndex, columnIndex);
            throw new UnsupportedOperationException("Underlying cell '" + cr.formatAsString() + "' is missing in master sheet.");
        }
        result = new ForkedEvaluationCell(this, mcell);
        _sharedCellsByRowCol.put(key, result);
    }
    return result;
}
Also used : EvaluationCell(org.apache.poi.ss.formula.EvaluationCell) CellReference(org.apache.poi.ss.util.CellReference)

Example 17 with CellReference

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

the class Address method evaluate.

public ValueEval evaluate(ValueEval[] args, int srcRowIndex, int srcColumnIndex) {
    if (args.length < 2 || args.length > 5) {
        return ErrorEval.VALUE_INVALID;
    }
    try {
        boolean pAbsRow, pAbsCol;
        int row = (int) NumericFunction.singleOperandEvaluate(args[0], srcRowIndex, srcColumnIndex);
        int col = (int) NumericFunction.singleOperandEvaluate(args[1], srcRowIndex, srcColumnIndex);
        int refType;
        if (args.length > 2 && args[2] != MissingArgEval.instance) {
            refType = (int) NumericFunction.singleOperandEvaluate(args[2], srcRowIndex, srcColumnIndex);
        } else {
            // this is also the default if parameter is not given
            refType = REF_ABSOLUTE;
        }
        switch(refType) {
            case REF_ABSOLUTE:
                pAbsRow = true;
                pAbsCol = true;
                break;
            case REF_ROW_ABSOLUTE_COLUMN_RELATIVE:
                pAbsRow = true;
                pAbsCol = false;
                break;
            case REF_ROW_RELATIVE_RELATIVE_ABSOLUTE:
                pAbsRow = false;
                pAbsCol = true;
                break;
            case REF_RELATIVE:
                pAbsRow = false;
                pAbsCol = false;
                break;
            default:
                throw new EvaluationException(ErrorEval.VALUE_INVALID);
        }
        //            boolean a1;
        //            if(args.length > 3){
        //                ValueEval ve = OperandResolver.getSingleValue(args[3], srcRowIndex, srcColumnIndex);
        //                // TODO R1C1 style is not yet supported
        //                a1 = ve == MissingArgEval.instance ? true : OperandResolver.coerceValueToBoolean(ve, false);
        //            } else {
        //                a1 = true;
        //            }
        String sheetName;
        if (args.length == 5) {
            ValueEval ve = OperandResolver.getSingleValue(args[4], srcRowIndex, srcColumnIndex);
            sheetName = ve == MissingArgEval.instance ? null : OperandResolver.coerceValueToString(ve);
        } else {
            sheetName = null;
        }
        CellReference ref = new CellReference(row - 1, col - 1, pAbsRow, pAbsCol);
        StringBuffer sb = new StringBuffer(32);
        if (sheetName != null) {
            SheetNameFormatter.appendFormat(sb, sheetName);
            sb.append('!');
        }
        sb.append(ref.formatAsString());
        return new StringEval(sb.toString());
    } catch (EvaluationException e) {
        return e.getErrorEval();
    }
}
Also used : CellReference(org.apache.poi.ss.util.CellReference)

Example 18 with CellReference

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

the class XSSFRow method shift.

/**
     * update cell references when shifting rows
     *
     * @param n the number of rows to move
     */
protected void shift(int n) {
    int rownum = getRowNum() + n;
    CalculationChain calcChain = _sheet.getWorkbook().getCalculationChain();
    int sheetId = (int) _sheet.sheet.getSheetId();
    String msg = "Row[rownum=" + getRowNum() + "] contains cell(s) included in a multi-cell array formula. " + "You cannot change part of an array.";
    for (Cell c : this) {
        XSSFCell cell = (XSSFCell) c;
        if (cell.isPartOfArrayFormulaGroup()) {
            cell.notifyArrayFormulaChanging(msg);
        }
        //remove the reference in the calculation chain
        if (calcChain != null)
            calcChain.removeItem(sheetId, cell.getReference());
        CTCell ctCell = cell.getCTCell();
        String r = new CellReference(rownum, cell.getColumnIndex()).formatAsString();
        ctCell.setR(r);
    }
    setRowNum(rownum);
}
Also used : CTCell(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCell) CalculationChain(org.apache.poi.xssf.model.CalculationChain) CellReference(org.apache.poi.ss.util.CellReference) CTCell(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCell) Cell(org.apache.poi.ss.usermodel.Cell)

Example 19 with CellReference

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

the class XSSFSingleXmlCell method getReferencedCell.

/**
	 * Gets the XSSFCell referenced by the R attribute or creates a new one if cell doesn't exists
	 * @return the referenced XSSFCell, null if the cell reference is invalid
	 */
public XSSFCell getReferencedCell() {
    XSSFCell cell = null;
    CellReference cellReference = new CellReference(singleXmlCell.getR());
    XSSFRow row = parent.getXSSFSheet().getRow(cellReference.getRow());
    if (row == null) {
        row = parent.getXSSFSheet().createRow(cellReference.getRow());
    }
    cell = row.getCell(cellReference.getCol());
    if (cell == null) {
        cell = row.createCell(cellReference.getCol());
    }
    return cell;
}
Also used : XSSFRow(org.apache.poi.xssf.usermodel.XSSFRow) XSSFCell(org.apache.poi.xssf.usermodel.XSSFCell) CellReference(org.apache.poi.ss.util.CellReference)

Example 20 with CellReference

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

the class TestThemesTable method testThemesTableColors.

@Test
public void testThemesTableColors() throws Exception {
    // Load our two test workbooks
    XSSFWorkbook simple = XSSFTestDataSamples.openSampleWorkbook(testFileSimple);
    XSSFWorkbook complex = XSSFTestDataSamples.openSampleWorkbook(testFileComplex);
    // Save and re-load them, to check for stability across that
    XSSFWorkbook simpleRS = XSSFTestDataSamples.writeOutAndReadBack(simple);
    XSSFWorkbook complexRS = XSSFTestDataSamples.writeOutAndReadBack(complex);
    // Fetch fresh copies to test with
    simple = XSSFTestDataSamples.openSampleWorkbook(testFileSimple);
    complex = XSSFTestDataSamples.openSampleWorkbook(testFileComplex);
    // Files and descriptions
    Map<String, XSSFWorkbook> workbooks = new LinkedHashMap<String, XSSFWorkbook>();
    workbooks.put(testFileSimple, simple);
    workbooks.put("Re-Saved_" + testFileSimple, simpleRS);
    workbooks.put(testFileComplex, complex);
    workbooks.put("Re-Saved_" + testFileComplex, complexRS);
    // Sanity check
    assertEquals(rgbExpected.length, rgbExpected.length);
    // For offline testing
    boolean createFiles = false;
    //  for the theme-applied cells in Column A are correct
    for (String whatWorkbook : workbooks.keySet()) {
        XSSFWorkbook workbook = workbooks.get(whatWorkbook);
        XSSFSheet sheet = workbook.getSheetAt(0);
        int startRN = 0;
        if (whatWorkbook.endsWith(testFileComplex))
            startRN++;
        for (int rn = startRN; rn < rgbExpected.length + startRN; rn++) {
            XSSFRow row = sheet.getRow(rn);
            assertNotNull("Missing row " + rn + " in " + whatWorkbook, row);
            String ref = (new CellReference(rn, 0)).formatAsString();
            XSSFCell cell = row.getCell(0);
            assertNotNull("Missing cell " + ref + " in " + whatWorkbook, cell);
            int expectedThemeIdx = rn - startRN;
            ThemeElement themeElem = ThemeElement.byId(expectedThemeIdx);
            assertEquals("Wrong theme at " + ref + " in " + whatWorkbook, themeElem.name.toLowerCase(Locale.ROOT), cell.getStringCellValue());
            // Fonts are theme-based in their colours
            XSSFFont font = cell.getCellStyle().getFont();
            CTColor ctColor = font.getCTFont().getColorArray(0);
            assertNotNull(ctColor);
            assertEquals(true, ctColor.isSetTheme());
            assertEquals(themeElem.idx, ctColor.getTheme());
            // Get the colour, via the theme
            XSSFColor color = font.getXSSFColor();
            // Theme colours aren't tinted
            assertEquals(false, color.hasTint());
            // Check the RGB part (no tint)
            assertEquals("Wrong theme colour " + themeElem.name + " on " + whatWorkbook, rgbExpected[expectedThemeIdx], Hex.encodeHexString(color.getRGB()));
            long themeIdx = font.getCTFont().getColorArray(0).getTheme();
            assertEquals("Wrong theme index " + expectedThemeIdx + " on " + whatWorkbook, expectedThemeIdx, themeIdx);
            if (createFiles) {
                XSSFCellStyle cs = row.getSheet().getWorkbook().createCellStyle();
                cs.setFillForegroundColor(color);
                cs.setFillPattern(CellStyle.SOLID_FOREGROUND);
                row.createCell(1).setCellStyle(cs);
            }
        }
        if (createFiles) {
            FileOutputStream fos = new FileOutputStream("Generated_" + whatWorkbook);
            workbook.write(fos);
            fos.close();
        }
    }
}
Also used : CellReference(org.apache.poi.ss.util.CellReference) CTColor(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTColor) LinkedHashMap(java.util.LinkedHashMap) ThemeElement(org.apache.poi.xssf.model.ThemesTable.ThemeElement) XSSFColor(org.apache.poi.xssf.usermodel.XSSFColor) XSSFSheet(org.apache.poi.xssf.usermodel.XSSFSheet) XSSFCellStyle(org.apache.poi.xssf.usermodel.XSSFCellStyle) XSSFRow(org.apache.poi.xssf.usermodel.XSSFRow) XSSFFont(org.apache.poi.xssf.usermodel.XSSFFont) FileOutputStream(java.io.FileOutputStream) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) XSSFCell(org.apache.poi.xssf.usermodel.XSSFCell) Test(org.junit.Test)

Aggregations

CellReference (org.apache.poi.ss.util.CellReference)116 Test (org.junit.Test)52 Cell (org.apache.poi.ss.usermodel.Cell)23 Row (org.apache.poi.ss.usermodel.Row)22 AreaReference (org.apache.poi.ss.util.AreaReference)19 Sheet (org.apache.poi.ss.usermodel.Sheet)13 CellRangeAddress (org.apache.poi.ss.util.CellRangeAddress)12 Workbook (org.apache.poi.ss.usermodel.Workbook)11 SXSSFWorkbook (org.apache.poi.xssf.streaming.SXSSFWorkbook)11 HSSFWorkbook (org.apache.poi.hssf.usermodel.HSSFWorkbook)10 XSSFWorkbook (org.apache.poi.xssf.usermodel.XSSFWorkbook)8 FormulaEvaluator (org.apache.poi.ss.usermodel.FormulaEvaluator)7 XSSFSheet (org.apache.poi.xssf.usermodel.XSSFSheet)6 FileOutputStream (java.io.FileOutputStream)4 ArrayList (java.util.ArrayList)4 CellStyle (org.apache.poi.ss.usermodel.CellStyle)4 XSSFRow (org.apache.poi.xssf.usermodel.XSSFRow)4 File (java.io.File)3 OutputStream (java.io.OutputStream)3 LinkedHashMap (java.util.LinkedHashMap)3