Check Custom Number Format when Setting Style.Custom Property

Possible Usage Scenarios

If you assign invalid custom number format to Style.Custom property then Aspose.Cells will not throw any exception. But if you want that Aspose.Cells should check if the assigned custom number format is valid or not then please set the Workbook.Settings.CheckCustomNumberFormat property to true.

Check the Custom Number Format when setting Style.Custom property

The following sample code assigns an invalid custom number format to Style.Custom property. Since we have already set Workbook.Settings.CheckCustomNumberFormat property to true, therefore the API will throw  CellsException e.g. Invalid number format.

Sample Code

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// Create a workbook
Workbook wb = new Workbook();
// Setting this property to true will make Aspose.Cells to throw exception
// when invalid custom number format is assigned to Style.Custom property
wb.getSettings().setCheckCustomNumberFormat(true);
// Access first worksheet
Worksheet ws = wb.getWorksheets().get(0);
// Access cell A1 and put some number inside it
Cell c = ws.getCells().get("A1");
c.putValue(2347);
// Access cell's style and set its Style.Custom property
Style s = c.getStyle();
try {
// This line will throw exception if
// Workbook.Settings.CheckCustomNumberFormat is set to true
s.setCustom("ggg @ fff"); // Invalid custom number format
c.setStyle(s);
}
catch (Exception ex) {
System.out.println("Exception Occured");
}
System.out.println("Done");