From 0ef1ee7454e83c1790cf9b57379b0f4c6987bf36 Mon Sep 17 00:00:00 2001 From: elizondogenexus Date: Tue, 10 Jan 2023 11:34:15 -0300 Subject: [PATCH] Add method getUnderlyingObject which returns the internal xssf object for the workbook and the cells in excel spreadsheet --- .../excel/ExcelSpreadsheetGXWrapper.java | 5 +++ .../msoffice/excel/IExcelSpreadsheet.java | 3 ++ .../msoffice/excel/poi/xssf/ExcelCells.java | 5 +++ .../excel/poi/xssf/ExcelSpreadsheet.java | 4 +++ .../msoffice/excel/ExcelSpreadsheetTest.java | 31 +++++++++++++++++++ 5 files changed, 48 insertions(+) diff --git a/gxoffice/src/main/java/com/genexus/msoffice/excel/ExcelSpreadsheetGXWrapper.java b/gxoffice/src/main/java/com/genexus/msoffice/excel/ExcelSpreadsheetGXWrapper.java index a79272560..d575bbb08 100644 --- a/gxoffice/src/main/java/com/genexus/msoffice/excel/ExcelSpreadsheetGXWrapper.java +++ b/gxoffice/src/main/java/com/genexus/msoffice/excel/ExcelSpreadsheetGXWrapper.java @@ -11,6 +11,7 @@ import com.genexus.msoffice.excel.exception.ExcelTemplateNotFoundException; import com.genexus.msoffice.excel.poi.xssf.ExcelCells; import com.genexus.msoffice.excel.poi.xssf.ExcelWorksheet; +import org.apache.poi.xssf.usermodel.XSSFWorkbook; public class ExcelSpreadsheetGXWrapper implements IGXError { private static final ILogger logger = LogManager.getLogger(ExcelSpreadsheetGXWrapper.class); @@ -333,4 +334,8 @@ public void setErrDes(String arg0) { _errDescription = arg0; } + public XSSFWorkbook getUnderlyingObject(){ + return _document.getUnderlyingObject(); + + } } diff --git a/gxoffice/src/main/java/com/genexus/msoffice/excel/IExcelSpreadsheet.java b/gxoffice/src/main/java/com/genexus/msoffice/excel/IExcelSpreadsheet.java index 14bf70e56..06a56e230 100644 --- a/gxoffice/src/main/java/com/genexus/msoffice/excel/IExcelSpreadsheet.java +++ b/gxoffice/src/main/java/com/genexus/msoffice/excel/IExcelSpreadsheet.java @@ -4,6 +4,7 @@ import com.genexus.msoffice.excel.exception.ExcelException; import com.genexus.msoffice.excel.poi.xssf.ExcelWorksheet; +import org.apache.poi.xssf.usermodel.XSSFWorkbook; public interface IExcelSpreadsheet { @@ -49,4 +50,6 @@ public interface IExcelSpreadsheet boolean toggleRow(IExcelWorksheet _currentWorksheet, int i, Boolean visible); boolean cloneSheet(String sheetName, String newSheetName) throws ExcelException; + + XSSFWorkbook getUnderlyingObject(); } diff --git a/gxoffice/src/main/java/com/genexus/msoffice/excel/poi/xssf/ExcelCells.java b/gxoffice/src/main/java/com/genexus/msoffice/excel/poi/xssf/ExcelCells.java index 4faaf16e1..8987779dd 100644 --- a/gxoffice/src/main/java/com/genexus/msoffice/excel/poi/xssf/ExcelCells.java +++ b/gxoffice/src/main/java/com/genexus/msoffice/excel/poi/xssf/ExcelCells.java @@ -1110,4 +1110,9 @@ private void applyBorderSide(XSSFCellStyle cellStyle, BorderCellSide bSide, Exce public enum BorderCellSide { RIGHT, LEFT, TOP, BOTTOM, DIAGONALUP, DIAGONALDOWN } + + public XSSFCell[] getUnderlyingObject(){ + return pCells; + + } } diff --git a/gxoffice/src/main/java/com/genexus/msoffice/excel/poi/xssf/ExcelSpreadsheet.java b/gxoffice/src/main/java/com/genexus/msoffice/excel/poi/xssf/ExcelSpreadsheet.java index b6635a692..b2cd083b3 100644 --- a/gxoffice/src/main/java/com/genexus/msoffice/excel/poi/xssf/ExcelSpreadsheet.java +++ b/gxoffice/src/main/java/com/genexus/msoffice/excel/poi/xssf/ExcelSpreadsheet.java @@ -487,4 +487,8 @@ public boolean toggleRow(IExcelWorksheet worksheet, int i, Boolean visible) { return false; } + public XSSFWorkbook getUnderlyingObject(){ + return _workbook; + } + } diff --git a/gxoffice/src/test/java/com/genexus/msoffice/excel/ExcelSpreadsheetTest.java b/gxoffice/src/test/java/com/genexus/msoffice/excel/ExcelSpreadsheetTest.java index 220f49b2e..a92a83b08 100644 --- a/gxoffice/src/test/java/com/genexus/msoffice/excel/ExcelSpreadsheetTest.java +++ b/gxoffice/src/test/java/com/genexus/msoffice/excel/ExcelSpreadsheetTest.java @@ -10,6 +10,9 @@ import java.util.List; import com.genexus.msoffice.excel.style.ExcelStyle; +import org.apache.poi.xssf.usermodel.XSSFCell; +import org.apache.poi.xssf.usermodel.XSSFCellStyle; +import org.apache.poi.xssf.usermodel.XSSFSheet; import org.junit.*; import com.genexus.msoffice.excel.poi.xssf.ExcelCells; @@ -1079,6 +1082,32 @@ public void testDateFormat() { excel.close(); } + @Test + public void testgetUnderlyingObject(){ + ExcelSpreadsheetGXWrapper excel = create("testgetUnderlyingObject"); + org.apache.poi.xssf.usermodel.XSSFWorkbook workbook2 = excel.getUnderlyingObject(); + XSSFSheet testsheet = workbook2.createSheet("test sheet"); + workbook2.setActiveSheet(workbook2.getSheetIndex(testsheet)); + ExcelCells cells = excel.getCells(1,1,1,1); + XSSFCell underlyingCells = cells.getUnderlyingObject()[1]; + underlyingCells.setCellValue("test cell value"); + + //test using the same excel instance + Assert.assertEquals(excel.getCurrentWorksheet().getName().trim(),"test sheet"); + cells = excel.getCell(1,1); + Assert.assertEquals(cells.getText(),"test cell value"); + + excel.save(); + excel.close(); + + //test after saving and opening the file again + excel = open("testgetUnderlyingObject"); + Assert.assertEquals(excel.getCurrentWorksheet().getName().trim(),"test sheet"); + cells = excel.getCell(1,1); + Assert.assertEquals(cells.getText(),"test cell value"); + + } + private void logErrorCodes(ExcelSpreadsheetGXWrapper excel) { // System.out.println(String.format("%s - %s", excel.getErrCode(), excel.getErrDescription())); } @@ -1096,4 +1125,6 @@ private void ensureFileDoesNotExists(String path){ } } + + }