]> source.dussan.org Git - poi.git/commitdiff
Bug 64494: Ensure "applyAlignment" in cell-styles is enabled when necessary
authorDominik Stadler <centic@apache.org>
Sun, 3 Jan 2021 12:01:08 +0000 (12:01 +0000)
committerDominik Stadler <centic@apache.org>
Sun, 3 Jan 2021 12:01:08 +0000 (12:01 +0000)
Also check it when fetching currently defined cell-alignment

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1885059 13f79535-47bb-0310-9956-ffa450edef68

src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCellStyle.java
src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFBugs.java

index 0bebc2d1196d2019d1dcd975f9084dc3620c358c..b5ec7f2fc71b95cfe861cf3047c3503123cf193a 100644 (file)
@@ -222,6 +222,8 @@ public class XSSFCellStyle implements CellStyle, Duplicatable {
 
     @Override
     public HorizontalAlignment getAlignment() {
+        if(!_cellXf.getApplyAlignment()) return HorizontalAlignment.GENERAL;
+
         CTCellAlignment align = _cellXf.getAlignment();
         if(align != null && align.isSetHorizontal()) {
             return HorizontalAlignment.forInt(align.getHorizontal().intValue()-1);
@@ -629,6 +631,8 @@ public class XSSFCellStyle implements CellStyle, Duplicatable {
 
     @Override
     public VerticalAlignment getVerticalAlignment() {
+        if(!_cellXf.getApplyAlignment()) return VerticalAlignment.BOTTOM;
+
         CTCellAlignment align = _cellXf.getAlignment();
         if(align != null && align.isSetVertical()) {
             return VerticalAlignment.forInt(align.getVertical().intValue()-1);
@@ -654,6 +658,8 @@ public class XSSFCellStyle implements CellStyle, Duplicatable {
      */
     @Override
     public void setAlignment(HorizontalAlignment align) {
+        _cellXf.setApplyAlignment(true);
+
         getCellAlignment().setHorizontal(align);
     }
 
@@ -1155,6 +1161,8 @@ public class XSSFCellStyle implements CellStyle, Duplicatable {
      * @param align - the type of alignment
      */
     public void setVerticalAlignment(VerticalAlignment align) {
+        _cellXf.setApplyAlignment(true);
+
         getCellAlignment().setVertical(align);
     }
 
index 7f17019844027a396e1ca8fb893e0661dc581944..034cdf1613562a3ae64e1e4bb5c658e7841c33e3 100644 (file)
@@ -3630,7 +3630,7 @@ public final class TestXSSFBugs extends BaseTestBugzillaIssues {
     }
 
     @Test
-    public void test64986() throws IOException {
+    public void test64986() {
         XSSFWorkbook w = new XSSFWorkbook();
         XSSFSheet s = w.createSheet();
         XSSFRow r = s.createRow(0);
@@ -3667,4 +3667,37 @@ public final class TestXSSFBugs extends BaseTestBugzillaIssues {
             assertNotNull(wb);
         }
     }
+
+    @Test
+    public void test64494() throws IOException {
+        try (Workbook wb = new XSSFWorkbook()) {
+            CellStyle styleRight = wb.createCellStyle();
+            CellStyle styleLeft = wb.createCellStyle();
+            styleRight.setAlignment(HorizontalAlignment.RIGHT);
+            //styleRight.setBorderBottom(BorderStyle.DASH_DOT);
+            styleLeft.setAlignment(HorizontalAlignment.LEFT);
+            //styleLeft.setBorderRight(BorderStyle.MEDIUM);
+
+            assertEquals(HorizontalAlignment.RIGHT, styleRight.getAlignment());
+            assertEquals(HorizontalAlignment.LEFT, styleLeft.getAlignment());
+
+            Sheet sheet = wb.createSheet("test");
+            Row row = sheet.createRow(0);
+
+            Cell cellRight = row.createCell(0);
+            cellRight.setCellValue("R");
+            cellRight.setCellStyle(styleRight);
+
+            Cell cellLeft = row.createCell(1);
+            cellLeft.setCellValue("L");
+            cellLeft.setCellStyle(styleLeft);
+
+            /*try (OutputStream out = new FileOutputStream("/tmp/64494.xlsx")) {
+                wb.write(out);
+            }*/
+
+            assertEquals(HorizontalAlignment.RIGHT, cellRight.getCellStyle().getAlignment());
+            assertEquals(HorizontalAlignment.LEFT, cellLeft.getCellStyle().getAlignment());
+        }
+    }
 }