Patch from Paolo from bug #44581 - fix ColumnHelper bug, and start on xssf stylings

git-svn-id: https://svn.apache.org/repos/asf/poi/branches/ooxml@636729 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Nick Burch 2008-03-13 11:50:50 +00:00
parent 98427a688c
commit 84a1e2052d
13 changed files with 1126 additions and 74 deletions

View File

@ -0,0 +1,287 @@
/* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==================================================================== */
package org.apache.poi.xssf.usermodel;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.extensions.XSSFCellBorder;
import org.apache.poi.xssf.usermodel.extensions.XSSFCellBorder.BorderSides;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTBorder;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTStylesheet;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTXf;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.STBorderStyle.Enum;
public class XSSFCellStyle implements CellStyle {
private CTStylesheet stylesheet;
private CTXf cellXf;
private CTXf cellStyleXf;
private XSSFCellBorder cellBorder;
public XSSFCellStyle(CTStylesheet stylesheet, int cellXfsId) {
this.stylesheet = stylesheet;
this.cellXf = stylesheet.getCellStyleXfs().getXfArray(cellXfsId);
if (cellXf.isSetXfId()) {
this.cellStyleXf = stylesheet.getCellStyleXfs().getXfArray((int) cellXf.getXfId());
}
}
public short getAlignment() {
// TODO Auto-generated method stub
return 0;
}
public short getBorderBottom() {
return (short) (getBorderStyle(BorderSides.BOTTOM).intValue() - 1);
}
public String getBorderBottomAsString() {
return getBorderStyle(BorderSides.BOTTOM).toString();
}
public short getBorderLeft() {
return (short) (getBorderStyle(BorderSides.LEFT).intValue() - 1);
}
public String getBorderLeftAsString() {
return getBorderStyle(BorderSides.LEFT).toString();
}
public short getBorderRight() {
return (short) (getBorderStyle(BorderSides.RIGHT).intValue() - 1);
}
public String getBorderRightAsString() {
return getBorderStyle(BorderSides.RIGHT).toString();
}
public short getBorderTop() {
return (short) (getBorderStyle(BorderSides.TOP).intValue() - 1);
}
public String getBorderTopAsString() {
return getBorderStyle(BorderSides.TOP).toString();
}
public short getBottomBorderColor() {
return getBorderColorBySide(BorderSides.BOTTOM);
}
public short getDataFormat() {
// TODO Auto-generated method stub
return 0;
}
public short getFillBackgroundColor() {
// TODO Auto-generated method stub
return 0;
}
public short getFillForegroundColor() {
// TODO Auto-generated method stub
return 0;
}
public short getFillPattern() {
// TODO Auto-generated method stub
return 0;
}
public Font getFont(Workbook parentWorkbook) {
// TODO Auto-generated method stub
return null;
}
public short getFontIndex() {
// TODO Auto-generated method stub
return 0;
}
public boolean getHidden() {
// TODO Auto-generated method stub
return false;
}
public short getIndention() {
// TODO Auto-generated method stub
return 0;
}
public short getIndex() {
// TODO Auto-generated method stub
return 0;
}
public short getLeftBorderColor() {
return getBorderColorBySide(BorderSides.LEFT);
}
public boolean getLocked() {
// TODO Auto-generated method stub
return false;
}
public short getRightBorderColor() {
return getBorderColorBySide(BorderSides.RIGHT);
}
public short getRotation() {
// TODO Auto-generated method stub
return 0;
}
public short getTopBorderColor() {
return getBorderColorBySide(BorderSides.TOP);
}
public short getVerticalAlignment() {
// TODO Auto-generated method stub
return 0;
}
public boolean getWrapText() {
// TODO Auto-generated method stub
return false;
}
public void setAlignment(short align) {
// TODO Auto-generated method stub
}
public void setBorderBottom(short border) {
// TODO Auto-generated method stub
}
public void setBorderLeft(short border) {
// TODO Auto-generated method stub
}
public void setBorderRight(short border) {
// TODO Auto-generated method stub
}
public void setBorderTop(short border) {
// TODO Auto-generated method stub
}
public void setBottomBorderColor(short color) {
// TODO Auto-generated method stub
}
public void setDataFormat(short fmt) {
// TODO Auto-generated method stub
}
public void setFillBackgroundColor(short bg) {
// TODO Auto-generated method stub
}
public void setFillForegroundColor(short bg) {
// TODO Auto-generated method stub
}
public void setFillPattern(short fp) {
// TODO Auto-generated method stub
}
public void setFont(Font font) {
// TODO Auto-generated method stub
}
public void setHidden(boolean hidden) {
// TODO Auto-generated method stub
}
public void setIndention(short indent) {
// TODO Auto-generated method stub
}
public void setLeftBorderColor(short color) {
// TODO Auto-generated method stub
}
public void setLocked(boolean locked) {
// TODO Auto-generated method stub
}
public void setRightBorderColor(short color) {
// TODO Auto-generated method stub
}
public void setRotation(short rotation) {
// TODO Auto-generated method stub
}
public void setTopBorderColor(short color) {
// TODO Auto-generated method stub
}
public void setVerticalAlignment(short align) {
// TODO Auto-generated method stub
}
public void setWrapText(boolean wrapped) {
// TODO Auto-generated method stub
}
private XSSFCellBorder getCellBorder() {
if (cellBorder == null) {
CTBorder border = stylesheet.getBorders().getBorderArray(getBorderId());
cellBorder = new XSSFCellBorder(border);
}
return cellBorder;
}
private int getBorderId() {
if (cellXf.isSetBorderId()) {
return (int) cellXf.getBorderId();
}
return (int) cellStyleXf.getBorderId();
}
private Enum getBorderStyle(BorderSides side) {
return getCellBorder().getBorderStyle(side);
}
private short getBorderColorBySide(BorderSides side) {
return (short) getCellBorder().getBorderColor(side).getIndexed();
}
}

View File

@ -0,0 +1,47 @@
/* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==================================================================== */
package org.apache.poi.xssf.usermodel.extensions;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCellAlignment;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.STHorizontalAlignment;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.STHorizontalAlignment.Enum;
public class XSSFCellAlignment {
private CTCellAlignment cellAlignement;
public XSSFCellAlignment(CTCellAlignment cellAlignment) {
this.cellAlignement = cellAlignment;
}
public Enum getHorizontal() {
return cellAlignement.getHorizontal();
}
public void setHorizontal(STHorizontalAlignment.Enum horizontal) {
cellAlignement.setHorizontal(horizontal);
}
public long getIndent() {
return cellAlignement.getIndent();
}
public void setIndent(long indent) {
cellAlignement.setIndent(indent);
}
}

View File

@ -0,0 +1,59 @@
/* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==================================================================== */
package org.apache.poi.xssf.usermodel.extensions;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTBorder;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTBorderPr;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.STBorderStyle.Enum;
public class XSSFCellBorder {
private CTBorder border;
public XSSFCellBorder(CTBorder border) {
this.border = border;
}
public static enum BorderSides {
TOP, RIGHT, BOTTOM, LEFT
}
public Enum getBorderStyle(BorderSides side) {
return getBorder(side).getStyle();
}
public XSSFColor getBorderColor(BorderSides side) {
CTBorderPr borderPr = getBorder(side);
if (!borderPr.isSetColor()) {
borderPr.addNewColor();
}
return new XSSFColor(getBorder(side).getColor());
}
private CTBorderPr getBorder(BorderSides side) {
switch (side) {
case TOP: return border.getTop();
case RIGHT: return border.getRight();
case BOTTOM: return border.getBottom();
case LEFT: return border.getLeft();
default: throw new IllegalArgumentException("No suitable side specified for the border");
}
}
}

View File

@ -0,0 +1,68 @@
/* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==================================================================== */
package org.apache.poi.xssf.usermodel.extensions;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTColor;
public class XSSFColor {
private CTColor color;
public XSSFColor(CTColor color) {
this.color = color;
}
public boolean isAuto() {
return color.getAuto();
}
public void setAuto(boolean auto) {
color.setAuto(auto);
}
public long getIndexed() {
return color.getIndexed();
}
public void setIndexed(long indexed) {
color.setIndexed(indexed);
}
public byte[] getRgb() {
return color.getRgb();
}
public void setRgb(byte[] rgb) {
color.setRgb(rgb);
}
public long getTheme() {
return color.getTheme();
}
public void setTheme(long theme) {
color.setTheme(theme);
}
public double getTint() {
return color.getTint();
}
public void setTint(double tint) {
color.setTint(tint);
}
}

View File

@ -17,14 +17,16 @@
package org.apache.poi.xssf.usermodel.helpers; package org.apache.poi.xssf.usermodel.helpers;
import java.util.Arrays;
import org.apache.poi.xssf.util.CTColComparator;
import org.apache.poi.xssf.util.NumericRanges;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCol; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCol;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCols; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCols;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorksheet; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorksheet;
public class ColumnHelper { public class ColumnHelper {
private CTWorksheet worksheet; private CTWorksheet worksheet;
private CTCols newCols; private CTCols newCols;
@ -38,81 +40,169 @@ public class ColumnHelper {
this.newCols = CTCols.Factory.newInstance(); this.newCols = CTCols.Factory.newInstance();
CTCols[] colsArray = worksheet.getColsArray(); CTCols[] colsArray = worksheet.getColsArray();
int i = 0; int i = 0;
for (i = 0 ; i < colsArray.length ; i++) { for (i = 0; i < colsArray.length; i++) {
CTCols cols = colsArray[i]; CTCols cols = colsArray[i];
CTCol[] colArray = cols.getColArray(); CTCol[] colArray = cols.getColArray();
for (int y = 0 ; y < colArray.length ; y++) { for (int y = 0; y < colArray.length; y++) {
CTCol col = colArray[y]; CTCol col = colArray[y];
for (long k = col.getMin() ; k <= col.getMax() ; k++) { newCols = addCleanColIntoCols(newCols, col);
if (!columnExists(newCols, k)) {
CTCol newCol = newCols.addNewCol();
newCol.setMin(k);
newCol.setMax(k);
setColumnAttributes(col, newCol);
}
}
} }
} }
for (int y = i-1 ; y >= 0 ; y--) { for (int y = i - 1; y >= 0; y--) {
worksheet.removeCols(y); worksheet.removeCols(y);
} }
worksheet.addNewCols(); worksheet.addNewCols();
worksheet.setColsArray(0, newCols); worksheet.setColsArray(0, newCols);
} }
public void sortColumns(CTCols newCols) {
CTCol[] colArray = newCols.getColArray();
Arrays.sort(colArray, new CTColComparator());
newCols.setColArray(colArray);
}
public CTCol cloneCol(CTCols cols, CTCol col) {
CTCol newCol = cols.addNewCol();
newCol.setMin(col.getMin());
newCol.setMax(col.getMax());
setColumnAttributes(col, newCol);
return newCol;
}
public CTCol getColumn(long index) { public CTCol getColumn(long index) {
for (int i = 0 ; i < worksheet.getColsArray(0).sizeOfColArray() ; i++) { for (int i = 0; i < worksheet.getColsArray(0).sizeOfColArray(); i++) {
if (worksheet.getColsArray(0).getColArray(i).getMin() == index) { if (worksheet.getColsArray(0).getColArray(i).getMin() == index) {
return worksheet.getColsArray(0).getColArray(i); return worksheet.getColsArray(0).getColArray(i);
} }
} }
return null; return null;
} }
public CTCols addCleanColIntoCols(CTCols cols, CTCol col) {
boolean colOverlaps = false;
for (int i = 0; i < cols.sizeOfColArray(); i++) {
CTCol ithCol = cols.getColArray(i);
long[] range1 = { ithCol.getMin(), ithCol.getMax() };
long[] range2 = { col.getMin(), col.getMax() };
long[] overlappingRange = NumericRanges.getOverlappingRange(range1,
range2);
int overlappingType = NumericRanges.getOverlappingType(range1,
range2);
// different behavior required for each of the 4 different
// overlapping types
if (overlappingType == NumericRanges.OVERLAPS_1_MINOR) {
ithCol.setMax(overlappingRange[0] - 1);
CTCol rangeCol = insertCol(cols, overlappingRange[0],
overlappingRange[1], new CTCol[] { ithCol, col });
i++;
CTCol newCol = insertCol(cols, (overlappingRange[1] + 1), col
.getMax(), new CTCol[] { col });
i++;
} else if (overlappingType == NumericRanges.OVERLAPS_2_MINOR) {
ithCol.setMin(overlappingRange[1] + 1);
CTCol rangeCol = insertCol(cols, overlappingRange[0],
overlappingRange[1], new CTCol[] { ithCol, col });
i++;
CTCol newCol = insertCol(cols, col.getMin(),
(overlappingRange[0] - 1), new CTCol[] { col });
i++;
} else if (overlappingType == NumericRanges.OVERLAPS_2_WRAPS) {
setColumnAttributes(col, ithCol);
if (col.getMin() != ithCol.getMin()) {
CTCol newColBefore = insertCol(cols, col.getMin(), (ithCol
.getMin() - 1), new CTCol[] { col });
i++;
}
if (col.getMax() != ithCol.getMax()) {
CTCol newColAfter = insertCol(cols, (ithCol.getMax() + 1),
col.getMax(), new CTCol[] { col });
i++;
}
} else if (overlappingType == NumericRanges.OVERLAPS_1_WRAPS) {
if (col.getMin() != ithCol.getMin()) {
CTCol newColBefore = insertCol(cols, ithCol.getMin(), (col
.getMin() - 1), new CTCol[] { ithCol });
i++;
}
if (col.getMax() != ithCol.getMax()) {
CTCol newColAfter = insertCol(cols, (col.getMax() + 1),
ithCol.getMax(), new CTCol[] { ithCol });
i++;
}
ithCol.setMin(overlappingRange[0]);
ithCol.setMax(overlappingRange[1]);
setColumnAttributes(col, ithCol);
}
if (overlappingType != NumericRanges.NO_OVERLAPS) {
colOverlaps = true;
}
}
if (!colOverlaps) {
CTCol newCol = cloneCol(cols, col);
}
sortColumns(cols);
return cols;
}
/*
* Insert a new CTCol at position 0 into cols, setting min=min, max=max and
* copying all the colsWithAttributes array cols attributes into newCol
*/
private CTCol insertCol(CTCols cols, long min, long max,
CTCol[] colsWithAttributes) {
CTCol newCol = cols.insertNewCol(0);
newCol.setMin(min);
newCol.setMax(max);
for (CTCol col : colsWithAttributes) {
setColumnAttributes(col, newCol);
}
return newCol;
}
public boolean columnExists(CTCols cols, long index) { public boolean columnExists(CTCols cols, long index) {
for (int i = 0 ; i < cols.sizeOfColArray() ; i++) { for (int i = 0; i < cols.sizeOfColArray(); i++) {
if (cols.getColArray(i).getMin() == index) { if (cols.getColArray(i).getMin() == index) {
return true; return true;
} }
} }
return false; return false;
} }
public void setColumnAttributes(CTCol col, CTCol newCol) { public void setColumnAttributes(CTCol fromCol, CTCol toCol) {
if (col.getWidth() != 0) { if (fromCol.getWidth() != 0) {
newCol.setWidth(col.getWidth()); toCol.setWidth(fromCol.getWidth());
} }
if (col.getHidden()) { if (fromCol.getHidden()) {
newCol.setHidden(true); toCol.setHidden(true);
} }
if (col.getBestFit()) { if (fromCol.getBestFit()) {
newCol.setBestFit(true); toCol.setBestFit(true);
} }
} }
public void setColBestFit(long index, boolean bestFit) {
CTCol col = getOrCreateColumn(index);
col.setBestFit(bestFit);
}
public void setColWidth(long index, double width) {
CTCol col = getOrCreateColumn(index);
col.setWidth(width);
}
public void setColHidden(long index, boolean hidden) {
CTCol col = getOrCreateColumn(index);
col.setHidden(hidden);
}
protected CTCol getOrCreateColumn(long index) { public void setColBestFit(long index, boolean bestFit) {
CTCol col = getColumn(index); CTCol col = getOrCreateColumn(index);
if (col == null) { col.setBestFit(bestFit);
col = worksheet.getColsArray(0).addNewCol(); }
col.setMin(index);
col.setMax(index); public void setColWidth(long index, double width) {
} CTCol col = getOrCreateColumn(index);
return col; col.setWidth(width);
} }
public void setColHidden(long index, boolean hidden) {
CTCol col = getOrCreateColumn(index);
col.setHidden(hidden);
}
protected CTCol getOrCreateColumn(long index) {
CTCol col = getColumn(index);
if (col == null) {
col = worksheet.getColsArray(0).addNewCol();
col.setMin(index);
col.setMax(index);
}
return col;
}
} }

View File

@ -0,0 +1,46 @@
/* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==================================================================== */
package org.apache.poi.xssf.util;
import java.util.Comparator;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCol;
public class CTColComparator implements Comparator<CTCol>{
public int compare(CTCol o1, CTCol o2) {
if (o1.getMin() < o2.getMin()) {
return -1;
}
else if (o1.getMin() > o2.getMin()) {
return 1;
}
else {
if (o1.getMax() < o2.getMax()) {
return -1;
}
if (o1.getMax() > o2.getMax()) {
return 1;
}
return 0;
}
}
}

View File

@ -0,0 +1,66 @@
/* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==================================================================== */
package org.apache.poi.xssf.util;
public class NumericRanges {
public static int NO_OVERLAPS = -1;
public static int OVERLAPS_1_MINOR = 0;
public static int OVERLAPS_2_MINOR = 1;
public static int OVERLAPS_1_WRAPS = 2;
public static int OVERLAPS_2_WRAPS = 3;
public static long[] getOverlappingRange(long[] range1, long[] range2) {
int overlappingType = getOverlappingType(range1, range2);
if (overlappingType == OVERLAPS_1_MINOR) {
return new long[]{range2[0], range1[1]};
}
else if (overlappingType == OVERLAPS_2_MINOR) {
return new long[]{range1[0], range2[1]};
}
else if (overlappingType == OVERLAPS_2_WRAPS) {
return range1;
}
else if (overlappingType == OVERLAPS_1_WRAPS) {
return range2;
}
return new long[]{-1, -1};
}
public static int getOverlappingType(long[] range1, long[] range2) {
long min1 = range1[0];
long max1 = range1[1];
long min2 = range2[0];
long max2 = range2[1];
if (min1 >= min2 && max1 <= max2) {
return OVERLAPS_2_WRAPS;
}
else if (min2 >= min1 && max2 <= max1) {
return OVERLAPS_1_WRAPS;
}
else if ((min2 >= min1 && min2 <= max1) && max2 >= max1) {
return OVERLAPS_1_MINOR;
}
else if ((min1 >= min2 && min1 <= max2) && max1 >= max2) {
return OVERLAPS_2_MINOR;
}
return NO_OVERLAPS;
}
}

View File

@ -37,7 +37,7 @@ public class TestLoadSaveXSSF extends TestCase {
protected void setUp() throws Exception { protected void setUp() throws Exception {
super.setUp(); super.setUp();
System.setProperty("org.apache.poi.util.POILogger", org.apache.poi.util.CommonsLogger.class.getName()); System.setProperty("org.apache.poi.util.POILogger", org.apache.poi.util.CommonsLogger.class.getName());
filename = System.getProperty("HSSF.testdata.path"); filename = System.getProperty("XSSF.testdata.path");
if (filename == null) { if (filename == null) {
filename = "src/ooxml/testcases/org/apache/poi/xssf/data"; filename = "src/ooxml/testcases/org/apache/poi/xssf/data";
} }
@ -58,7 +58,7 @@ public class TestLoadSaveXSSF extends TestCase {
// TODO filename string hard coded in XSSFWorkbook constructor in order to make ant test-ooxml target be successfull. // TODO filename string hard coded in XSSFWorkbook constructor in order to make ant test-ooxml target be successfull.
public void testLoadStyles() throws Exception { public void testLoadStyles() throws Exception {
XSSFWorkbook workbook = new XSSFWorkbook(new File("src/ooxml/testcases/org/apache/poi/xssf/data", "styles.xlsx").getAbsolutePath()); XSSFWorkbook workbook = new XSSFWorkbook(new File(filename, "styles.xlsx").getAbsolutePath());
Sheet sheet = workbook.getSheetAt(0); Sheet sheet = workbook.getSheetAt(0);
Row row = sheet.getRow(0); Row row = sheet.getRow(0);
Cell cell = row.getCell((short) 0); Cell cell = row.getCell((short) 0);
@ -68,7 +68,7 @@ public class TestLoadSaveXSSF extends TestCase {
// TODO filename string hard coded in XSSFWorkbook constructor in order to make ant test-ooxml target be successfull. // TODO filename string hard coded in XSSFWorkbook constructor in order to make ant test-ooxml target be successfull.
public void testLoadPictures() throws Exception { public void testLoadPictures() throws Exception {
XSSFWorkbook workbook = new XSSFWorkbook(new File("src/ooxml/testcases/org/apache/poi/xssf/data", "picture.xlsx").getAbsolutePath()); XSSFWorkbook workbook = new XSSFWorkbook(new File(filename, "picture.xlsx").getAbsolutePath());
List<PictureData> pictures = workbook.getAllPictures(); List<PictureData> pictures = workbook.getAllPictures();
assertEquals(1, pictures.size()); assertEquals(1, pictures.size());
} }

View File

@ -0,0 +1,85 @@
/* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==================================================================== */
package org.apache.poi.xssf.usermodel;
import junit.framework.TestCase;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTBorder;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTStylesheet;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTXf;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.STBorderStyle;
public class TestXSSFCellStyle extends TestCase {
private CTStylesheet ctStylesheet;
private CTBorder ctBorder;
private CTXf cellStyleXf;
private CTXf cellXf;
private XSSFCellStyle cellStyle;
public void setUp() {
ctStylesheet = CTStylesheet.Factory.newInstance();
ctBorder = ctStylesheet.addNewBorders().insertNewBorder(0);
cellStyleXf = ctStylesheet.addNewCellStyleXfs().addNewXf();
cellStyleXf.setBorderId(0);
cellXf = ctStylesheet.addNewCellXfs().addNewXf();
cellXf.setXfId(0);
cellStyle = new XSSFCellStyle(ctStylesheet, 0);
}
public void testGetBorderBottom() {
ctBorder.addNewBottom().setStyle(STBorderStyle.THIN);
assertEquals((short)1, cellStyle.getBorderBottom());
}
public void testGetBorderBottomAsString() {
ctBorder.addNewBottom().setStyle(STBorderStyle.THIN);
assertEquals("thin", cellStyle.getBorderBottomAsString());
}
public void testGetBorderRight() {
ctBorder.addNewRight().setStyle(STBorderStyle.MEDIUM);
assertEquals((short)2, cellStyle.getBorderRight());
}
public void testGetBorderRightAsString() {
ctBorder.addNewRight().setStyle(STBorderStyle.MEDIUM);
assertEquals("medium", cellStyle.getBorderRightAsString());
}
public void testGetBorderLeft() {
ctBorder.addNewLeft().setStyle(STBorderStyle.DASHED);
assertEquals((short)3, cellStyle.getBorderLeft());
}
public void testGetBorderLeftAsString() {
ctBorder.addNewLeft().setStyle(STBorderStyle.DASHED);
assertEquals("dashed", cellStyle.getBorderLeftAsString());
}
public void testGetBorderTop() {
ctBorder.addNewTop().setStyle(STBorderStyle.HAIR);
assertEquals((short)7, cellStyle.getBorderTop());
}
public void testGetTopBottomAsString() {
ctBorder.addNewTop().setStyle(STBorderStyle.HAIR);
assertEquals("hair", cellStyle.getBorderTopAsString());
}
}

View File

@ -0,0 +1,48 @@
/* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==================================================================== */
package org.apache.poi.xssf.usermodel.extensions;
import org.apache.poi.xssf.usermodel.extensions.XSSFCellBorder.BorderSides;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTBorder;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTBorderPr;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTStylesheet;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.STBorderStyle;
import junit.framework.TestCase;
public class TestXSSFBorder extends TestCase {
public void testGetBorderStyle() {
CTStylesheet stylesheet = CTStylesheet.Factory.newInstance();
CTBorder border = stylesheet.addNewBorders().addNewBorder();
CTBorderPr top = border.addNewTop();
CTBorderPr right = border.addNewRight();
CTBorderPr bottom = border.addNewBottom();
top.setStyle(STBorderStyle.DASH_DOT);
right.setStyle(STBorderStyle.NONE);
bottom.setStyle(STBorderStyle.THIN);
XSSFCellBorder cellBorderStyle = new XSSFCellBorder(border);
assertEquals("dashDot", cellBorderStyle.getBorderStyle(BorderSides.TOP).toString());
assertEquals("none", cellBorderStyle.getBorderStyle(BorderSides.RIGHT).toString());
assertEquals(1, cellBorderStyle.getBorderStyle(BorderSides.RIGHT).intValue());
assertEquals("thin", cellBorderStyle.getBorderStyle(BorderSides.BOTTOM).toString());
assertEquals(2, cellBorderStyle.getBorderStyle(BorderSides.BOTTOM).intValue());
}
}

View File

@ -25,12 +25,11 @@ import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCol;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCols; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCols;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorksheet; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorksheet;
public class TestColumnHelper extends TestCase { public class TestColumnHelper extends TestCase {
public void testCleanColumns() { public void testCleanColumns() {
CTWorksheet worksheet = CTWorksheet.Factory.newInstance(); CTWorksheet worksheet = CTWorksheet.Factory.newInstance();
CTCols cols1 = worksheet.addNewCols(); CTCols cols1 = worksheet.addNewCols();
CTCol col1 = cols1.addNewCol(); CTCol col1 = cols1.addNewCol();
col1.setMin(1); col1.setMin(1);
@ -42,26 +41,146 @@ public class TestColumnHelper extends TestCase {
col2.setMax(3); col2.setMax(3);
CTCols cols2 = worksheet.addNewCols(); CTCols cols2 = worksheet.addNewCols();
CTCol col4 = cols2.addNewCol(); CTCol col4 = cols2.addNewCol();
col4.setMin(3); col4.setMin(13);
col4.setMax(6); col4.setMax(16384);
// Test cleaning cols // Test cleaning cols
assertEquals(2, worksheet.sizeOfColsArray()); assertEquals(2, worksheet.sizeOfColsArray());
int count = countColumns(worksheet); int count = countColumns(worksheet);
assertEquals(7, count); assertEquals(16375, count);
// Clean columns and test a clean worksheet // Clean columns and test a clean worksheet
ColumnHelper helper = new ColumnHelper(worksheet); ColumnHelper helper = new ColumnHelper(worksheet);
assertEquals(1, worksheet.sizeOfColsArray()); assertEquals(1, worksheet.sizeOfColsArray());
count = countColumns(worksheet); count = countColumns(worksheet);
assertEquals(6, count); assertEquals(16375, count);
assertEquals((double) 88, helper.getColumn(1).getWidth()); assertEquals((double) 88, helper.getColumn(1).getWidth());
assertTrue(helper.getColumn(1).getHidden()); assertTrue(helper.getColumn(1).getHidden());
} }
public void testSortColumns() {
CTWorksheet worksheet = CTWorksheet.Factory.newInstance();
ColumnHelper helper = new ColumnHelper(worksheet);
CTCols cols1 = CTCols.Factory.newInstance();
CTCol col1 = cols1.addNewCol();
col1.setMin(1);
col1.setMax(1);
col1.setWidth(88);
col1.setHidden(true);
CTCol col2 = cols1.addNewCol();
col2.setMin(2);
col2.setMax(3);
CTCol col3 = cols1.addNewCol();
col3.setMin(13);
col3.setMax(16750);
assertEquals(3, cols1.sizeOfColArray());
CTCol col4 = cols1.addNewCol();
col4.setMin(8);
col4.setMax(11);
assertEquals(4, cols1.sizeOfColArray());
CTCol col5 = cols1.addNewCol();
col5.setMin(4);
col5.setMax(5);
assertEquals(5, cols1.sizeOfColArray());
CTCol col6 = cols1.addNewCol();
col6.setMin(8);
col6.setMax(9);
col6.setHidden(true);
CTCol col7 = cols1.addNewCol();
col7.setMin(6);
col7.setMax(8);
col7.setWidth(17.0);
CTCol col8 = cols1.addNewCol();
col8.setMin(25);
col8.setMax(27);
CTCol col9 = cols1.addNewCol();
col9.setMin(20);
col9.setMax(30);
assertEquals(9, cols1.sizeOfColArray());
assertEquals(20, cols1.getColArray(8).getMin());
assertEquals(30, cols1.getColArray(8).getMax());
helper.sortColumns(cols1);
assertEquals(9, cols1.sizeOfColArray());
assertEquals(25, cols1.getColArray(8).getMin());
assertEquals(27, cols1.getColArray(8).getMax());
}
public void testCloneCol() {
CTWorksheet worksheet = CTWorksheet.Factory.newInstance();
ColumnHelper helper = new ColumnHelper(worksheet);
CTCols cols = CTCols.Factory.newInstance();
CTCol col = CTCol.Factory.newInstance();
col.setMin(2);
col.setMax(8);
col.setHidden(true);
col.setWidth(13.4);
CTCol newCol = helper.cloneCol(cols, col);
assertEquals(2, newCol.getMin());
assertEquals(8, newCol.getMax());
assertTrue(newCol.getHidden());
assertEquals(13.4, newCol.getWidth());
}
public void testAddCleanColIntoCols() {
CTWorksheet worksheet = CTWorksheet.Factory.newInstance();
ColumnHelper helper = new ColumnHelper(worksheet);
CTCols cols1 = CTCols.Factory.newInstance();
CTCol col1 = cols1.addNewCol();
col1.setMin(1);
col1.setMax(1);
col1.setWidth(88);
col1.setHidden(true);
CTCol col2 = cols1.addNewCol();
col2.setMin(2);
col2.setMax(3);
CTCol col3 = cols1.addNewCol();
col3.setMin(13);
col3.setMax(16750);
assertEquals(3, cols1.sizeOfColArray());
CTCol col4 = cols1.addNewCol();
col4.setMin(8);
col4.setMax(9);
assertEquals(4, cols1.sizeOfColArray());
CTCol col5 = CTCol.Factory.newInstance();
col5.setMin(4);
col5.setMax(5);
helper.addCleanColIntoCols(cols1, col5);
assertEquals(5, cols1.sizeOfColArray());
CTCol col6 = CTCol.Factory.newInstance();
col6.setMin(8);
col6.setMax(11);
col6.setHidden(true);
helper.addCleanColIntoCols(cols1, col6);
assertEquals(6, cols1.sizeOfColArray());
CTCol col7 = CTCol.Factory.newInstance();
col7.setMin(6);
col7.setMax(8);
col7.setWidth(17.0);
helper.addCleanColIntoCols(cols1, col7);
assertEquals(8, cols1.sizeOfColArray());
CTCol col8 = CTCol.Factory.newInstance();
col8.setMin(20);
col8.setMax(30);
helper.addCleanColIntoCols(cols1, col8);
assertEquals(10, cols1.sizeOfColArray());
CTCol col9 = CTCol.Factory.newInstance();
col9.setMin(25);
col9.setMax(27);
helper.addCleanColIntoCols(cols1, col9);
System.err.println(cols1);
}
public void testGetColumn() { public void testGetColumn() {
CTWorksheet worksheet = CTWorksheet.Factory.newInstance(); CTWorksheet worksheet = CTWorksheet.Factory.newInstance();
CTCols cols1 = worksheet.addNewCols(); CTCols cols1 = worksheet.addNewCols();
CTCol col1 = cols1.addNewCol(); CTCol col1 = cols1.addNewCol();
col1.setMin(1); col1.setMin(1);
@ -75,7 +194,7 @@ public class TestColumnHelper extends TestCase {
CTCol col4 = cols2.addNewCol(); CTCol col4 = cols2.addNewCol();
col4.setMin(3); col4.setMin(3);
col4.setMax(6); col4.setMax(6);
ColumnHelper helper = new ColumnHelper(worksheet); ColumnHelper helper = new ColumnHelper(worksheet);
assertNotNull(helper.getColumn(1)); assertNotNull(helper.getColumn(1));
assertEquals((double) 88, helper.getColumn(1).getWidth()); assertEquals((double) 88, helper.getColumn(1).getWidth());
@ -83,7 +202,7 @@ public class TestColumnHelper extends TestCase {
assertFalse(helper.getColumn(2).getHidden()); assertFalse(helper.getColumn(2).getHidden());
assertNull(helper.getColumn(99)); assertNull(helper.getColumn(99));
} }
public void testSetColumnAttributes() { public void testSetColumnAttributes() {
CTCol col = CTCol.Factory.newInstance(); CTCol col = CTCol.Factory.newInstance();
col.setWidth(12); col.setWidth(12);
@ -91,12 +210,13 @@ public class TestColumnHelper extends TestCase {
CTCol newCol = CTCol.Factory.newInstance(); CTCol newCol = CTCol.Factory.newInstance();
assertEquals((double) 0, newCol.getWidth()); assertEquals((double) 0, newCol.getWidth());
assertFalse(newCol.getHidden()); assertFalse(newCol.getHidden());
ColumnHelper helper = new ColumnHelper(CTWorksheet.Factory.newInstance()); ColumnHelper helper = new ColumnHelper(CTWorksheet.Factory
.newInstance());
helper.setColumnAttributes(col, newCol); helper.setColumnAttributes(col, newCol);
assertEquals((double) 12, newCol.getWidth()); assertEquals((double) 12, newCol.getWidth());
assertTrue(newCol.getHidden()); assertTrue(newCol.getHidden());
} }
public void testGetOrCreateColumn() { public void testGetOrCreateColumn() {
XSSFWorkbook workbook = new XSSFWorkbook(); XSSFWorkbook workbook = new XSSFWorkbook();
XSSFSheet sheet = (XSSFSheet) workbook.createSheet("Sheet 1"); XSSFSheet sheet = (XSSFSheet) workbook.createSheet("Sheet 1");
@ -113,14 +233,15 @@ public class TestColumnHelper extends TestCase {
private int countColumns(CTWorksheet worksheet) { private int countColumns(CTWorksheet worksheet) {
int count; int count;
count = 0; count = 0;
for (int i = 0 ; i < worksheet.sizeOfColsArray() ; i++) { for (int i = 0; i < worksheet.sizeOfColsArray(); i++) {
for (int y = 0 ; y < worksheet.getColsArray(i).sizeOfColArray() ; y++) { for (int y = 0; y < worksheet.getColsArray(i).sizeOfColArray(); y++) {
for (long k = worksheet.getColsArray(i).getColArray(y).getMin() ; k <= worksheet.getColsArray(i).getColArray(y).getMax() ; k++) { for (long k = worksheet.getColsArray(i).getColArray(y).getMin(); k <= worksheet
.getColsArray(i).getColArray(y).getMax(); k++) {
count++; count++;
} }
} }
} }
return count; return count;
} }
} }

View File

@ -0,0 +1,77 @@
/* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==================================================================== */
package org.apache.poi.xssf.util;
import java.util.Arrays;
import junit.framework.TestCase;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCol;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCols;
public class TestCTColComparator extends TestCase {
public void testCompare() {
CTColComparator comparator = new CTColComparator();
CTCol o1 = CTCol.Factory.newInstance();
o1.setMin(1);
o1.setMax(10);
CTCol o2 = CTCol.Factory.newInstance();
o2.setMin(11);
o2.setMax(12);
assertEquals(-1, comparator.compare(o1, o2));
CTCol o3 = CTCol.Factory.newInstance();
o3.setMin(5);
o3.setMax(8);
CTCol o4 = CTCol.Factory.newInstance();
o4.setMin(5);
o4.setMax(80);
assertEquals(-1, comparator.compare(o3, o4));
}
public void testArraysSort() {
CTColComparator comparator = new CTColComparator();
CTCol o1 = CTCol.Factory.newInstance();
o1.setMin(1);
o1.setMax(10);
CTCol o2 = CTCol.Factory.newInstance();
o2.setMin(11);
o2.setMax(12);
assertEquals(-1, comparator.compare(o1, o2));
CTCol o3 = CTCol.Factory.newInstance();
o3.setMin(5);
o3.setMax(80);
CTCol o4 = CTCol.Factory.newInstance();
o4.setMin(5);
o4.setMax(8);
assertEquals(1, comparator.compare(o3, o4));
CTCol[] cols = new CTCol[4];
cols[0] = o1;
cols[1] = o2;
cols[2] = o3;
cols[3] = o4;
assertEquals(80, cols[2].getMax());
assertEquals(8, cols[3].getMax());
Arrays.sort(cols, comparator);
assertEquals(12, cols[3].getMax());
assertEquals(8, cols[1].getMax());
assertEquals(80, cols[2].getMax());
}
}

View File

@ -0,0 +1,58 @@
/* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==================================================================== */
package org.apache.poi.xssf.util;
import junit.framework.TestCase;
public class TestNumericRanges extends TestCase {
public void testGetOverlappingType() {
long[] r1 = {3, 8};
long[] r2 = {6, 11};
long[] r3 = {1, 5};
long[] r4 = {2, 20};
long[] r5 = {5, 6};
long[] r6 = {20, 23};
assertEquals(NumericRanges.OVERLAPS_1_MINOR, NumericRanges.getOverlappingType(r1, r2));
assertEquals(NumericRanges.OVERLAPS_2_MINOR, NumericRanges.getOverlappingType(r1, r3));
assertEquals(NumericRanges.OVERLAPS_2_WRAPS, NumericRanges.getOverlappingType(r1, r4));
assertEquals(NumericRanges.OVERLAPS_1_WRAPS, NumericRanges.getOverlappingType(r1, r5));
assertEquals(NumericRanges.NO_OVERLAPS, NumericRanges.getOverlappingType(r1, r6));
}
public void testGetOverlappingRange() {
long[] r1 = {3, 8};
long[] r2 = {6, 11};
long[] r3 = {1, 5};
long[] r4 = {2, 20};
long[] r5 = {5, 6};
long[] r6 = {20, 23};
assertEquals(6, NumericRanges.getOverlappingRange(r1, r2)[0]);
assertEquals(8, NumericRanges.getOverlappingRange(r1, r2)[1]);
assertEquals(3, NumericRanges.getOverlappingRange(r1, r3)[0]);
assertEquals(5, NumericRanges.getOverlappingRange(r1, r3)[1]);
assertEquals(3, NumericRanges.getOverlappingRange(r1, r4)[0]);
assertEquals(8, NumericRanges.getOverlappingRange(r1, r4)[1]);
assertEquals(5, NumericRanges.getOverlappingRange(r1, r5)[0]);
assertEquals(6, NumericRanges.getOverlappingRange(r1, r5)[1]);
assertEquals(-1, NumericRanges.getOverlappingRange(r1, r6)[0]);
assertEquals(-1, NumericRanges.getOverlappingRange(r1, r6)[1]);
}
}