在设置Style.Custom属性时检查自定义数字格式

可能的使用场景

如果将无效的自定义数字格式分配给 Style.custom 属性,则Aspose.Cells不会抛出任何异常。但如果您希望Aspose.Cells检查分配的自定义数字格式是否有效,则请将 Workbook.settings.check_custom_number_format 属性设置为true

设置 Style.Custom 属性时检查自定义数字格式

以下示例代码向 Style.custom 属性分配了一个无效的自定义数字格式。由于我们已将 Workbook.settings.check_custom_number_format 属性设置为 true,因此会抛出异常,例如无效的数字格式。请阅读代码内的注释以获取更多帮助。

示例代码

from aspose.cells import Workbook
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET
# Create an instance of Workbook class
book = Workbook()
# Setting this property to true will make Aspose.Cells to throw exception
# when invalid custom number format is assigned to Style.Custom property
book.settings.check_custom_number_format = True
# Access first worksheet
sheet = book.worksheets[0]
# Access cell A1 and put some number to it
cell = sheet.cells.get("A1")
cell.put_value(2347)
# Access cell's style and set its Style.Custom property
style = cell.get_style()
# This line will throw exception if Workbook.Settings.CheckCustomNumberFormat is set to true
style.custom = "ggg @ fff"