Get an Array of Values from ICustomFunction & Calculate the Width and Height of the Cell Value using Aspose.Cells for Java 8.5.1

Share on FacebookTweet about this on TwitterShare on LinkedIn

Aspose.Cells for JavaWe are pleased to announce that Aspose.Cells for Java 8.5.1 is now available for public use. This month’s release contains many useful features and improvements along with some critical bug fixes. Please refer to the release notes of Aspose.Cells for Java 8.5.1 for a full list of enhancements. If you are planning to upgrade the API from any previous version, we strongly suggest you to check the Public API Changes section to know what has been changed since your current revision of the API.

While you are downloading the latest build, here is a look at the biggest features in this release.

Get a Range of Values using ICustomFunction

Aspose.Cells APIs provide the ICustomFunction interface which can be used to implement the user defined or custom functions that are not supported by Microsoft Excel as built-in functions. While implementing the ICustomFunction interface, most of the times we require to return a single cell value however, sometimes we may also need to return a range of values.

Following code snippet demonstrates how to return a range of values from ICustomFunction.


public class CustomFunctionStaticValue implements ICustomFunction 
{
	public Object calculateCustomFunction(String functionName, ArrayList paramsList, ArrayList contextObjects) {
		return new Object[][] {
				new Object[] { new DateTime(2015, 6, 12, 10, 6, 30), 2 },
				new Object[] { 3.0, "Test" } };
	}
}

//-------------------
//   Main Method
//-------------------

Workbook wb = new Workbook();
Cells cells = wb.getWorksheets().get(0).getCells();

Cell cell = cells.get(0, 0);
cell.setArrayFormula("=MYFUNC()", 2, 2);

Style style = cell.getStyle();
style.setNumber(14);
cell.setStyle(style);

CalculationOptions copt = new CalculationOptions();
copt.setCustomFunction(new CustomFunctionStaticValue());
wb.calculateFormula(copt);

//Save to xlsx by setting the calculation mode to manual
wb.getSettings().setCalcMode(CalcModeType.MANUAL);
wb.save("output.xlsx");

Calculate the Width and Height of the Cell Value

Aspose.Cells for Java provides Cell.getWidthOfValue and Cell.getHeightOfValue methods to calculate the width & height of the cell value in unit of pixels. These methods are useful in scenarios where application requirement is to set the width of the column and height of the row according to the size of cell value.

The following sample code explains the usage of Cell.getWidthOfValue and Cell.getHeightOfValue methods.


//Create workbook object
Workbook workbook = new Workbook();

//Access first worksheet
Worksheet worksheet = workbook.getWorksheets().get(0);

//Access cell B2 and add some value inside it
Cell cell = worksheet.getCells().get("B2");
cell.putValue("Welcome to Aspose!");

//Enlarge its font to size 16
Style style = cell.getStyle();
style.getFont().setSize(16);
cell.setStyle(style);

//Calculate the width and height of the cell value in unit of pixels
int widthOfValue = cell.getWidthOfValue();
int heightOfValue = cell.getHeightOfValue();

//Print both values
System.out.println("Width of Cell Value: " + widthOfValue);
System.out.println("Height of Cell Value: " + heightOfValue);

//Set the row height and column width to adjust/fit the cell value inside cell
worksheet.getCells().setColumnWidthPixel(1, widthOfValue);
worksheet.getCells().setRowHeightPixel(1, heightOfValue);

//Save the output excel file
workbook.save("output.xlsx");

Release Unmanaged Resources of Workbook

Aspose.Cells for Java 8.5.1 has exposed the Workbook.dispose method to release the unmanaged resources of the Workbook object. The dispose pattern is used only for objects that access unmanaged resources, such as file and pipe handles, registry handles, wait handles, or pointers to blocks of unmanaged memory. This is because the garbage collector is very efficient at reclaiming unused managed objects, but it is unable to reclaim unmanaged objects.

Detect the Data Source Type of ListObject

With this revision, the API has exposed the TableDataSourceType enumeration & readonly ListObject.DataSourceType property that can be used to detect the data source type of a ListObject.

The TableDataSourceType enumeration has the following fields.

  • TableDataSourceType.QUERY_TABLE
  • TableDataSourceType.XML
  • TableDataSourceType.WORKSHEET
  • TableDataSourceType.SHARE_POINT

Other Enhancements & Improvements

The most notable  enhancements in this release are as follow:

  • Improved the PDF rendering for text boxes, markers & arrows.
  • Tweaked calculate engine for handling custom functions & built-in TREND function.
  • Improved the Chart2Image module.
  • Handled ArrayIndexOutOfBoundsException triggered while saving some specific spreadsheets with Aspose.Cells API.

Please visit the documentation for details, and if you still have any questions, we always welcome inquiries on Aspose.Cells Support Forum.