Refresh Values of Linked Shapes with JavaScript via C++
Contents
[
Hide
]
Sometimes, you have a linked shape in your Excel file which is linked to some cell. In Microsoft Excel, changing the value of the linked cell also changes the value of the linked shape. This also works fine with Aspose.Cells for JavaScript via C++ if you want to save your workbook in XLS or XLSX format. However, if you want to save your workbook in PDF or HTML format, then you will have to call ShapeCollection.updateSelectedValue() method to refresh the value of the linked shape.
Example
The following screenshot shows the source Excel file used in the sample code below. It has a linked picture linked to cells A1 to E4. We will change the value of cell B4 with Aspose.Cells and then call ShapeCollection.updateSelectedValue() method to refresh the value of the picture and save it in PDF format.
You can download the source Excel file and the output PDF from the given links.
JavaScript code to refresh the values of linked shapes
<!DOCTYPE html>
<html>
<head>
<title>Refresh Value Of Linked Shapes Example</title>
</head>
<body>
<h1>Refresh Value Of Linked Shapes 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');
if (!fileInput.files.length) {
document.getElementById('result').innerHTML = '<p style="color: red;">Please select an Excel file.</p>';
return;
}
const file = fileInput.files[0];
const arrayBuffer = await file.arrayBuffer();
// Create workbook from uploaded file
const workbook = new Workbook(new Uint8Array(arrayBuffer));
// Access first worksheet
const worksheet = workbook.worksheets.get(0);
// Change the value of cell B4
const cell = worksheet.cells.get("B4");
cell.value = 100;
// Update the value of the Linked Picture which is linked to cell B4
worksheet.shapes.updateSelectedValue();
// Save the workbook in PDF format
const outputData = workbook.save(SaveFormat.Pdf);
const blob = new Blob([outputData]);
const downloadLink = document.getElementById('downloadLink');
downloadLink.href = URL.createObjectURL(blob);
downloadLink.download = 'outputRefreshValueOfLinkedShapes.pdf';
downloadLink.style.display = 'block';
downloadLink.textContent = 'Download PDF File';
document.getElementById('result').innerHTML = '<p style="color: green;">Operation completed successfully! Click the download link to get the PDF.</p>';
});
</script>
</html>