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

可能的使用场景

如果你为 Style.setCustom(string) 方法分配了无效的自定义数字格式,Aspose.Cells for Node.js via C++ 不会抛出任何异常。但如果你希望 Aspose.Cells 检查所分配的自定义数字格式是否有效,请将 Workbook.getSettings().setCheckCustomNumberFormat(boolean) 方法设置为 true

在调用 Style.setCustom(string) 方法时检查自定义数字格式

以下示例代码为 Style.setCustom(string) 方法分配了无效的自定义数字格式。由于我们已将 Workbook.getSettings().setCheckCustomNumberFormat(boolean) 方法设置为 true,因此会抛出异常,例如无效的数字格式。请阅读代码内的注释以获取更多帮助。

示例代码

const path = require("path");
const AsposeCells = require("aspose.cells.node");
// Create an instance of Workbook class
const book = new AsposeCells.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.getSettings().setCheckCustomNumberFormat(true);
// Access first worksheet
const sheet = book.getWorksheets().get(0);
// Access cell A1 and put some number to it
const cell = sheet.getCells().get("A1");
cell.putValue(2347);
// Access cell's style and set its Style.Custom property
const style = cell.getStyle();
// This line will throw exception if Workbook.Settings.CheckCustomNumberFormat is set to true
style.setCustom("ggg @ fff"); //Invalid custom number format