セル値の幅と高さをピクセル単位で計測します
Contents
[
Hide
]
セル値の幅と高さを計算してセル内に収まるようにする必要がある場合があります。Aspose.Cells ではこの目的のために Cell.getWidthOfValue() および Cell.getHeightOfValue() のメソッドを提供しています。これらのメソッドを使用することで、セル値の幅と高さを計算し、そのセルの列の幅と行の高さをそれぞれ設定し、これによりセル値を調整またはセル内に収めることができます。
また、Aspose.Cells APIを使用してセルまたはセル範囲の自動調整も可能です。
Cell.getWidthOfValue()とCell.getHeightOfValue()メソッドの使用例を次のコードで説明します。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const path = require("path"); | |
const AsposeCells = require("aspose.cells.node"); | |
// The path to the documents directory. | |
const dataDir = path.join(__dirname, "data"); | |
// Create workbook object | |
let workbook = new AsposeCells.Workbook(); | |
// Access first worksheet | |
let worksheet = workbook.getWorksheets().get(0); | |
// Access cell B2 and add some value inside it | |
let cell = worksheet.getCells().get("B2"); | |
cell.putValue("Welcome to Aspose!"); | |
// Enlarge its font to size 16 | |
let style = cell.getStyle(); | |
style.getFont().setSize(16); | |
cell.setStyle(style); | |
// Calculate the width and height of the cell value in unit of pixels | |
let widthOfValue = cell.getWidthOfValue(); | |
let heightOfValue = cell.getHeightOfValue(); | |
// Print both values | |
console.log("Width of Cell Value: " + widthOfValue); | |
console.log("Height of Cell Value: " + heightOfValue); | |
// Set the row height and column width to adjust/fit the cell value inside cell | |
worksheet.getCells().setColumnWidthPixel(1, widthOfValue); | |
worksheet.getCells().setRowHeightPixel(1, heightOfValue); | |
// Save the output excel file | |
workbook.save(path.join(dataDir, "output_out.xlsx")); |