Node.jsとC++を使ったドキュメントプロパティの管理
紹介
Microsoft Excelはスプレッドシートファイルにプロパティを追加できる機能を提供します。これらのドキュメントプロパティは有用な情報を提供し、以下のように2つのカテゴリに分かれています。
- システム定義(組み込み)プロパティ: 組み込みプロパティには文書のタイトル、作成者名、文書の統計などの一般的な情報が含まれています。
- ユーザー定義(カスタム)プロパティ: ユーザーが名前-値のペアの形式で定義したカスタムプロパティ。
Microsoft Excelを使用してドキュメントプロパティを管理する方法
Microsoft Excelでは、ExcelファイルのドキュメントプロパティをWYSIWYG方式で管理できます。Excel 2016でプロパティダイアログを開く手順については、下記をお尋ねください。
- ファイルメニューから情報を選択します。
情報メニューを選択 |
---|
![]() |
- プロパティの見出しをクリックし、「詳細プロパティ」を選択します。
詳細プロパティの選択をクリック |
---|
![]() |
- ファイルのドキュメントプロパティを管理します。
プロパティダイアログ |
---|
![]() |
プロパティダイアログでは、一般、概要、統計、内容、カスタムのような異なるタブがあります。各タブはファイルに関連する異なる種類の情報を設定するのに役立ちます。カスタムタブはカスタムプロパティを管理するために使用されます。 |
Aspose.Cellsを使用してドキュメントプロパティを操作する方法
開発者はAspose.CellsのAPIを使用してドキュメントプロパティを動的に管理できます。この機能により、ファイルが受信された時点、処理された時点、タイムスタンプなどの有用な情報をファイルと一緒に保存できます。
Aspose.Cells for Node.js via C++はAPIおよびバージョン番号の情報を出力ドキュメントに直接記述します。例えば、ドキュメントをPDFにレンダリングするとき、Aspose.Cells for Node.js via C++はApplicationフィールドに’Aspose.Cells’を、PDF Producerフィールドに例えば’Aspose.Cells v17.9’を設定します。
この情報を出力ドキュメントから変更または削除するよう关于Aspose.Cells for Node.js via C++に指示できないことに注意してください。
ドキュメントプロパティにアクセスする方法
Aspose.Cells APIは、組み込みとカスタムの両方のタイプのドキュメントプロパティをサポートします。Aspose.CellsのWorkbookクラスはExcelファイルを表し、そのようにファイルと同様に、Workbookクラスは複数のワークシートを含むことができ、各ワークシートはWorksheetクラスによって表され、ワークシートのコレクションはWorksheetCollectionクラスによって表されます。
下記の説明のとおり、WorksheetCollectionを使用してファイルのドキュメントプロパティにアクセスします。
- 組み込みドキュメントプロパティにアクセスするには、WorksheetCollection.getBuiltInDocumentProperties()を使用します。
- カスタムドキュメントのプロパティにアクセスするにはWorksheetCollection.getCustomDocumentProperties()を使用します。
WorksheetCollection.getBuiltInDocumentProperties()とWorksheetCollection.getCustomDocumentProperties()はどちらもAspose.Cells.Properties.DocumentPropertyCollectionのインスタンスを返します。このコレクションはAspose.Cells.Properties.DocumentPropertyオブジェクトを含み、各オブジェクトは単一の組み込みまたはカスタムドキュメントプロパティを表します。
どのようにプロパティにアクセスするかはアプリケーションの要件次第です。すなわち、例の通りDocumentPropertyCollectionのインデックスまたは名前を使用します。
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クラスは、ドキュメントプロパティの名前、値、型を取得できます。
- プロパティ名を取得するにはDocumentProperty.getName()を使用します。
- プロパティの値を取得するには、DocumentProperty.getValue()を使用します。DocumentProperty.getValue()は値をObjectとして返します。
- プロパティの型を取得するには、DocumentProperty.getType()を使用します。これはPropertyType列挙値のいずれかを返します。プロパティの型を取得した後、適切な型の値を取得するためにDocumentProperty.ToXXXメソッドのいずれかを使用してください。これらのメソッドは下表に記載されています。
メンバー名 | 説明 | ToXXXメソッド |
---|---|---|
Boolean | プロパティのデータ型はブールです | ToBool |
Date | プロパティのデータ型は日時です。Microsoft Excelでは日付部分のみが保存され、時刻は保存されません。 | ToDateTime |
Float | プロパティのデータ型はダブルです | ToDouble |
Number | プロパティのデータ型はInt32です | ToInt |
String | プロパティのデータ型は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)メソッドを公開し、CustomDocumentPropertyCollectionクラスにカスタムプロパティを追加できるようにしています。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()プロパティを使用して確認できます。さらに、DocumentPropertyクラスのgetSource()プロパティを使用してソース範囲を取得可能です。
この例では、シンプルなテンプレートの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"));