Gestisci proprietà del documento con Node.js 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.

  1. Dal menu File, seleziona Informazioni.
Selezionare il menu Informazioni
todo:image_alt_text
  1. Clicca sulla voce Proprietà e seleziona “Proprietà avanzate”.
Selezione Proprietà Avanzate
todo:image_alt_text
  1. Gestire le proprietà del documento del file.
Dialogo Proprietà
todo:image_alt_text
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.

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.

Sia WorksheetCollection.getBuiltInDocumentProperties() che WorksheetCollection.getCustomDocumentProperties() 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.

const path = require("path");
const AsposeCells = require("aspose.cells.node");

// The path to the documents directory.
const dataDir = path.join(__dirname, "data");
const filePath = path.join(dataDir, "sample-document-properties.xlsx");
// Instantiate a Workbook object
// Open an Excel file
const workbook = new AsposeCells.Workbook(filePath);

// Retrieve a list of all custom document properties of the Excel file
const customProperties = workbook.getCustomDocumentProperties();

// Accessing a custom document property by using the property name
const customProperty1 = customProperties.get("ContentTypeId");
console.log(`${customProperty1.getName()} ${customProperty1.getValue()}`);

// Accessing the same custom document property by using the property index
const customProperty2 = customProperties.get(0);
console.log(`${customProperty2.getName()} ${customProperty2.getValue()}`);

La classe Aspose.Cells.Properties.DocumentProperty permette di recuperare il nome, il valore e il tipo della proprietà del documento:

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
const path = require("path");
const AsposeCells = require("aspose.cells.node");

// The path to the documents directory.
const dataDir = path.join(__dirname, "data");
const filePath = path.join(dataDir, "sample-document-properties.xlsx");
// Instantiate a Workbook object
// Open an Excel file
const workbook = new AsposeCells.Workbook(filePath);

// Retrieve a list of all custom document properties of the Excel file
const customProperties = workbook.getCustomDocumentProperties();

// Accessing a custom document property
const customProperty1 = customProperties.get(0);

// Storing the value of the document property as an object
const objectValue = customProperty1.getValue();

// Accessing a custom document property
const customProperty2 = customProperties.get(1);

// Checking the type of the document property and then storing the value of the
// document property according to that type
if (customProperty2.getType() === AsposeCells.PropertyType.String) {
const value = customProperty2.getValue().toString();
console.log(`${customProperty2.getName()} : ${value}`);
}

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.

const path = require("path");
const AsposeCells = require("aspose.cells.node");

// The path to the documents directory.
const dataDir = path.join(__dirname, "data");
// Instantiate a Workbook object
// Open an Excel file
const workbook = new AsposeCells.Workbook(path.join(dataDir, "sample-document-properties.xlsx"));

// Retrieve a list of all custom document properties of the Excel file
const customProperties = workbook.getCustomDocumentProperties();

// Adding a custom document property to the Excel file
customProperties.add("Publisher", "Aspose");

// Saving resultant spreadsheet
workbook.save(path.join(dataDir, "out_sample-document-properties.xlsx"));

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à getSource() 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.

const path = require("path");
const AsposeCells = require("aspose.cells.node");

// The path to the documents directory.
const dataDir = path.join(__dirname, "data");
// Instantiate an object of Workbook
// Open an Excel file
const workbook = new AsposeCells.Workbook(path.join(dataDir, "sample-document-properties.xlsx"));

// Retrieve a list of all custom document properties of the Excel file
const customProperties = workbook.getWorksheets().getCustomDocumentProperties();

// 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.getSource();

// Save the file
workbook.save(path.join(dataDir, "out_sample-document-properties.xlsx"));

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.

const path = require("path");
const AsposeCells = require("aspose.cells.node");

// The path to the documents directory.
const dataDir = path.join(__dirname, "data");
// Instantiate a Workbook object
// Open an Excel file
const workbook = new AsposeCells.Workbook(path.join(dataDir, "sample-document-properties.xlsx"));

// Retrieve a list of all custom document properties of the Excel file
const customProperties = workbook.getCustomDocumentProperties();

// Removing a custom document property
customProperties.remove("Publisher");

// Save the file
workbook.save(path.join(dataDir, "out_sample-document-properties.xlsx"));

Argomenti avanzati