Misura la larghezza e l altezza del valore della cella in unità di pixel
A volte è necessario calcolare la larghezza e l’altezza del valore della cella per adattare il valore della cella all’interno della cella stessa. Aspose.Cells fornisce i seguenti metodi per questo scopo. Utilizzando questi metodi è possibile calcolare la larghezza e l’altezza del valore della cella e quindi impostare la larghezza della colonna e l’altezza della riga di quella cella rispettivamente e questo regolerà o adatterà il valore della cella all’interno della cella.
In alternativa, puoi anche adattare automaticamente righe e colonne della tua cella o intervallo di celle usando le API di Aspose.Cells.
Il codice seguente spiega l’uso dei metodi Cell.getWidthOfValue() e Cell.getHeightOfValue().
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")); |