Hantera dokumentegenskaper med Node.js via C++

Introduktion

Microsoft Excel ger möjligheten att lägga till egenskaper i kalkylbladfiler. Dessa dokumentegenskaper förser användbar information och är uppdelade i 2 kategorier enligt detaljerna nedan.

  • Systemdefinierade (inbyggda) egenskaper: Inbyggda egenskaper innehåller allmän information om dokumentet som dokumenttitel, författarnamn, dokumentstatistik och så vidare.
  • Användardefinierade (anpassade) egenskaper: Anpassade egenskaper som definieras av användaren i form av namn-värdepar.

Hur man hanterar dokumentegenskaper med Microsoft Excel

Microsoft Excel gör det möjligt att hantera dokumentegenskaper för Excel-filer på ett WYSIWYG-sätt. Följ stegen nedan för att öppna Egenskaper-dialogrutan i Excel 2016.

  1. Välj Info i Fil-menyn.
Val av Info-meny
todo:image_alt_text
2. Klicka på Egenskaper och välj “Avancerade egenskaper”.
Klicka på Avancerad Val av egenskaper
todo:image_alt_text
3. Hantera filens dokumentegenskaper.
Dialogruta Egenskaper
todo:image_alt_text
I dialogrutan Egenskaper finns olika flikar, som Allmänt, Sammanfattning, Statistik, Innehåll och Anpassade. Varje flik hjälper till att konfigurera olika typer av information relaterad till filen. Anpassad flik används för att hantera anpassade egenskaper.

Hur man arbetar med dokumentegenskaper med Aspose.Cells

Utvecklare kan dynamiskt hantera dokumentegenskaper med hjälp av Aspose.Cells API:er. Denna funktion hjälper utvecklarna att lagra användbar information tillsammans med filen, som när filen mottogs, bearbetades, tidsstämplades och så vidare.

Hur man får åtkomst till dokumentegenskaper

Aspose.Cells API:er stödjer båda typerna av dokumentegenskaper, inbyggda och anpassade. Aspose.Cells' Workbook-klass representerar en Excel-fil och, precis som en Excel-fil, kan Workbook-klassen innehålla flera kalkblad, var och ett representerat av Worksheet-klassen medan samlingen av kalkblad representeras av WorksheetCollection-klassen.

Använd WorksheetCollection för att komma åt filens dokumentegenskaper som beskrivs nedan.

Både WorksheetCollection.getBuiltInDocumentProperties() och WorksheetCollection.getCustomDocumentProperties() returnerar instansen av Aspose.Cells.Properties.DocumentPropertyCollection. Denna samling innehåller Aspose.Cells.Properties.DocumentProperty objekt, var och ett som representerar en enkel inbyggd eller anpassad dokumentegenskap.

Det är upp till applikationskravet hur man får åtkomst till en egenskap, dvs. genom att använda index eller namn på egenskapen från DocumentPropertyCollection som visas i exemplet nedan.

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()}`);

Aspose.Cells.Properties.DocumentProperty-klassen gör det möjligt att hämta namn, värde och typ för dokumentegenskapen:

Medlemsnamn Beskrivning ToXXX-metod
Boolean Egenskapens datatyp är Boolean ToBool
Date Egenskapens datatyp är DateTime. Observera att Microsoft Excel endast lagrar
datumdelen, ingen tid kan lagras i en anpassad egenskap av denna typ
ToDateTime
Float Egenskapens datatyp är Dubbel ToDouble
Number Egenskapens datatyp är Int32 ToInt
String Typen av egenskapsdata är 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}`);
}

Hur man lägger till eller tar bort anpassade dokumentegenskaper

Som vi tidigare har beskrivit i början av detta ämne kan utvecklare inte lägga till eller ta bort inbyggda egenskaper eftersom dessa egenskaper är systemdefinierade men det är möjligt att lägga till eller ta bort anpassade egenskaper eftersom dessa är användardefinierade.

Hur man lägger till anpassade egenskaper

Aspose.Cells API:er har exponerat add(string, string)-metoden för CustomDocumentPropertyCollection-klassen för att lägga till anpassade egenskaper till samlingen. add(string, string)-metoden lägger till egenskapen i excel-filen och returnerar en referens till den nya dokumentegenskapen som ett Aspose.Cells.Properties.DocumentProperty-objekt.

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"));

Hur man konfigurerar egendom med länk till innehåll

För att skapa en anpassad egenskap kopplad till innehållet i ett givet område, kalla på CustomDocumentPropertyCollection.addLinkToContent(string, string)-metoden och passera egenskapens namn och källa. Du kan kontrollera om en egenskap är konfigurerad som kopplad till innehåll med hjälp av DocumentProperty.isLinkedToContent()-egenskapen. Dessutom är det också möjligt att få källområdet med hjälp av getSource()-egenskapen i DocumentProperty-klassen.

Vi använder en enkel mall Microsoft Excel-fil i exemplet. Arbetsboken har en definierad namngiven område märkt MyRange som hänvisar till en cellvärde.

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"));

Hur man tar bort anpassade egenskaper

För att ta bort anpassade egenskaper med Aspose.Cells, kalla på DocumentPropertyCollection.remove(string)-metoden och passera namnet på dokumentegenskapen som ska tas bort.

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"));

Fortsatta ämnen