1 WorkBook
WorkBook Class represent a full Excel SpreadSheet.
Return | Method | Description |
---|---|---|
WorkBook | Ax.ms.Excel("Workbook1") | Returns a new blank Excel SpreadSheet Object |
WorkBook | Ax.ms.Excel.load(Ax.io.File) | Loads an file containing an Excel WorkBook and returns WorkBook object |
WorkBook | evaluate() | Evaluate all formulas in WorkBook |
JSBlob | toBlob() | Return excel worbook into a Blob |
Sheet | getSheet(int sheetid) | Return Sheet Object in position sheetid (Starts at 0) |
Sheet | getSheet(String name) | Return Sheet Object searching by name |
Integer | getNumberOfSheets() | Return Number of Sheets of WorkBook |
JSSheet | createSheet(String SheetName) | Creates a new Sheet at the end of existing ones and name it. |
JSDiffAssert | diff(String Reason, WorkBook wb2) | Compares current WorkBook with other. |
Void | setLogger(Logger) | Configure logger to use to show debug data in WorkBook processing. E.G. wb.setLogger(console.getLogger()); |
2 Creating a Workbook
Yo create an empry Workbook
simply call the Workbook
constructor with its name. By default,
the XSSF implementation is used (xlsx).
<script> var xls = new Ax.ms.Excel("Workbook1"); </script>
3 Loading a Workbook
You can load a Workbook
from an input file resource or load from any input stream resource like http, https, blob, or even base64 encoded.
<script> var wb = Ax.ms.Excel.load("https://bitbucket.org/deister/axional-docs-resources/raw/master/Excel/Sample-sales.xls"); console.log(wb.getName()); </script>
Sample-sales.xls
4 Iterate through sheets
Workbook
implements Iterable
interface.
<script> var wb = Ax.ms.Excel.load("https://bitbucket.org/deister/axional-docs-resources/raw/master/Excel/Sample-sales.xls"); console.log("Num sheets: " + wb.getNumberOfSheets()); console.log("----- Iterating -----"); for (var sheet of wb) { console.log(sheet.getName()); } </script>
Num sheets: 3
----- Iterating -----
Sales Data
Product Ledger
Country Groupings
5 Get a sheet
You can get a workbook sheet by name or index. For example:
- To get a sheet by name use
getSheet(String name)
method. - To get a sheet by index use
getSheet(int index)
method.
<script> var wb = Ax.ms.Excel.load("https://bitbucket.org/deister/axional-docs-resources/raw/master/Excel/Sample-sales.xls"); console.log("Num sheets: " + wb.getNumberOfSheets()); console.log("----- For loop -----"); for (var idx = 0; idx < wb.getNumberOfSheets(); idx++) { var sheet = wb.getSheet(idx); console.log(sheet.getName()); } </script>
Num sheets: 3
----- For loop -----
Sales Data
Product Ledger
Country Groupings
6 Create a sheet
You can create a sheet on an existing Workbook
by using the createSheet
method.
<script> var wb = new Ax.ms.Excel("Sample.xlsx"); var sheet1 = wb.createSheet("sheet1"); </script>
7 Clone a sheet
You can create a sheet on an existing Workbook
by using the createSheet
method.
<script> var wb = new Ax.ms.Excel("Sample.xlsx"); var sheet1 = wb.createSheet("sheet1"); sheet1.setCellValue("A1", 10); sheet1.setCellFormula("A2", "A1*100"); var sheet2 = wb.cloneSheet("sheet1").setSheetName("sheet2"); sheet2.setCellFormula("A3", "A2*100"); return wb; </script>
8 Getting cells by name
By using names, you can make your formulas much easier to understand and maintain. You can define a name for a cell range, function, constant, or table. Once you adopt the practice of using names in your workbook, you can easily update, audit, and manage these names.
-
You can get a named Cell using
getCellByName(String name)
-
To set a cell name, you sould use
setCellName(String name)
on the cell object.
9 Creating fonts
createFont()
method creates a new Font and add it to the workbook's font table
10 Creating styles
createCellStyle()
method creates a new Cell style and add it to the workbook's style table. This may
cause an Exception if the number of cell styles exceeded the limit for this type of Workbook
.
11 Embedded objects
TO DO
This section is incomplete and will be concluded as soon as possible.12 Comparing Excel workBooks
To compare two Excel workbooks you can use diff method:
Return | Method | Description |
---|---|---|
Array<String> | diff(Workbook other) |
Return a string array with differences between workbooks or null if both workbooks are equal |
<script> var wb1 = new Ax.ms.Excel("Excel1.xlsx"); var sheet1 = wb1.createSheet("Data"); sheet1.setCellValue("A1", 10); sheet1.setCellValue("A2", 20); sheet1.setCellValue("A3", 30); var wb2 = new Ax.ms.Excel("Excel2.xlsx"); var sheet2 = wb2.createSheet("Data"); sheet2.setCellValue("A1", 15); sheet2.setCellValue("A2", 25); sheet2.setCellValue("A3", 30); console.log("Comparing workbooks " + wb1 + " vs " + wb2); var assertDiff = wb1.diff("Reason", wb2); if (assertDiff.length > 0) { for (var msg of assertDiff) { console.log(msg); } // console.log(assertDiff.toString()); } else { console.log("Both workbooks are equal"); } </script>
Comparing workbooks deister.axional.server.dbstudio.script.js.ax.ms.Excel.JSExcelWorkbook@70d60a20 vs deister.axional.server.dbstudio.script.js.ax.ms.Excel.JSExcelWorkbook@33ed2f4
Cell Data does not Match ::
workbook1 -> Data -> A1 [10.0] != workbook2 -> Data -> A1 [15.0]
Cell Data does not Match ::
workbook1 -> Data -> A2 [20.0] != workbook2 -> Data -> A2 [25.0]