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;
}
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();
}
}
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);
}
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;
}
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();
}
}
}
Aggregations