Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.
Fitting a picture exactly to a single cell is a common requirement when designing spreadsheets that act as visual reports, product catalogs, employee directories, dashboards, or inventory lists. Rather than stretching an image across many cells or placing it loosely on a worksheet, you may want a clean, cell-bound image that stays aligned with the cell that owns it. Aspose.Cells supports this scenario in two complementary ways:
Picture to the worksheet, set its placement with picture.setPlacement(PlacementType.MoveAndSize), and adjust its anchor cells through picture.setUpperLeftRow(), picture.setUpperLeftColumn(), picture.setLowerRightRow(), and picture.setLowerRightColumn() so the picture covers exactly one cell.Buffer containing the image data to the cell with cell.setEmbeddedImage(imageBuffer). The image automatically scales to fit the cell’s display area and travels with the cell.
The rest of this article walks through both approaches, explains the relevant APIs, and shows how to use them in code.A floating picture is a Picture object that lives on the worksheet drawing layer. Although it is not part of any single cell, it is anchored to a cell range. The picture’s anchor cells — its upper-left and lower-right corners — determine its visual extent on the worksheet. By default, a freshly added picture spans several cells.
To make a floating picture cover exactly one cell, you need to:
worksheet.getPictures().add(row, column, imageBuffer), where imageBuffer is a Buffer containing the image data. This anchors the new picture to the given cell.picture.setPlacement(PlacementType.MoveAndSize) so the picture moves and resizes with the underlying cell when the user changes the column width or row height.The picture’s anchor is defined by four zero-based index accessors:
picture.getUpperLeftRow() and picture.setUpperLeftRow(value) — the row index of the picture’s top edge.picture.getUpperLeftColumn() and picture.setUpperLeftColumn(value) — the column index of the picture’s left edge.picture.getLowerRightRow() and picture.setLowerRightRow(value) — the row index of the picture’s bottom edge. To make the picture’s bottom edge sit at the bottom of row r, set this to r + 1.picture.getLowerRightColumn() and picture.setLowerRightColumn(value) — the column index of the picture’s right edge. To make the picture’s right edge sit at the right of column c, set this to c + 1.picture.getPlacement() and picture.setPlacement(value) use the PlacementType enum to control how the picture behaves when the user resizes the row or column beneath it. The recommended value for a single-cell picture is PlacementType.MoveAndSize, which causes the picture to move and resize together with its underlying cell, preserving the exact fit.
Workbook (or open an existing one).Worksheet through workbook.getWorksheets().getAt(0).Buffer, for example by using Node.js fs.readFileSync.worksheet.getPictures().add(5, 2, imageBuffer) to add a picture anchored to cell C6. The method returns the index of the new picture in the Pictures collection; use it to retrieve the corresponding Picture.picture.setUpperLeftRow(5), picture.setUpperLeftColumn(2), picture.setLowerRightRow(6), and picture.setLowerRightColumn(3).picture.setPlacement(PlacementType.MoveAndSize) to keep the picture aligned with C6 when the column or row is resized..xlsx file.
The following code demonstrates the complete approach.<!DOCTYPE html>
<html>
<head>
<title>Aspose.Cells Example</title>
</head>
<body>
<h1>Aspose.Cells Example</h1>
<input type="file" id="fileInput" accept=".png,image/png" />
<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');
const file = fileInput.files[0];
if (!file) {
document.getElementById('result').innerHTML =
'<p style="color: red;">Please select a PNG image.</p>';
return;
}
const workbook = new Workbook();
const worksheet = workbook.worksheets.get(0);
const imageData = new Uint8Array(await file.arrayBuffer());
const picIndex = worksheet.pictures.add(5, 2, imageData);
const picture = worksheet.pictures.get(picIndex);
picture.upperLeftRow = 5;
picture.upperLeftColumn = 2;
picture.lowerRightRow = 6;
picture.lowerRightColumn = 3;
picture.placement = AsposeCells.Drawing.PlacementType.MoveAndSize;
const outputData = workbook.save(SaveFormat.Xlsx);
const blob = new Blob([outputData]);
const downloadLink = document.getElementById('downloadLink');
downloadLink.href = URL.createObjectURL(blob);
downloadLink.download = "output.xlsx";
downloadLink.style.display = "block";
document.getElementById('result').innerHTML =
'<p style="color: green;">Done! Click the download link to get the result file.</p>';
});
</script>
</html>
Aspose.Cells also exposes a simpler mechanism for cell-bound images: the cell.setEmbeddedImage(imageBuffer) method. Assigning image data with this method attaches the image to the cell itself, as if it were inline content.
cell.setEmbeddedImage(imageBuffer) the most concise option when your goal is simply “an image that lives inside this cell.”Workbook (or open an existing one).Worksheet through workbook.getWorksheets().getAt(0).Buffer, for example by using Node.js fs.readFileSync.worksheet.getCells().get("C6") or worksheet.getCells().get(5, 2).Buffer to the cell with cell.setEmbeddedImage(imageBuffer)..xlsx file.
The following code demonstrates the complete approach.<!DOCTYPE html>
<html>
<head>
<title>Aspose.Cells Example</title>
</head>
<body>
<h1>Aspose.Cells Example</h1>
<input type="file" id="fileInput" accept=".png,.jpg,.jpeg" />
<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 } = AsposeCells;
const asposeReady = 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');
const downloadLink = document.getElementById('downloadLink');
const result = document.getElementById('result');
if (!fileInput.files || fileInput.files.length === 0) {
result.innerHTML = '<p style="color: red;">Please select an image file.</p>';
return;
}
const workbook = new Workbook();
const worksheet = workbook.worksheets.get(0);
// Get the target cell C6
const cell = worksheet.cells.get("C6");
// Read the image file into a byte array
const imageData = new Uint8Array(await fileInput.files[0].arrayBuffer());
// Embed the image directly into the cell
cell.embeddedImage = imageData;
// Optionally adjust row height and column width so the embedded image is more visible
worksheet.cells.setColumnWidth(2, 30); // Column C (index 2)
worksheet.cells.setRowHeight(5, 100); // Row 6 (index 5)
// Save the resulting workbook as an .xlsx file
const outputData = workbook.save(AsposeCells.SaveFormat.Xlsx);
const blob = new Blob([outputData]);
downloadLink.href = URL.createObjectURL(blob);
downloadLink.download = "output.xlsx";
downloadLink.style.display = "block";
result.innerHTML = '<p style="color: green;">Done! Click the download link to get the result file.</p>';
});
</script>
</html>
Both approaches produce a picture that fits inside a single cell, but they differ in how the picture is stored and how it behaves:
Pictures collection accessed through worksheet.getPictures().Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.