---
title: Agregar Partes XML Personalizadas y Seleccionarlas por ID con JavaScript vía C++
linktitle: Agregar partes XML personalizadas y seleccionarlas por ID
type: docs
weight: 70
url: /es/javascript-cpp/add-custom-xml-parts-and-select-them-by-id/
description: Aprende cómo agregar partes XML personalizadas a documentos de Excel y seleccionarlas por ID usando Aspose.Cells for JavaScript vía C++.
---
## **Escenarios de uso posibles**
Las partes XML personalizadas son los datos XML almacenados dentro de los documentos de Microsoft Excel y son utilizados por las aplicaciones que trabajan con ellos. Actualmente no hay una forma directa de agregarlas usando la interfaz de Excel de Microsoft. Sin embargo, puedes agregarlas programáticamente de varias maneras, por ejemplo, usando VSTO, Aspose.Cells, etc. Utiliza la colección [**Workbook.customXmlParts**](https://reference.aspose.com/cells/javascript-cpp/workbook/#customXmlParts--) si deseas agregar una parte XML personalizada usando la API de Aspose.Cells. También puedes establecer su ID usando la propiedad [**CustomXmlPart.iD**](https://reference.aspose.com/cells/javascript-cpp/customxmlpart/#iD--). De manera similar, si deseas seleccionar una parte XML personalizada por ID, puedes usar la colección [**Workbook.customXmlParts**](https://reference.aspose.com/cells/javascript-cpp/workbook/#customXmlParts--).
## **Agregar partes XML personalizadas y seleccionarlas por ID**
El siguiente código de ejemplo primero agrega cuatro partes XML personalizadas usando la colección [**Workbook.customXmlParts**](https://reference.aspose.com/cells/javascript-cpp/workbook/#customXmlParts--). Luego establece sus IDs usando la propiedad [**CustomXmlPart.iD**](https://reference.aspose.com/cells/javascript-cpp/customxmlpart/#iD--). Finalmente, encuentra o selecciona una de las partes XML personalizadas agregadas usando la colección [**Workbook.customXmlParts**](https://reference.aspose.com/cells/javascript-cpp/workbook/#customXmlParts--). También consulta la salida de la consola del código a continuación como referencia.
## **Código de muestra**
```html
<!DOCTYPE html>
<html>
<head>
<title>Add and Select Custom XML Parts Example</title>
</head>
<body>
<h1>Add and Select Custom XML Parts 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, 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');
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();
// Loads the workbook which contains hidden external links
const wb = new Workbook(new Uint8Array(arrayBuffer));
// Some data in the form of byte array.
// Please use correct XML and Schema instead.
const btsData = new Uint8Array([1, 2, 3]);
const btsSchema = new Uint8Array([1, 2, 3]);
// Create four custom xml parts.
wb.customXmlParts.add(btsData, btsSchema);
wb.customXmlParts.add(btsData, btsSchema);
wb.customXmlParts.add(btsData, btsSchema);
wb.customXmlParts.add(btsData, btsSchema);
// Assign ids to custom xml parts.
wb.customXmlParts.get(0).id = "Fruit";
wb.customXmlParts.get(1).id = "Color";
wb.customXmlParts.get(2).id = "Sport";
wb.customXmlParts.get(3).id = "Shape";
// Specify search custom xml part id.
let srchID = "Fruit";
srchID = "Color";
srchID = "Sport";
// Search custom xml part by the search id.
const cxp = wb.customXmlParts.selectByID(srchID);
// Print the found or not found message on console and UI.
if (cxp.isNull()) {
console.log(`Not Found: CustomXmlPart ID ${srchID}`);
document.getElementById('result').innerHTML = `<p style="color: red;">Not Found: CustomXmlPart ID ${srchID}</p>`;
} else {
console.log(`Found: CustomXmlPart ID ${srchID}`);
document.getElementById('result').innerHTML = `<p style="color: green;">Found: CustomXmlPart ID ${srchID}</p>`;
}
// Save the modified workbook and provide download link
const outputData = wb.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';
downloadLink.textContent = 'Download Modified Excel File';
console.log("AddCustomXMLPartsAndSelectThemByID executed successfully.");
});
</script>
</html>
Salida de la consola
Found: CustomXmlPart ID Sport