---
title: Aggiungi parti XML personalizzate e selezionalo tramite ID con JavaScript tramite C++
linktitle: Aggiungi Parti XML personalizzate e selezionale per ID
type: docs
weight: 70
url: /it/javascript-cpp/add-custom-xml-parts-and-select-them-by-id/
description: Scopri come aggiungere parti XML personalizzate ai documenti Excel e selezionarle tramite ID usando Aspose.Cells for JavaScript via C++.
---
## **Possibili Scenari di Utilizzo**
Le parti XML personalizzate sono dati XML memorizzati all'interno dei documenti Microsoft Excel e sono utilizzate dalle applicazioni che le gestiscono. Al momento, non esiste un modo diretto per aggiungerle tramite l'interfaccia utente di Microsoft Excel. Tuttavia, puoi aggiungerle programmaticamente in vari modi, ad esempio usando VSTO, Aspose.Cells, ecc. Si prega di usare la collezione [**Workbook.customXmlParts**](https://reference.aspose.com/cells/javascript-cpp/workbook/#customXmlParts--) se si desidera aggiungere una Parte XML Personalizzata usando l'API Aspose.Cells. È inoltre possibile impostare il suo ID usando la proprietà [**CustomXmlPart.iD**](https://reference.aspose.com/cells/javascript-cpp/customxmlpart/#iD--). Analogamente, se si desidera selezionare una Parte XML Personalizzata per ID, è possibile usare la collezione [**Workbook.customXmlParts**](https://reference.aspose.com/cells/javascript-cpp/workbook/#customXmlParts--).
## **Aggiungi parti XML personalizzate e selezionale per ID**
Il seguente codice di esempio aggiunge prima quattro Parti XML Personalizzate usando la collezione [**Workbook.customXmlParts**](https://reference.aspose.com/cells/javascript-cpp/workbook/#customXmlParts--). Successivamente, imposta i loro ID usando la proprietà [**CustomXmlPart.iD**](https://reference.aspose.com/cells/javascript-cpp/customxmlpart/#iD--). Infine, trova o seleziona una delle Parti XML Personalizzate aggiunte usando la collezione [**Workbook.customXmlParts**](https://reference.aspose.com/cells/javascript-cpp/workbook/#customXmlParts--). Si consiglia di consultare anche l'output della console del codice qui sotto per riferimento.
## **Codice di Esempio**
```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>
Output della console
Found: CustomXmlPart ID Sport