ドキュメントプロパティの管理

紹介

Microsoft Excelはスプレッドシートファイルにプロパティを追加できる機能を提供します。これらのドキュメントプロパティは有用な情報を提供し、以下のように2つのカテゴリに分かれています。

  • システム定義(組み込み)プロパティ: 組み込みプロパティには文書のタイトル、作成者名、文書の統計などの一般的な情報が含まれています。
  • ユーザー定義(カスタム)プロパティ: ユーザーが名前-値のペアの形式で定義したカスタムプロパティ。

Microsoft Excel を使用したドキュメントプロパティの管理

Microsoft Excel では、Excel ファイルのドキュメントプロパティを WYSIWYG の方法で管理することができます。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.BuiltInDocumentPropertiesWorksheetCollection.CustomDocumentPropertiesの両方がDocumentPropertyCollectionのインスタンスを返します。このコレクションには、DocumentPropertyオブジェクトが含まれており、それぞれが単一の組み込みまたはカスタムドキュメントプロパティを表します。

プロパティへのアクセス方法はアプリケーションの要件によって異なります。つまり、DocumentPropertyCollectionからのプロパティの名前またはインデックスを使用するか、以下の例に示すようにします。

// For complete examples and data files, please go to https://github.com/aspose-tasks/Aspose.Cells-for-Java
// Instantiate a Workbook object
// Open an Excel file
Workbook workbook = new Workbook(dataDir + "sample.xls");
// Retrieve a list of all custom document properties of the Excel file
DocumentPropertyCollection customProperties = workbook.getWorksheets().getCustomDocumentProperties();
// Accessing a custom document property by using the property name
DocumentProperty customProperty1 = customProperties.get("ContentTypeId");
System.out.println(customProperty1.getName() + " " + customProperty1.getValue());
// Accessing a custom document property by using the property index
DocumentProperty customProperty2 = customProperties.get(0);
System.out.println(customProperty2.getName() + " " + customProperty2.getValue());

DocumentPropertyクラスでは、ドキュメントのプロパティの名前、値、および型を取得することができます。

// For complete examples and data files, please go to https://github.com/aspose-tasks/Aspose.Cells-for-Java
// Instantiate a Workbook object
// Open an Excel file
Workbook workbook = new Workbook(dataDir + "sample.xls");
// Retrieve a list of all custom document properties of the Excel file
DocumentPropertyCollection customProperties = workbook.getWorksheets().getCustomDocumentProperties();
// Accessing a custom document property
DocumentProperty customProperty1 = customProperties.get(0);
// Storing the value of the document property as an object
Object objectValue = customProperty1.getValue();
// Accessing a custom document property
DocumentProperty 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() == PropertyType.STRING)
{
String value = customProperty2.getValue().toString();
}

カスタムドキュメントプロパティの追加または削除

このトピックの冒頭で既に説明した通り、ビルトインプロパティはシステム定義されたものであり、開発者は追加または削除することはできませんが、ユーザー定義のカスタムプロパティを追加または削除することは可能です。

カスタムプロパティの追加

Aspose.Cells APIでは、CustomDocumentPropertyCollectionクラスにカスタムプロパティを追加するためのaddメソッドが公開されており、addメソッドはExcelファイルにプロパティを追加し、新しいドキュメントプロパティの参照としてDocumentPropertyオブジェクトを返します。

// For complete examples and data files, please go to https://github.com/aspose-tasks/Aspose.Cells-for-Java
// Instantiate a Workbook object
// Open an Excel file
Workbook workbook = new Workbook(dataDir + "sample.xlsx");
// Retrieve a list of all custom document properties of the Excel file
CustomDocumentPropertyCollection customProperties = workbook.getWorksheets().getCustomDocumentProperties();
// Adding a custom document property to the Excel file
DocumentProperty publisher = customProperties.add("Publisher", "Aspose");

「コンテンツにリンク」カスタムプロパティの構成

特定の範囲のコンテンツにリンクされたカスタムプロパティを作成するには、CustomDocumentPropertyCollection.addLinkToContentメソッドを呼び出してプロパティ名とソースを渡します。 プロパティがコンテンツにリンクされているかどうかを確認するにはDocumentProperty.isLinkedToContentプロパティを使用できます。 また、DocumentPropertyクラスのSourceプロパティを使用してソース範囲を取得することも可能です。

この例では、シンプルなテンプレートのMicrosoft Excelファイルを使用します。 ワークブックには、MyRangeと表記された名前付き範囲があり、セルの値を参照しています。

// For complete examples and data files, please go to https://github.com/aspose-tasks/Aspose.Cells-for-Java
// Instantiate a Workbook object
// Open an Excel file
Workbook workbook = new Workbook(dataDir + "sample.xlsx");
// Retrieve a list of all custom document properties of the Excel file
CustomDocumentPropertyCollection customProperties = workbook.getWorksheets().getCustomDocumentProperties();
// Add link to content.
customProperties.addLinkToContent("Owner", "MyRange");
// Accessing the custom document property by using the property name
DocumentProperty customProperty1 = customProperties.get("Owner");
// Check whether the property is lined to content
Boolean islinkedtocontent = customProperty1.isLinkedToContent();
// Get the source for the property
String source = customProperty1.getSource();

カスタムプロパティの削除

Aspose.Cellsを使用してカスタムプロパティを削除するには、DocumentPropertyCollection.removeメソッドを呼び出して削除するドキュメントプロパティの名前を渡します。

// For complete examples and data files, please go to https://github.com/aspose-tasks/Aspose.Cells-for-Java
// Instantiate a Workbook object
// Open an Excel file
Workbook workbook = new Workbook(dataDir + "sample.xlsx");
// Retrieve a list of all custom document properties of the Excel file
DocumentPropertyCollection customProperties = workbook.getWorksheets().getCustomDocumentProperties();
// Removing a custom document property
customProperties.remove("Publisher");