Actualizar valores de formas vinculadas con JavaScript a través de C++
Contents
[
Hide
]
A veces, tienes una forma vinculada en tu archivo de Excel que está vinculada a una celda. En Microsoft Excel, cambiar el valor de la celda vinculada también cambia el valor de la forma vinculada. Esto también funciona bien con Aspose.Cells for JavaScript vía C++ si deseas guardar tu libro en formato XLS o XLSX. Sin embargo, si quieres guardar tu libro en formato PDF o HTML, entonces tendrás que llamar al método ShapeCollection.updateSelectedValue() para actualizar el valor de la forma vinculada.
Ejemplo
La siguiente captura muestra el archivo Excel fuente usado en el ejemplo de código a continuación. Tiene una imagen enlazada a las celdas A1 a E4. Cambiaremos el valor de la celda B4 con Aspose.Cells y luego llamaremos al método ShapeCollection.updateSelectedValue() para actualizar el valor de la imagen y guardarla en formato PDF.

Puedes descargar el archivo Excel fuente y el PDF de salida desde los enlaces proporcionados.
Código JavaScript para actualizar los valores de las formas vinculadas
<!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>