Kontrollera Custom Number Format när du ställer in Style.Custom Property

Möjliga användningsscenarier

Om du tilldelar ogiltigt anpassat nummerformat tillStyle.Customegendom så kommer Aspose.Cells inte att ge något undantag. Men om du vill att Aspose.Cells ska kontrollera om det tilldelade anpassade nummerformatet är giltigt eller inte, ställ inWorkbook.Settings.CheckCustomNumberFormat egendom tillSann.

Kontrollera Custom Number Format när du ställer in Style.Custom-egenskapen

Följande exempelkod tilldelar ett ogiltigt anpassat nummerformat tillStyle.Custom fast egendom. Eftersom vi redan har sattWorkbook.Settings.CheckCustomNumberFormat egendom tillSann , därför kommer API att kasta CellsException t.exOgiltigt nummerformat.

Exempelkod

// 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");