Gestire le Proprietà del Documento con JavaScript tramite C++
Introduzione
Microsoft Excel fornisce la possibilità di aggiungere proprietà ai file di fogli elettronici. Queste proprietà del documento forniscono informazioni utili e sono divise in 2 categorie come dettagliato di seguito.
- Proprietà predefinite di sistema (builtin): Le proprietà incorporano informazioni generali sul documento come il titolo del documento, il nome dell’autore, le statistiche del documento e così via.
- Proprietà definite dall’utente (personalizzate): Proprietà personalizzate definite dall’utente sotto forma di coppia nome-valore.
Come gestire le proprietà del documento con Microsoft Excel
Microsoft Excel ti permette di gestire le proprietà del documento dei file Excel in modo WYSIWYG. Segui i passaggi sottostanti per aprire la finestra di dialogo Proprietà in Excel 2016.
- Dal menu File, seleziona Informazioni.
| Selezionare il menu Informazioni |
|---|
![]() |
- Clicca sulla voce Proprietà e seleziona “Proprietà avanzate”.
| Selezione Proprietà Avanzate |
|---|
![]() |
- Gestire le proprietà del documento del file.
| Dialogo Proprietà |
|---|
![]() |
| Nel dialogo Proprietà, ci sono diverse schede, come Generale, Riepilogo, Statistiche, Contenuti e Personalizzati. Ogni scheda aiuta a configurare diversi tipi di informazioni relative al file. La scheda Personalizzati è utilizzata per gestire le proprietà personalizzate. |
Come lavorare con le proprietà del documento utilizzando Aspose.Cells
Gli sviluppatori possono gestire dinamicamente le proprietà del documento utilizzando le API di Aspose.Cells. Questa funzionalità aiuta gli sviluppatori a memorizzare informazioni utili insieme al file, come quando il file è stato ricevuto, elaborato, con timestamp e così via.
Aspose.Cells for JavaScript tramite C++ scrive direttamente le informazioni sull’API e il Numero di Versione nei documenti di output. Ad esempio, durante la conversione di un Documento in PDF, Aspose.Cells for JavaScript tramite C++ popola il campo Applicazione con il valore ‘Aspose.Cells’ e il campo Produttore PDF con il valore, ad esempio ‘Aspose.Cells v17.9’.
Si prega di notare che non è possibile istruire Aspose.Cells for JavaScript tramite C++ a modificare o rimuovere queste informazioni dai Documenti di output.
Come accedere alle proprietà del documento
Le API di Aspose.Cells supportano sia le proprietà del documento integrate che quelle personalizzate. La classe Workbook di Aspose.Cells rappresenta un file Excel e, come un file Excel, la classe Workbook può contenere più fogli di lavoro, ciascuno rappresentato dalla classe Worksheet, mentre la raccolta di fogli di lavoro è rappresentata dalla classe WorksheetCollection.
Usa WorksheetCollection per accedere alle proprietà del documento del file come descritto di seguito.
- Per accedere alle proprietà integrate del documento, utilizzare WorksheetCollection.builtInDocumentProperties().
- Per accedere alle proprietà personalizzate del documento, utilizzare WorksheetCollection.customDocumentProperties().
Sia WorksheetCollection.builtInDocumentProperties() che WorksheetCollection.customDocumentProperties() restituiscono l’istanza di Aspose.Cells.Properties.DocumentPropertyCollection. Questa raccolta contiene Aspose.Cells.Properties.DocumentProperty oggetti, ciascuno dei quali rappresenta una proprietà del documento, sia integrata che personalizzata.
Spetta all’applicazione decidere come accedere a una proprietà; ad esempio, usando l’indice o il nome della proprietà da DocumentPropertyCollection, come mostrato nell’esempio sottostante.
<!DOCTYPE html>
<html>
<head>
<title>Aspose.Cells Example - Document Properties</title>
</head>
<body>
<h1>Document Properties Example</h1>
<input type="file" id="fileInput" accept=".xls,.xlsx,.csv" />
<button id="runExample">Load Document Properties</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 resultEl = document.getElementById('result');
const fileInput = document.getElementById('fileInput');
if (!fileInput.files.length) {
resultEl.innerHTML = '<p style="color: red;">Please select an Excel file.</p>';
return;
}
const file = fileInput.files[0];
const arrayBuffer = await file.arrayBuffer();
// Instantiating a Workbook object and opening the Excel file
const workbook = new Workbook(new Uint8Array(arrayBuffer));
// Retrieve a list of all custom document properties of the Excel file
const customProperties = workbook.customDocumentProperties;
// Accessing a custom document property by using the property name
const customProperty1 = customProperties.get("ContentTypeId");
// Accessing the same custom document property by using the property index
const customProperty2 = customProperties.get(0);
const outputs = [];
if (customProperty1) {
outputs.push(`<p>${customProperty1.name} ${customProperty1.value}</p>`);
}
if (customProperty2) {
outputs.push(`<p>${customProperty2.name} ${customProperty2.value}</p>`);
}
if (!outputs.length) {
resultEl.innerHTML = '<p style="color: orange;">No custom document properties found.</p>';
} else {
resultEl.innerHTML = outputs.join('');
}
});
</script>
</html>
La classe Aspose.Cells.Properties.DocumentProperty permette di recuperare il nome, il valore e il tipo della proprietà del documento:
- Per ottenere il nome della proprietà, utilizzare DocumentProperty.name().
- Per ottenere il valore della proprietà, usa DocumentProperty.value(). DocumentProperty.value() restituisce il valore come Oggetto.
- Per ottenere il tipo di proprietà, usa DocumentProperty.type(). Questa restituisce uno dei valori dell’enumerazione PropertyType. Dopo aver ottenuto il tipo di proprietà, usa uno dei metodi DocumentProperty.ToXXX per ottenere il valore del tipo appropriato invece di usare DocumentProperty.value(). I metodi DocumentProperty.ToXXX sono descritti nella tabella sottostante.
| Nome membro | Descrizione | Metodo ToXXX |
|---|---|---|
| Boolean | Il tipo di dati della proprietà è Booleano | ToBool |
| Date | Il tipo di dati della proprietà è DataOra. Nota che Microsoft Excel memorizza solo la parte della data, nessuna ora può essere memorizzata in una proprietà personalizzata di questo tipo |
ToDateTime |
| Float | Il tipo di dati della proprietà è Double | ToDouble |
| Number | Il tipo di dati della proprietà è Int32 | ToInt |
| String | Il tipo di dato della proprietà è string | ToString |
<!DOCTYPE html>
<html>
<head>
<title>Aspose.Cells Example</title>
</head>
<body>
<h1>Retrieve Custom Document Properties 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, Worksheet, Cell } = 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');
resultDiv.innerHTML = '';
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();
// Instantiate a Workbook object by opening the uploaded Excel file
const workbook = new Workbook(new Uint8Array(arrayBuffer));
// Retrieve a list of all custom document properties of the Excel file
const customProperties = workbook.customDocumentProperties;
let outputHtml = '<h2>Custom Document Properties</h2>';
// Accessing a custom document property (first)
const customProperty1 = customProperties.get(0);
if (customProperty1) {
// Storing the value of the document property as an object
const objectValue = customProperty1.value;
outputHtml += `<p><strong>${customProperty1.name}</strong> (type: ${customProperty1.type}) : ${objectValue}</p>`;
} else {
outputHtml += '<p>No first custom property found.</p>';
}
// Accessing a custom document property (second)
const customProperty2 = customProperties.get(1);
if (customProperty2) {
// Checking the type of the document property and then storing the value according to that type
if (customProperty2.type === AsposeCells.PropertyType.String) {
const value = customProperty2.value.toString();
outputHtml += `<p>${customProperty2.name} : ${value}</p>`;
} else {
outputHtml += `<p>${customProperty2.name} (type: ${customProperty2.type}) : ${customProperty2.value}</p>`;
}
} else {
outputHtml += '<p>No second custom property found.</p>';
}
resultDiv.innerHTML = outputHtml;
});
</script>
</html>
Come Aggiungere o Rimuovere Proprietà del Documento Personalizzate
Come abbiamo descritto in precedenza all’inizio di questo argomento, i programmatori non possono aggiungere o rimuovere proprietà integrate perché queste proprietà sono definite dal sistema, ma è possibile aggiungere o rimuovere proprietà personalizzate poiché queste sono definite dall’utente.
Come Aggiungere Proprietà Personalizzate
Le API Aspose.Cells hanno esposto il metodo add(string, string) per la classe CustomDocumentPropertyCollection al fine di aggiungere proprietà personalizzate alla raccolta. Il metodo add(string, string) aggiunge la proprietà al file Excel e restituisce un riferimento alla nuova proprietà del documento come oggetto Aspose.Cells.Properties.DocumentProperty.
<!DOCTYPE html>
<html>
<head>
<title>Aspose.Cells Example</title>
</head>
<body>
<h1>Aspose.Cells Example - Add Custom Document Property</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();
// Instantiating a Workbook object and opening the Excel file
const workbook = new Workbook(new Uint8Array(arrayBuffer));
// Retrieve a list of all custom document properties of the Excel file
const customProperties = workbook.customDocumentProperties;
// Adding a custom document property to the Excel file
customProperties.add("Publisher", "Aspose");
// Saving the modified Excel file
const outputData = workbook.save(SaveFormat.Xlsx);
const blob = new Blob([outputData]);
const downloadLink = document.getElementById('downloadLink');
downloadLink.href = URL.createObjectURL(blob);
downloadLink.download = 'out_sample-document-properties.xlsx';
downloadLink.style.display = 'block';
downloadLink.textContent = 'Download Modified Excel File';
document.getElementById('result').innerHTML = '<p style="color: green;">Custom property added successfully! Click the download link to get the modified file.</p>';
});
});
</script>
</html>
Come Configurare Proprietà Personalizzata “Collegamento al contenuto”
Per creare una proprietà personalizzata collegata al contenuto di un intervallo dato, chiamare il metodo CustomDocumentPropertyCollection.addLinkToContent(string, string) e passare il nome della proprietà e la sorgente. È possibile verificare se una proprietà è configurata come collegata al contenuto utilizzando la proprietà DocumentProperty.isLinkedToContent(). Inoltre, è possibile ottenere anche l’intervallo sorgente utilizzando la proprietà source() della classe DocumentProperty.
Utilizziamo un file modello semplice di Microsoft Excel nell’esempio. Il workbook ha un intervallo denominato definito MyRange, che si riferisce a un valore della cella.
<!DOCTYPE html>
<html>
<head>
<title>Aspose.Cells Example</title>
</head>
<body>
<h1>Aspose.Cells Example - Custom Document Properties</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');
const resultEl = document.getElementById('result');
if (!fileInput.files.length) {
resultEl.innerHTML = '<p style="color: red;">Please select an Excel file.</p>';
return;
}
const file = fileInput.files[0];
const arrayBuffer = await file.arrayBuffer();
// Instantiate a Workbook by loading the uploaded file
const workbook = new Workbook(new Uint8Array(arrayBuffer));
// Retrieve a list of all custom document properties of the Excel file
const customProperties = workbook.worksheets.customDocumentProperties;
// Add link to content.
customProperties.addLinkToContent("Owner", "MyRange");
// Accessing the custom document property by using the property name
const customProperty1 = customProperties.get("Owner");
// Check whether the property is linked to content
const isLinkedToContent = customProperty1.isLinkedToContent;
// Get the source for the property
const source = customProperty1.source;
// Save the file
const outputData = workbook.save(SaveFormat.Xlsx);
const blob = new Blob([outputData]);
const downloadLink = document.getElementById('downloadLink');
downloadLink.href = URL.createObjectURL(blob);
downloadLink.download = 'out_sample-document-properties.xlsx';
downloadLink.style.display = 'block';
downloadLink.textContent = 'Download Modified Excel File';
resultEl.innerHTML = `<p style="color: green;">Operation completed successfully! Property linked: ${isLinkedToContent}. Source: ${source}. Click the download link to get the modified file.</p>`;
});
</script>
</html>
Come rimuovere proprietà personalizzate
Per rimuovere proprietà personalizzate usando Aspose.Cells, chiamare il metodo DocumentPropertyCollection.remove(string) e passare il nome della proprietà del documento da rimuovere.
<!DOCTYPE html>
<html>
<head>
<title>Remove Custom Document Property Example</title>
</head>
<body>
<h1>Remove Custom Document Property 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();
// Instantiate a Workbook object by opening the uploaded Excel file
const workbook = new Workbook(new Uint8Array(arrayBuffer));
// Retrieve a list of all custom document properties of the Excel file
const customProperties = workbook.customDocumentProperties;
// Removing a custom document property named "Publisher"
customProperties.remove("Publisher");
// Save the file and provide a download link
const outputData = workbook.save(SaveFormat.Xlsx);
const blob = new Blob([outputData]);
const downloadLink = document.getElementById('downloadLink');
downloadLink.href = URL.createObjectURL(blob);
downloadLink.download = 'out_sample-document-properties.xlsx';
downloadLink.style.display = 'block';
downloadLink.textContent = 'Download Modified Excel File';
document.getElementById('result').innerHTML = '<p style="color: green;">Custom property removed successfully! Click the download link to get the modified file.</p>';
});
</script>
</html>
Argomenti avanzati
- Aggiunta di proprietà personalizzate visibili all’interno del pannello delle informazioni del documento
- Impostazione delle proprietà ScaleCrop e LinksUpToDate delle proprietà del documento integrato
- Specifica la versione del documento del file Excel utilizzando le proprietà del documento integrato
- Specificare la lingua del file Excel utilizzando le proprietà di documento incorporate


