From 8f63a462557601824d30cd3f0326715671dcb84b Mon Sep 17 00:00:00 2001 From: Paolo Mottadelli Date: Thu, 10 Apr 2008 07:35:47 +0000 Subject: [PATCH] ColumnHelper get/setColDefaultStyle; XSSFSheet setDefaultColumnStyle; XSSFCellStyle constructor refactored abd getIndex method implemented; StyesTable get/setCellXf and get/setCellStyleXf. + tests git-svn-id: https://svn.apache.org/repos/asf/poi/branches/ooxml@646668 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/poi/ss/usermodel/StylesSource.java | 2 + .../apache/poi/xssf/model/StylesTable.java | 25 ++++++-- .../poi/xssf/usermodel/XSSFCellStyle.java | 13 ++-- .../apache/poi/xssf/usermodel/XSSFSheet.java | 7 +-- .../xssf/usermodel/helpers/ColumnHelper.java | 61 +++++++++++++------ .../poi/xssf/model/TestStylesTable.java | 2 + .../poi/xssf/usermodel/TestXSSFCellStyle.java | 4 +- .../poi/xssf/usermodel/TestXSSFSheet.java | 36 ++++++++++- .../usermodel/helpers/TestColumnHelper.java | 58 ++++++++++++++---- 9 files changed, 162 insertions(+), 46 deletions(-) diff --git a/src/ooxml/interfaces-jdk15/org/apache/poi/ss/usermodel/StylesSource.java b/src/ooxml/interfaces-jdk15/org/apache/poi/ss/usermodel/StylesSource.java index fbb10fab07..f5a9d6deb6 100644 --- a/src/ooxml/interfaces-jdk15/org/apache/poi/ss/usermodel/StylesSource.java +++ b/src/ooxml/interfaces-jdk15/org/apache/poi/ss/usermodel/StylesSource.java @@ -16,6 +16,8 @@ ==================================================================== */ package org.apache.poi.ss.usermodel; + + public interface StylesSource { public String getNumberFormatAt(long idx); public long putNumberFormat(String fmt); diff --git a/src/ooxml/java/org/apache/poi/xssf/model/StylesTable.java b/src/ooxml/java/org/apache/poi/xssf/model/StylesTable.java index e6965a91be..63dca9eb7a 100644 --- a/src/ooxml/java/org/apache/poi/xssf/model/StylesTable.java +++ b/src/ooxml/java/org/apache/poi/xssf/model/StylesTable.java @@ -168,15 +168,14 @@ public class StylesTable implements StylesSource, XSSFModel { } public CellStyle getStyleAt(long idx) { - CTXf mainXf = xfs.get((int)idx); - CTXf styleXf = null; + int styleXfId = 0; // 0 is the empty default - if(mainXf.getXfId() > 0) { - styleXf = styleXfs.get((int)mainXf.getXfId()); + if(xfs.get((int) idx).getXfId() > 0) { + styleXfId = (int) xfs.get((int) idx).getXfId(); } - return new XSSFCellStyle(mainXf, styleXf, this); + return new XSSFCellStyle((int) idx, styleXfId, this); } public synchronized long putStyle(CellStyle style) { XSSFCellStyle xStyle = (XSSFCellStyle)style; @@ -201,6 +200,22 @@ public class StylesTable implements StylesSource, XSSFModel { public long putFill(XSSFCellFill fill) { return putFill(fill, fills); } + + public CTXf getCellXfAt(long idx) { + return xfs.get((int) idx); + } + public long putCellXf(CTXf cellXf) { + xfs.add(cellXf); + return xfs.size(); + } + + public CTXf getCellStyleXfAt(long idx) { + return styleXfs.get((int) idx); + } + public long putCellStyleXf(CTXf cellStyleXf) { + styleXfs.add(cellStyleXf); + return styleXfs.size(); + } /** * For unit testing only */ diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCellStyle.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCellStyle.java index 4f8296d3bd..42ff608b65 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCellStyle.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCellStyle.java @@ -36,6 +36,8 @@ import org.openxmlformats.schemas.spreadsheetml.x2006.main.STVerticalAlignment; public class XSSFCellStyle implements CellStyle { + private int cellXfId; + private int cellStyleXfId; private StylesSource stylesSource; private CTXf cellXf; private CTXf cellStyleXf; @@ -50,10 +52,12 @@ public class XSSFCellStyle implements CellStyle { * @param cellStyleXf Optional, style xf * @param stylesSource Styles Source to work off */ - public XSSFCellStyle(CTXf cellXf, CTXf cellStyleXf, StylesSource stylesSource) { + public XSSFCellStyle(int cellXfId, int cellStyleXfId, StylesTable stylesSource) { + this.cellXfId = cellXfId; + this.cellStyleXfId = cellStyleXfId; this.stylesSource = stylesSource; - this.cellXf = cellXf; - this.cellStyleXf = cellStyleXf; + this.cellXf = stylesSource.getCellXfAt(this.cellXfId); + this.cellStyleXf = stylesSource.getCellStyleXfAt(this.cellStyleXfId); } /** @@ -168,8 +172,7 @@ public class XSSFCellStyle implements CellStyle { } public short getIndex() { - // TODO Auto-generated method stub - return 0; + return (short) this.cellXfId; } public short getLeftBorderColor() { diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java index 0311599046..b4378cdd42 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java @@ -313,7 +313,7 @@ public class XSSFSheet implements Sheet { } public short getColumnWidth(short column) { - return (short) columnHelper.getColumn(column).getWidth(); + return (short) columnHelper.getColumn(column, false).getWidth(); } public short getDefaultColumnWidth() { @@ -592,7 +592,7 @@ public class XSSFSheet implements Sheet { } public boolean isColumnHidden(short column) { - return columnHelper.getColumn(column).getHidden(); + return columnHelper.getColumn(column, false).getHidden(); } public boolean isDisplayFormulas() { @@ -724,8 +724,7 @@ public class XSSFSheet implements Sheet { } public void setDefaultColumnStyle(short column, CellStyle style) { - // TODO Auto-generated method stub - + columnHelper.setColDefaultStyle(column, style); } public void setDefaultColumnWidth(short width) { diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/helpers/ColumnHelper.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/helpers/ColumnHelper.java index cbda23ce66..c7806be748 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/helpers/ColumnHelper.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/helpers/ColumnHelper.java @@ -19,6 +19,7 @@ package org.apache.poi.xssf.usermodel.helpers; import java.util.Arrays; +import org.apache.poi.ss.usermodel.CellStyle; import org.apache.poi.xssf.util.CTColComparator; import org.apache.poi.xssf.util.NumericRanges; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCol; @@ -69,10 +70,22 @@ public class ColumnHelper { return newCol; } - public CTCol getColumn(long index) { - for (int i = 0; i < worksheet.getColsArray(0).sizeOfColArray(); i++) { - if (worksheet.getColsArray(0).getColArray(i).getMin() == index) { - return worksheet.getColsArray(0).getColArray(i); + public CTCol getColumn(long index, boolean splitColumns) { + CTCols colsArray = worksheet.getColsArray(0); + for (int i = 0; i < colsArray.sizeOfColArray(); i++) { + CTCol colArray = colsArray.getColArray(i); + if (colArray.getMin() <= index && colArray.getMax() >= index) { + if (splitColumns) { + if (colArray.getMin() < index) { + insertCol(colsArray, colArray.getMin(), (index - 1), new CTCol[]{colArray}); + } + if (colArray.getMax() > index) { + insertCol(colsArray, (index + 1), colArray.getMax(), new CTCol[]{colArray}); + } + colArray.setMin(index); + colArray.setMax(index); + } + return colArray; } } return null; @@ -169,34 +182,29 @@ public class ColumnHelper { } public void setColumnAttributes(CTCol fromCol, CTCol toCol) { - if (fromCol.getWidth() != 0) { - toCol.setWidth(fromCol.getWidth()); - } - if (fromCol.getHidden()) { - toCol.setHidden(true); - } - if (fromCol.getBestFit()) { - toCol.setBestFit(true); - } + toCol.setWidth(fromCol.getWidth()); + toCol.setHidden(fromCol.getHidden()); + toCol.setBestFit(fromCol.getBestFit()); + toCol.setStyle(fromCol.getStyle()); } public void setColBestFit(long index, boolean bestFit) { - CTCol col = getOrCreateColumn(index); + CTCol col = getOrCreateColumn(index, false); col.setBestFit(bestFit); } public void setColWidth(long index, double width) { - CTCol col = getOrCreateColumn(index); + CTCol col = getOrCreateColumn(index, false); col.setWidth(width); } public void setColHidden(long index, boolean hidden) { - CTCol col = getOrCreateColumn(index); + CTCol col = getOrCreateColumn(index, false); col.setHidden(hidden); } - protected CTCol getOrCreateColumn(long index) { - CTCol col = getColumn(index); + protected CTCol getOrCreateColumn(long index, boolean splitColumns) { + CTCol col = getColumn(index, splitColumns); if (col == null) { col = worksheet.getColsArray(0).addNewCol(); col.setMin(index); @@ -205,4 +213,21 @@ public class ColumnHelper { return col; } + public void setColDefaultStyle(long index, CellStyle style) { + setColDefaultStyle(index, style.getIndex()); + } + + public void setColDefaultStyle(long index, int styleId) { + CTCol col = getOrCreateColumn(index, true); + col.setStyle(styleId); + } + + // Returns -1 if no column is found for the given index + public int getColDefaultStyle(long index) { + if (getColumn(index, false) != null) { + return (int) getColumn(index, false).getStyle(); + } + return -1; + } + } diff --git a/src/ooxml/testcases/org/apache/poi/xssf/model/TestStylesTable.java b/src/ooxml/testcases/org/apache/poi/xssf/model/TestStylesTable.java index be187cc77f..edb051520b 100644 --- a/src/ooxml/testcases/org/apache/poi/xssf/model/TestStylesTable.java +++ b/src/ooxml/testcases/org/apache/poi/xssf/model/TestStylesTable.java @@ -20,9 +20,11 @@ package org.apache.poi.xssf.model; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.File; +import java.io.IOException; import org.apache.poi.xssf.usermodel.XSSFCellStyle; import org.apache.poi.xssf.usermodel.XSSFWorkbook; +import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTXf; import junit.framework.TestCase; diff --git a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFCellStyle.java b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFCellStyle.java index eea36b2f76..7780b5c3f2 100644 --- a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFCellStyle.java +++ b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFCellStyle.java @@ -81,7 +81,9 @@ public class TestXSSFCellStyle extends TestCase { cellXfs = ctStylesheet.addNewCellXfs(); cellXf = cellXfs.addNewXf(); cellXf.setXfId(1); - cellStyle = new XSSFCellStyle(cellXf, cellStyleXf, stylesTable); + stylesTable.putCellStyleXf(cellStyleXf); + stylesTable.putCellXf(cellXf); + cellStyle = new XSSFCellStyle(1, 1, stylesTable); } public void testGetSetBorderBottom() { diff --git a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFSheet.java b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFSheet.java index da73fa8d51..c709d81be3 100644 --- a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFSheet.java +++ b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFSheet.java @@ -25,12 +25,15 @@ import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.ss.util.Region; import org.apache.poi.xssf.model.CommentsTable; +import org.apache.poi.xssf.model.StylesTable; import org.apache.poi.xssf.usermodel.helpers.ColumnHelper; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCol; +import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCols; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTComment; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTComments; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTSheet; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorksheet; +import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTXf; import org.openxmlformats.schemas.spreadsheetml.x2006.main.STPane; import org.openxmlformats.schemas.spreadsheetml.x2006.main.STPaneState; @@ -338,10 +341,10 @@ public class TestXSSFSheet extends TestCase { XSSFWorkbook workbook = new XSSFWorkbook(); XSSFSheet sheet = (XSSFSheet) workbook.createSheet("Sheet 1"); ColumnHelper columnHelper = sheet.getColumnHelper(); - CTCol col = columnHelper.getColumn(13); + CTCol col = columnHelper.getColumn(13, false); assertNull(col); sheet.autoSizeColumn((short)13); - col = columnHelper.getColumn(13); + col = columnHelper.getColumn(13, false); assertNotNull(col); assertTrue(col.getBestFit()); } @@ -591,6 +594,35 @@ public class TestXSSFSheet extends TestCase { assertEquals(0, sheet.getNumMergedRegions()); } + public void testSetDefaultColumnStyle() { + XSSFWorkbook workbook = new XSSFWorkbook(); + CTSheet ctSheet = CTSheet.Factory.newInstance(); + CTWorksheet ctWorksheet = CTWorksheet.Factory.newInstance(); + XSSFSheet sheet = new XSSFSheet(ctSheet, ctWorksheet, (XSSFWorkbook) workbook); + StylesTable stylesTable = (StylesTable) workbook.getStylesSource(); + XSSFFont font = new XSSFFont(); + font.setFontName("Cambria"); + stylesTable.putFont(font); + CTXf cellStyleXf = CTXf.Factory.newInstance(); + cellStyleXf.setFontId(1); + cellStyleXf.setFillId(0); + cellStyleXf.setBorderId(0); + cellStyleXf.setNumFmtId(0); + stylesTable.putCellStyleXf(cellStyleXf); + CTXf cellXf = CTXf.Factory.newInstance(); + cellXf.setXfId(1); + stylesTable.putCellXf(cellXf); + XSSFCellStyle cellStyle = new XSSFCellStyle(1, 1, stylesTable); + assertEquals(1, cellStyle.getFontIndex()); + + sheet.setDefaultColumnStyle((short) 3, cellStyle); + assertEquals(1, ctWorksheet.getColsArray(0).getColArray(0).getStyle()); + XSSFRow row = (XSSFRow) sheet.createRow(0); + XSSFCell cell = (XSSFCell) sheet.getRow(0).createCell(3); + System.out.println(cell.getCellStyle()); + + } + private XSSFSheet createSheet(XSSFWorkbook workbook, String name) { XSSFSheet sheet = (XSSFSheet) workbook.createSheet(name); diff --git a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/helpers/TestColumnHelper.java b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/helpers/TestColumnHelper.java index d9d3294af7..6e964b7957 100644 --- a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/helpers/TestColumnHelper.java +++ b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/helpers/TestColumnHelper.java @@ -19,11 +19,15 @@ package org.apache.poi.xssf.usermodel.helpers; import junit.framework.TestCase; +import org.apache.poi.xssf.model.StylesTable; +import org.apache.poi.xssf.usermodel.XSSFCellStyle; import org.apache.poi.xssf.usermodel.XSSFSheet; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCol; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCols; +import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTSheet; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorksheet; +import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTXf; public class TestColumnHelper extends TestCase { @@ -53,8 +57,8 @@ public class TestColumnHelper extends TestCase { assertEquals(1, worksheet.sizeOfColsArray()); count = countColumns(worksheet); assertEquals(16375, count); - assertEquals((double) 88, helper.getColumn(1).getWidth()); - assertTrue(helper.getColumn(1).getHidden()); + assertEquals((double) 88, helper.getColumn(1, false).getWidth()); + assertTrue(helper.getColumn(1, false).getHidden()); } public void testSortColumns() { @@ -196,11 +200,12 @@ public class TestColumnHelper extends TestCase { col4.setMax(6); ColumnHelper helper = new ColumnHelper(worksheet); - assertNotNull(helper.getColumn(1)); - assertEquals((double) 88, helper.getColumn(1).getWidth()); - assertTrue(helper.getColumn(1).getHidden()); - assertFalse(helper.getColumn(2).getHidden()); - assertNull(helper.getColumn(99)); + assertNotNull(helper.getColumn(1, false)); + assertEquals((double) 88, helper.getColumn(1, false).getWidth()); + assertTrue(helper.getColumn(1, false).getHidden()); + assertFalse(helper.getColumn(2, false).getHidden()); + assertNull(helper.getColumn(99, false)); + assertNotNull(helper.getColumn(5, false)); } public void testSetColumnAttributes() { @@ -221,13 +226,44 @@ public class TestColumnHelper extends TestCase { XSSFWorkbook workbook = new XSSFWorkbook(); XSSFSheet sheet = (XSSFSheet) workbook.createSheet("Sheet 1"); ColumnHelper columnHelper = sheet.getColumnHelper(); - CTCol col = columnHelper.getOrCreateColumn(3); + CTCol col = columnHelper.getOrCreateColumn(3, false); assertNotNull(col); - assertNotNull(columnHelper.getColumn(3)); + assertNotNull(columnHelper.getColumn(3, false)); - CTCol col2 = columnHelper.getOrCreateColumn(30); + CTCol col2 = columnHelper.getOrCreateColumn(30, false); assertNotNull(col2); - assertNotNull(columnHelper.getColumn(30)); + assertNotNull(columnHelper.getColumn(30, false)); + } + + public void testGetSetColDefaultStyle() { + XSSFWorkbook workbook = new XSSFWorkbook(); + CTSheet ctSheet = CTSheet.Factory.newInstance(); + CTWorksheet ctWorksheet = CTWorksheet.Factory.newInstance(); + XSSFSheet sheet = new XSSFSheet(ctSheet, ctWorksheet, (XSSFWorkbook) workbook); + ColumnHelper columnHelper = sheet.getColumnHelper(); + CTCol col = columnHelper.getOrCreateColumn(3, false); + assertNotNull(col); + assertNotNull(columnHelper.getColumn(3, false)); + columnHelper.setColDefaultStyle(3, 2); + assertEquals(2, columnHelper.getColDefaultStyle(3)); + assertEquals(-1, columnHelper.getColDefaultStyle(4)); + StylesTable stylesTable = (StylesTable) workbook.getStylesSource(); + CTXf cellXf = CTXf.Factory.newInstance(); + cellXf.setFontId(0); + cellXf.setFillId(0); + cellXf.setBorderId(0); + cellXf.setNumFmtId(0); + cellXf.setXfId(0); + stylesTable.putCellXf(cellXf); + CTCol col_2 = ctWorksheet.getColsArray(0).addNewCol(); + col_2.setMin(10); + col_2.setMax(12); + col_2.setStyle(1); + assertEquals(1, columnHelper.getColDefaultStyle(11)); + XSSFCellStyle cellStyle = new XSSFCellStyle(0, 0, stylesTable); + columnHelper.setColDefaultStyle(11, cellStyle); + assertEquals(0, col_2.getStyle()); + assertEquals(1, columnHelper.getColDefaultStyle(10)); } private int countColumns(CTWorksheet worksheet) { -- 2.39.5