用Node.js通过C++管理文档属性

介绍

Microsoft Excel提供了向电子表格文件添加属性的功能。这些文档属性提供有用信息,分为以下2类。

  • 系统定义(内置)属性:内置属性包含有关文档的一般信息,如文档标题、作者姓名、文档统计信息等。
  • 用户定义(自定义)属性:最终用户以名称-值对的形式定义的自定义属性。

如何使用Microsoft Excel管理文档属性

Microsoft Excel允许以WYSIWYG方式管理Excel文件的文档属性。请按照以下步骤在Excel 2016中打开属性对话框。

  1. 文件菜单中选择信息
选择信息菜单
todo:image_alt_text
  1. 点击属性标题并选择"高级属性"。
单击高级属性选择
todo:image_alt_text
  1. 管理文件的文档属性。
属性对话框
todo:image_alt_text
在属性对话框中,有不同的选项卡,如常规、摘要、统计、内容和自定义。每个选项卡都可以帮助配置文件相关的不同信息。自定义选项卡用于管理自定义属性。

如何使用Aspose.Cells处理文档属性

开发人员可以使用Aspose.Cells API动态管理文档属性。此功能帮助开发人员存储有用信息,如文件接收时间、处理时间戳等。

如何访问文档属性

Aspose.Cells API支持内置和自定义两种类型的文档属性。Aspose.Cells的Workbook类代表Excel文件,类似Excel文件,Workbook类可以包含多个工作表,每个由Worksheet类表示,而工作表集合由WorksheetCollection类表示。

使用WorksheetCollection访问文件的文档属性,如下所示。

WorksheetCollection.getBuiltInDocumentProperties()WorksheetCollection.getCustomDocumentProperties()都返回Aspose.Cells.Properties.DocumentPropertyCollection的实例。该集合包含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");
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类允许检索文档属性的名称、值和类型:

成员名称 描述 ToXXX方法
Boolean 属性数据类型为布尔值 ToBool
Date 属性数据类型为日期时间。请注意,Microsoft Excel仅存储日期部分,无法在此类型的自定义属性中存储时间 ToDateTime
Float 属性数据类型为双精度浮点数 ToDouble
Number 属性数据类型为Int32 ToInt
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}`);
}

如何添加或删除自定义文档属性

正如我们在本主题开头所述的那样,开发人员无法添加或删除内置属性,因为这些属性是系统定义的,但可以添加或删除自定义属性,因为这些是用户定义的。

如何添加自定义属性

Aspose.Cells API已暴露add(string, string)方法,用于向集合中添加自定义属性。add(string, string)方法将属性添加到Excel文件中,并返回新文档属性的引用,为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"));

如何配置’链接到内容’自定义属性

要创建一个与给定范围内容关联的自定义属性,请调用CustomDocumentPropertyCollection.addLinkToContent(string, string)方法并传递属性名和源。可以通过DocumentProperty.isLinkedToContent()属性检查是否将属性配置为与内容关联。此外,也可以使用getSource()属性和DocumentProperty类获取源范围。

我们在示例中使用了一个简单的模板Microsoft Excel文件。工作簿有一个命名范围标记为MyRange,它指向单元格值。

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

如何移除自定义属性

要使用Aspose.Cells删除自定义属性,请调用DocumentPropertyCollection.remove(string)方法并传递要删除的文档属性名称。

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

高级主题