Finding Absolute Position of Shape inside the Worksheet with JavaScript via C++
Contents
[
Hide
]
Sometimes, you need to know the absolute position of a shape in a worksheet. Aspose.Cells for JavaScript via C++ provides the Shape.leftToCorner and Shape.topToCorner properties for this purpose. These properties return the absolute position of the shape inside the worksheet in pixels.
The following sample code displays the absolute position of the first shape in the worksheet in pixels. The sample code displays the following console output:
Absolute Position of this Shape is (320 , 183)
<!DOCTYPE html>
<html>
<head>
<title>Aspose.Cells Example - Shape Position</title>
</head>
<body>
<h1>Get Shape Absolute Position</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, Utils } = 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 resultDiv = document.getElementById('result');
if (!fileInput.files.length) {
resultDiv.innerHTML = '<p style="color: red;">Please select an Excel file.</p>';
return;
}
const file = fileInput.files[0];
const arrayBuffer = await file.arrayBuffer();
// Load the sample Excel file inside the workbook object
const workbook = new Workbook(new Uint8Array(arrayBuffer));
// Access the first worksheet
const worksheet = workbook.worksheets.get(0);
// Access the first shape inside the worksheet
const shape = worksheet.shapes.get(0);
// Displays the absolute position of the shape
const left = shape.leftToCorner;
const top = shape.topToCorner;
const message = `Absolute Position of this Shape is (${left} , ${top})`;
console.log(message);
resultDiv.innerHTML = `<p>${message}</p>`;
});
</script>
</html>