获取单元值的文本宽度

获取单元值的文本宽度

有时,开发者可能需要计算单元格值的宽度以安排布局。Aspose.Cells for Node.js via C++提供 CellsHelper.getTextWidth(string, Font, number) 方法,允许开发者获取单元格值的文本宽度。以下示例代码演示了如何使用 CellsHelper.getTextWidth(string, Font, number) 访问单元格值的文本宽度。

以下代码片段中使用的源文件,请参考附件。

源文件

示例代码

const AsposeCells = require("aspose.cells.node");
const path = require("path");
// The path to the documents directory.
const sourceDir = path.join(__dirname, "data");
let workbook = new AsposeCells.Workbook(sourceDir + "GetTextWidthSample.xlsx");
console.log("Text width: " + AsposeCells.CellsHelper.getTextWidth(workbook.getWorksheets().get(0).getCells().get("A1").getStringValue(), workbook.getDefaultStyle().getFont(), 1));