package org.apache.poi.ss.util;
+import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
+import org.apache.poi.util.Removal;
/**
* Various utility functions that make working with a region of cells easier.
public void setProperty(Row row, int column) {
+ // create cell if it does not exist
Cell cell = CellUtil.getCell(row, column);
CellUtil.setCellStyleProperty(cell, _propertyName, _propertyValue);
}
}
/**
- * Sets the left border for a region of cells by manipulating the cell style of the individual
+ * Sets the left border style for a region of cells by manipulating the cell style of the individual
* cells on the left
*
* @param border The new border
* @param region The region that should have the border
* @param workbook The workbook that the region is on.
* @param sheet The sheet that the region is on.
- * @deprecated 3.15 beta 2. Use {@link #setBorderLeft(int, CellRangeAddress, Sheet)}.
+ * @deprecated 3.15 beta 2. Use {@link #setBorderLeft(BorderStyle, CellRangeAddress, Sheet)}.
*/
+ @Removal(version="3.17")
public static void setBorderLeft(int border, CellRangeAddress region, Sheet sheet, Workbook workbook) {
setBorderLeft(border, region, sheet);
}
/**
- * Sets the left border for a region of cells by manipulating the cell style of the individual
+ * Sets the left border style for a region of cells by manipulating the cell style of the individual
* cells on the left
*
* @param border The new border
* @param region The region that should have the border
* @param sheet The sheet that the region is on.
+ * @since POI 3.15 beta 2
+ * @deprecated 3.16 beta 1. Use {@link #setBorderLeft(BorderStyle, CellRangeAddress, Sheet)}.
*/
+ @Removal(version="3.18")
public static void setBorderLeft(int border, CellRangeAddress region, Sheet sheet) {
int rowStart = region.getFirstRow();
int rowEnd = region.getLastRow();
cps.setProperty(CellUtil.getRow(i, sheet), column);
}
}
+ /**
+ * Sets the left border style for a region of cells by manipulating the cell style of the individual
+ * cells on the left
+ *
+ * @param border The new border
+ * @param region The region that should have the border
+ * @param sheet The sheet that the region is on.
+ * @since POI 3.16 beta 1
+ */
+ public static void setBorderLeft(BorderStyle border, CellRangeAddress region, Sheet sheet) {
+ setBorderLeft(border.getCode(), region, sheet);
+ }
/**
* Sets the left border color for a region of cells by manipulating the cell style of the individual
* @param sheet The sheet that the region is on.
* @deprecated 3.15 beta 2. Use {@link #setLeftBorderColor(int, CellRangeAddress, Sheet)}.
*/
+ @Removal(version="3.17")
public static void setLeftBorderColor(int color, CellRangeAddress region, Sheet sheet, Workbook workbook) {
setLeftBorderColor(color, region, sheet);
}
* @param color The color of the border
* @param region The region that should have the border
* @param sheet The sheet that the region is on.
+ * @since POI 3.15 beta 2
*/
public static void setLeftBorderColor(int color, CellRangeAddress region, Sheet sheet) {
int rowStart = region.getFirstRow();
}
/**
- * Sets the right border for a region of cells by manipulating the cell style of the individual
+ * Sets the right border style for a region of cells by manipulating the cell style of the individual
* cells on the right
*
* @param border The new border
* @param region The region that should have the border
* @param workbook The workbook that the region is on.
* @param sheet The sheet that the region is on.
- * @deprecated 3.15 beta 2. Use {@link #setBorderRight(int, CellRangeAddress, Sheet)}.
+ * @deprecated 3.15 beta 2. Use {@link #setBorderRight(BorderStyle, CellRangeAddress, Sheet)}.
*/
+ @Removal(version="3.17")
public static void setBorderRight(int border, CellRangeAddress region, Sheet sheet, Workbook workbook) {
setBorderRight(border, region, sheet);
}
/**
- * Sets the right border for a region of cells by manipulating the cell style of the individual
+ * Sets the right border style for a region of cells by manipulating the cell style of the individual
* cells on the right
*
* @param border The new border
* @param region The region that should have the border
* @param sheet The sheet that the region is on.
+ * @since POI 3.15 beta 2
+ * @deprecated POI 3.16 beta 1. Use {@link #setBorderRight(BorderStyle, CellRangeAddress, Sheet)}.
*/
+ @Removal(version="3.18")
public static void setBorderRight(int border, CellRangeAddress region, Sheet sheet) {
int rowStart = region.getFirstRow();
int rowEnd = region.getLastRow();
cps.setProperty(CellUtil.getRow(i, sheet), column);
}
}
+ /**
+ * Sets the right border style for a region of cells by manipulating the cell style of the individual
+ * cells on the right
+ *
+ * @param border The new border
+ * @param region The region that should have the border
+ * @param sheet The sheet that the region is on.
+ * @since POI 3.16 beta 1
+ */
+ public static void setBorderRight(BorderStyle border, CellRangeAddress region, Sheet sheet) {
+ setBorderRight(border.getCode(), region, sheet);
+ }
/**
* Sets the right border color for a region of cells by manipulating the cell style of the individual
* @param sheet The sheet that the region is on.
* @deprecated 3.15 beta 2. Use {@link #setRightBorderColor(int, CellRangeAddress, Sheet)}.
*/
+ @Removal(version="3.17")
public static void setRightBorderColor(int color, CellRangeAddress region, Sheet sheet, Workbook workbook) {
setRightBorderColor(color, region, sheet);
}
* @param color The color of the border
* @param region The region that should have the border
* @param sheet The sheet that the region is on.
+ * @since POI 3.15 beta 2
*/
public static void setRightBorderColor(int color, CellRangeAddress region, Sheet sheet) {
int rowStart = region.getFirstRow();
}
/**
- * Sets the bottom border for a region of cells by manipulating the cell style of the individual
+ * Sets the bottom border style for a region of cells by manipulating the cell style of the individual
* cells on the bottom
*
* @param border The new border
* @param region The region that should have the border
* @param workbook The workbook that the region is on.
* @param sheet The sheet that the region is on.
- * @deprecated 3.15 beta 2. Use {@link #setBorderBottom(int, CellRangeAddress, Sheet)}.
+ * @deprecated 3.15 beta 2. Use {@link #setBorderBottom(BorderStyle, CellRangeAddress, Sheet)}.
*/
+ @Removal(version="3.17")
public static void setBorderBottom(int border, CellRangeAddress region, Sheet sheet, Workbook workbook) {
setBorderBottom(border, region, sheet);
}
/**
- * Sets the bottom border for a region of cells by manipulating the cell style of the individual
+ * Sets the bottom border style for a region of cells by manipulating the cell style of the individual
* cells on the bottom
*
* @param border The new border
* @param region The region that should have the border
* @param sheet The sheet that the region is on.
+ * @since POI 3.15 beta 2
+ * @deprecated POI 3.16 beta 1. Use {@link #setBorderBottom(BorderStyle, CellRangeAddress, Sheet)}.
*/
+ @Removal(version="3.18")
public static void setBorderBottom(int border, CellRangeAddress region, Sheet sheet) {
int colStart = region.getFirstColumn();
int colEnd = region.getLastColumn();
cps.setProperty(row, i);
}
}
+ /**
+ * Sets the bottom border style for a region of cells by manipulating the cell style of the individual
+ * cells on the bottom
+ *
+ * @param border The new border
+ * @param region The region that should have the border
+ * @param sheet The sheet that the region is on.
+ * @since POI 3.16 beta 1
+ */
+ public static void setBorderBottom(BorderStyle border, CellRangeAddress region, Sheet sheet) {
+ setBorderBottom(border.getCode(), region, sheet);
+ }
/**
* Sets the bottom border color for a region of cells by manipulating the cell style of the individual
* @param sheet The sheet that the region is on.
* @deprecated 3.15 beta 2. Use {@link #setBottomBorderColor(int, CellRangeAddress, Sheet)}.
*/
+ @Removal(version="3.17")
public static void setBottomBorderColor(int color, CellRangeAddress region, Sheet sheet, Workbook workbook) {
setBottomBorderColor(color, region, sheet);
}
* @param color The color of the border
* @param region The region that should have the border
* @param sheet The sheet that the region is on.
+ * @since POI 3.15 beta 2
*/
public static void setBottomBorderColor(int color, CellRangeAddress region, Sheet sheet) {
int colStart = region.getFirstColumn();
}
/**
- * Sets the top border for a region of cells by manipulating the cell style of the individual
+ * Sets the top border style for a region of cells by manipulating the cell style of the individual
* cells on the top
*
* @param border The new border
* @param region The region that should have the border
* @param workbook The workbook that the region is on.
* @param sheet The sheet that the region is on.
- * @deprecated 3.15 beta 2. Use {@link #setBorderTop(int, CellRangeAddress, Sheet)}.
+ * @deprecated 3.15 beta 2. Use {@link #setBorderTop(BorderStyle, CellRangeAddress, Sheet)}.
*/
+ @Removal(version="3.17")
public static void setBorderTop(int border, CellRangeAddress region, Sheet sheet, Workbook workbook) {
setBorderTop(border, region, sheet);
}
/**
- * Sets the top border for a region of cells by manipulating the cell style of the individual
+ * Sets the top border style for a region of cells by manipulating the cell style of the individual
* cells on the top
*
* @param border The new border
* @param region The region that should have the border
* @param sheet The sheet that the region is on.
+ * @since POI 3.15 beta 2
+ * @deprecated 3.16 beta 1. Use {@link #setBorderTop(BorderStyle, CellRangeAddress, Sheet)}.
*/
+ @Removal(version="3.18")
public static void setBorderTop(int border, CellRangeAddress region, Sheet sheet) {
int colStart = region.getFirstColumn();
int colEnd = region.getLastColumn();
cps.setProperty(row, i);
}
}
+ /**
+ * Sets the top border style for a region of cells by manipulating the cell style of the individual
+ * cells on the top
+ *
+ * @param border The new border
+ * @param region The region that should have the border
+ * @param sheet The sheet that the region is on.
+ * @since POI 3.16 beta 1
+ */
+ public static void setBorderTop(BorderStyle border, CellRangeAddress region, Sheet sheet) {
+ setBorderTop(border.getCode(), region, sheet);
+ }
/**
* Sets the top border color for a region of cells by manipulating the cell style of the individual
* @param sheet The sheet that the region is on.
* @deprecated 3.15 beta 2. Use {@link #setTopBorderColor(int, CellRangeAddress, Sheet)}.
*/
+ @Removal(version="3.17")
public static void setTopBorderColor(int color, CellRangeAddress region, Sheet sheet, Workbook workbook) {
setTopBorderColor(color, region, sheet);
}
* @param color The color of the border
* @param region The region that should have the border
* @param sheet The sheet that the region is on.
+ * @since POI 3.15 beta 2
*/
public static void setTopBorderColor(int color, CellRangeAddress region, Sheet sheet) {
int colStart = region.getFirstColumn();
package org.apache.poi.ss.util;
+import static org.junit.Assert.*;
+
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.BorderStyle;
+import org.apache.poi.ss.usermodel.Cell;
+import org.apache.poi.ss.usermodel.CellStyle;
+import org.apache.poi.ss.usermodel.IndexedColors;
+import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.junit.After;
*/
public final class TestRegionUtil {
private static final CellRangeAddress A1C3 = new CellRangeAddress(0, 2, 0, 2);
- private static short THIN = BorderStyle.THIN.getCode();
+ private static final BorderStyle NONE = BorderStyle.NONE;
+ private static final BorderStyle THIN = BorderStyle.THIN;
+ private static final int RED = IndexedColors.RED.getIndex();
+ private static final int DEFAULT_COLOR = 0;
private Workbook wb;
private Sheet sheet;
wb.close();
}
- // TODO: fill this in with meaningful unit tests
- // Right now this just makes sure that RegionUtil is compiled into poi schemas
- // and that the code doesn't run in an infinite loop.
- // Don't spend too much time getting this unit test to work as this class
- // will likely be replaced by CellStyleTemplate soon.
+ private CellStyle getCellStyle(int rowIndex, int columnIndex) {
+ Row row = sheet.getRow(rowIndex);
+ if (row == null) row = sheet.createRow(rowIndex);
+ Cell cell = row.getCell(columnIndex);
+ if (cell == null) cell = row.createCell(columnIndex);
+ return cell.getCellStyle();
+ }
+
@Test
public void setBorderTop() {
+ assertEquals(NONE, getCellStyle(0, 0).getBorderTopEnum());
+ assertEquals(NONE, getCellStyle(0, 1).getBorderTopEnum());
+ assertEquals(NONE, getCellStyle(0, 2).getBorderTopEnum());
RegionUtil.setBorderTop(THIN, A1C3, sheet);
+ assertEquals(THIN, getCellStyle(0, 0).getBorderTopEnum());
+ assertEquals(THIN, getCellStyle(0, 1).getBorderTopEnum());
+ assertEquals(THIN, getCellStyle(0, 2).getBorderTopEnum());
}
@Test
public void setBorderBottom() {
+ assertEquals(NONE, getCellStyle(2, 0).getBorderBottomEnum());
+ assertEquals(NONE, getCellStyle(2, 1).getBorderBottomEnum());
+ assertEquals(NONE, getCellStyle(2, 2).getBorderBottomEnum());
RegionUtil.setBorderBottom(THIN, A1C3, sheet);
+ assertEquals(THIN, getCellStyle(2, 0).getBorderBottomEnum());
+ assertEquals(THIN, getCellStyle(2, 1).getBorderBottomEnum());
+ assertEquals(THIN, getCellStyle(2, 2).getBorderBottomEnum());
}
@Test
public void setBorderRight() {
+ assertEquals(NONE, getCellStyle(0, 2).getBorderRightEnum());
+ assertEquals(NONE, getCellStyle(1, 2).getBorderRightEnum());
+ assertEquals(NONE, getCellStyle(2, 2).getBorderRightEnum());
RegionUtil.setBorderRight(THIN, A1C3, sheet);
+ assertEquals(THIN, getCellStyle(0, 2).getBorderRightEnum());
+ assertEquals(THIN, getCellStyle(1, 2).getBorderRightEnum());
+ assertEquals(THIN, getCellStyle(2, 2).getBorderRightEnum());
}
@Test
public void setBorderLeft() {
+ assertEquals(NONE, getCellStyle(0, 0).getBorderLeftEnum());
+ assertEquals(NONE, getCellStyle(1, 0).getBorderLeftEnum());
+ assertEquals(NONE, getCellStyle(2, 0).getBorderLeftEnum());
RegionUtil.setBorderLeft(THIN, A1C3, sheet);
+ assertEquals(THIN, getCellStyle(0, 0).getBorderLeftEnum());
+ assertEquals(THIN, getCellStyle(1, 0).getBorderLeftEnum());
+ assertEquals(THIN, getCellStyle(2, 0).getBorderLeftEnum());
}
@Test
public void setTopBorderColor() {
- RegionUtil.setTopBorderColor(THIN, A1C3, sheet);
+ assertEquals(DEFAULT_COLOR, getCellStyle(0, 0).getTopBorderColor());
+ assertEquals(DEFAULT_COLOR, getCellStyle(0, 1).getTopBorderColor());
+ assertEquals(DEFAULT_COLOR, getCellStyle(0, 2).getTopBorderColor());
+ RegionUtil.setTopBorderColor(RED, A1C3, sheet);
+ assertEquals(RED, getCellStyle(0, 0).getTopBorderColor());
+ assertEquals(RED, getCellStyle(0, 1).getTopBorderColor());
+ assertEquals(RED, getCellStyle(0, 2).getTopBorderColor());
}
@Test
public void setBottomBorderColor() {
- RegionUtil.setBottomBorderColor(THIN, A1C3, sheet);
+ assertEquals(DEFAULT_COLOR, getCellStyle(2, 0).getBottomBorderColor());
+ assertEquals(DEFAULT_COLOR, getCellStyle(2, 1).getBottomBorderColor());
+ assertEquals(DEFAULT_COLOR, getCellStyle(2, 2).getBottomBorderColor());
+ RegionUtil.setBottomBorderColor(RED, A1C3, sheet);
+ assertEquals(RED, getCellStyle(2, 0).getBottomBorderColor());
+ assertEquals(RED, getCellStyle(2, 1).getBottomBorderColor());
+ assertEquals(RED, getCellStyle(2, 2).getBottomBorderColor());
}
@Test
public void setRightBorderColor() {
- RegionUtil.setRightBorderColor(THIN, A1C3, sheet);
+ assertEquals(DEFAULT_COLOR, getCellStyle(0, 2).getRightBorderColor());
+ assertEquals(DEFAULT_COLOR, getCellStyle(1, 2).getRightBorderColor());
+ assertEquals(DEFAULT_COLOR, getCellStyle(2, 2).getRightBorderColor());
+ RegionUtil.setRightBorderColor(RED, A1C3, sheet);
+ assertEquals(RED, getCellStyle(0, 2).getRightBorderColor());
+ assertEquals(RED, getCellStyle(1, 2).getRightBorderColor());
+ assertEquals(RED, getCellStyle(2, 2).getRightBorderColor());
}
@Test
public void setLeftBorderColor() {
- RegionUtil.setLeftBorderColor(THIN, A1C3, sheet);
+ assertEquals(DEFAULT_COLOR, getCellStyle(0, 0).getLeftBorderColor());
+ assertEquals(DEFAULT_COLOR, getCellStyle(1, 0).getLeftBorderColor());
+ assertEquals(DEFAULT_COLOR, getCellStyle(2, 0).getLeftBorderColor());
+ RegionUtil.setLeftBorderColor(RED, A1C3, sheet);
+ assertEquals(RED, getCellStyle(0, 0).getLeftBorderColor());
+ assertEquals(RED, getCellStyle(1, 0).getLeftBorderColor());
+ assertEquals(RED, getCellStyle(2, 0).getLeftBorderColor());
+ }
+
+ @Test
+ public void bordersCanBeAddedToNonExistantCells() {
+ RegionUtil.setBorderTop(THIN, A1C3, sheet);
+ assertEquals(THIN, getCellStyle(0, 0).getBorderTopEnum());
+ assertEquals(THIN, getCellStyle(0, 1).getBorderTopEnum());
+ assertEquals(THIN, getCellStyle(0, 2).getBorderTopEnum());
+ }
+ @Test
+ public void borderColorsCanBeAddedToNonExistantCells() {
+ RegionUtil.setTopBorderColor(RED, A1C3, sheet);
+ assertEquals(RED, getCellStyle(0, 0).getTopBorderColor());
+ assertEquals(RED, getCellStyle(0, 1).getTopBorderColor());
+ assertEquals(RED, getCellStyle(0, 2).getTopBorderColor());
}
}