セル値の幅と高さをピクセル単位で計測します
Contents
[
Hide
]
セル値の幅と高さを計算してセル内に収まるようにする必要がある場合があります。Aspose.Cells ではこの目的のために Cell.widthOfValue および Cell.heightOfValue のメソッドを提供しています。これらのメソッドを使用することで、セル値の幅と高さを計算し、そのセルの列の幅と行の高さをそれぞれ設定し、これによりセル値を調整またはセル内に収めることができます。
または、Aspose.Cells APIを使用してセルまたはセル範囲の高さと幅を自動調整することも可能です。[/cells/ja/javascript-cpp/autofit-rows-and-columns/]を参照してください。
Cell.widthOfValueとCell.heightOfValueメソッドの使用例を次のコードで説明します。
<!DOCTYPE html>
<html>
<head>
<title>Aspose.Cells Example</title>
</head>
<body>
<h1>Get Cell Size Example</h1>
<input type="file" id="fileInput" accept=".xls,.xlsx,.csv" />
<button id="runExample">Run Example</button>
<a id="downloadLink" style="display: none;">Download Result</a>
<div id="result"></div>
</body>
<script src="aspose.cells.js.min.js"></script>
<script type="text/javascript">
const { Workbook, SaveFormat } = AsposeCells;
AsposeCells.onReady({
license: "/lic/aspose.cells.enc",
fontPath: "/fonts/",
fontList: [
"arial.ttf",
"NotoSansSC-Regular.ttf"
]
}).then(() => {
console.log("Aspose.Cells initialized");
});
document.getElementById('runExample').addEventListener('click', async () => {
const fileInput = document.getElementById('fileInput');
// Create or load workbook
let workbook;
if (fileInput.files.length) {
const file = fileInput.files[0];
const arrayBuffer = await file.arrayBuffer();
workbook = new Workbook(new Uint8Array(arrayBuffer));
} else {
workbook = new Workbook();
}
// Access first worksheet
const worksheet = workbook.worksheets.get(0);
// Access cell B2 and add some value inside it
const cell = worksheet.cells.get("B2");
cell.value = "Welcome to Aspose!";
// Enlarge its font to size 16
const style = cell.style;
style.font.size = 16;
cell.style = style;
// Calculate the width and height of the cell value in unit of pixels
const widthOfValue = cell.widthOfValue;
const heightOfValue = cell.heightOfValue;
// Print both values to the page
document.getElementById('result').innerHTML = `<p>Width of Cell Value: ${widthOfValue}</p><p>Height of Cell Value: ${heightOfValue}</p>`;
// Set the row height and column width to adjust/fit the cell value inside cell
worksheet.cells.setColumnWidthPixel(1, widthOfValue);
worksheet.cells.setRowHeightPixel(1, heightOfValue);
// Save the output excel file and provide download link
const outputData = workbook.save(SaveFormat.Xlsx);
const blob = new Blob([outputData]);
const downloadLink = document.getElementById('downloadLink');
downloadLink.href = URL.createObjectURL(blob);
downloadLink.download = 'output_out.xlsx';
downloadLink.style.display = 'block';
downloadLink.textContent = 'Download Excel File';
});
</script>
</html>