ドキュメントプロパティを管理する
紹介
Microsoft Excelはスプレッドシートファイルにプロパティを追加できる機能を提供します。これらのドキュメントプロパティは有用な情報を提供し、以下のように2つのカテゴリに分かれています。
- システム定義(組み込み)プロパティ: 組み込みプロパティには文書のタイトル、作成者名、文書の統計などの一般的な情報が含まれています。
- ユーザー定義(カスタム)プロパティ: ユーザーが名前-値のペアの形式で定義したカスタムプロパティ。
Microsoft Excelを使用してドキュメントプロパティを管理する方法
Microsoft Excelを使用してExcelファイルのドキュメントプロパティをWYSIWYG形式で管理できます。以下の手順に従ってExcel 2016でプロパティダイアログを開いてください。
- ファイルメニューから情報を選択します。
情報メニューを選択 |
---|
![]() |
- プロパティの見出しをクリックし、「詳細プロパティ」を選択します。
詳細プロパティの選択をクリック |
---|
![]() |
- ファイルのドキュメントプロパティを管理します。
プロパティダイアログ |
---|
![]() |
プロパティダイアログでは、一般、概要、統計、内容、カスタムのような異なるタブがあります。各タブはファイルに関連する異なる種類の情報を設定するのに役立ちます。カスタムタブはカスタムプロパティを管理するために使用されます。 |
Aspose.Cellsを使用してドキュメントプロパティを操作する方法
開発者はAspose.CellsのAPIを使用してドキュメントプロパティを動的に管理できます。この機能により、ファイルが受信された時点、処理された時点、タイムスタンプなどの有用な情報をファイルと一緒に保存できます。
Aspose.Cells for .NETは、出力ドキュメントにAPIおよびバージョン番号に関する情報を直接書き込みます。たとえば、ドキュメントをPDFにレンダリングすると、「アプリケーション」フィールドに’Aspose.Cells’という値、また「PDFプロデューサー」フィールドに’Aspose.Cells v17.9’などの値が設定されます。
ただし、Aspose.Cells for .NETに対してこの情報を出力ドキュメントから変更または削除するよう指示することはできません。
ドキュメントプロパティにアクセスする方法
Aspose.CellsのAPIは組み込みプロパティとカスタムプロパティの両方をサポートしています。Aspose.CellsのWorkbookクラスはExcelファイルを表し、Excelファイルと同様にWorkbookクラスは複数のワークシートを含むことができ、各ワークシートはWorksheetクラスによって表されます。ワークシートのコレクションはWorksheetCollectionクラスによって表されます。
以下に示すように、WorksheetCollectionを使用してファイルのドキュメントプロパティにアクセスします。
- 組み込みドキュメントプロパティにアクセスするには、WorksheetCollection.BuiltInDocumentPropertiesを使用します。
- カスタムドキュメントのプロパティにアクセスするにはWorksheetCollection.CustomDocumentPropertiesを使用します。
WorksheetCollection.BuiltInDocumentPropertiesとWorksheetCollection.CustomDocumentPropertiesの両方がAspose.Cells.Properties.DocumentPropertyCollectionのインスタンスを返します。 このコレクションにはAspose.Cells.Properties.DocumentPropertyを含むオブジェクトが含まれており、それぞれが単一のビルトインまたはカスタムドキュメントプロパティを表します。
プロパティへのアクセス方法はアプリケーションの要件によって異なります。つまり、DocumentPropertyCollectionからのプロパティの名前またはインデックスを使用するか、以下の例に示すようにします。
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
// Instantiate a Workbook object | |
// Open an Excel file | |
Workbook workbook = new Workbook(dataDir + "sample-document-properties.xlsx"); | |
// Retrieve a list of all custom document properties of the Excel file | |
Aspose.Cells.Properties.DocumentPropertyCollection customProperties = workbook.Worksheets.CustomDocumentProperties; | |
// Accessing a custom document property by using the property name | |
Aspose.Cells.Properties.DocumentProperty customProperty1 = customProperties["ContentTypeId"]; | |
Console.WriteLine(customProperty1.Name + " " + customProperty1.Value); | |
// Accessing the same custom document property by using the property index | |
Aspose.Cells.Properties.DocumentProperty customProperty2 = customProperties[0]; | |
Console.WriteLine(customProperty2.Name + " " + customProperty2.Value); |
Aspose.Cells.Properties.DocumentPropertyクラスでは、ドキュメントのプロパティの名前、値、および型を取得することができます。
- プロパティ名を取得するにはDocumentProperty.Nameを使用します。
- プロパティの値を取得するにはDocumentProperty.Valueを使用します。DocumentProperty.Valueは値をオブジェクトとして返します。
- プロパティの型を取得するにはDocumentProperty.Typeを使用します。 これにより、PropertyTypeの列挙型の値のいずれかが返されます。 プロパティの型を取得した後は、DocumentProperty.Valueを使用せずに適切な型の値を取得するためにDocumentProperty.ToXXXメソッドのいずれかを使用します。 DocumentProperty.ToXXXメソッドについては、以下の表に説明があります。
メンバー名 | 説明 | ToXXXメソッド |
---|---|---|
Boolean | プロパティのデータ型はブールです | ToBool |
Date | プロパティのデータ型は日時です。Microsoft Excelでは日付部分のみが保存され、時刻は保存されません。 | ToDateTime |
Float | プロパティのデータ型はダブルです | ToDouble |
Number | プロパティのデータ型はInt32です | ToInt |
String | プロパティのデータ型は文字列です | ToString |
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
// Instantiate a Workbook object | |
// Open an Excel file | |
Workbook workbook = new Workbook(dataDir + "sample-document-properties.xlsx"); | |
// Retrieve a list of all custom document properties of the Excel file | |
Aspose.Cells.Properties.DocumentPropertyCollection customProperties = workbook.Worksheets.CustomDocumentProperties; | |
// Accessing a custom document property | |
Aspose.Cells.Properties.DocumentProperty customProperty1 = customProperties[0]; | |
// Storing the value of the document property as an object | |
object objectValue = customProperty1.Value; | |
// Accessing a custom document property | |
Aspose.Cells.Properties.DocumentProperty customProperty2 = customProperties[1]; | |
// Checking the type of the document property and then storing the value of the | |
// document property according to that type | |
if (customProperty2.Type == Aspose.Cells.Properties.PropertyType.String) | |
{ | |
string value = customProperty2.Value.ToString(); | |
Console.WriteLine(customProperty2.Name + " : " + value); | |
} |
カスタムドキュメントプロパティの追加または削除方法
このトピックの冒頭で既に説明した通り、ビルトインプロパティはシステム定義されたものであり、開発者は追加または削除することはできませんが、ユーザー定義のカスタムプロパティを追加または削除することは可能です。
カスタムプロパティの追加方法
Aspose.Cells APIでは、カスタムプロパティをコレクションに追加するために、AddクラスのCustomDocumentPropertyCollectionメソッドが公開されています。Addメソッドは、プロパティをExcelファイルに追加して新しいドキュメントプロパティの参照をAspose.Cells.Properties.DocumentPropertyオブジェクトとして返します。
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
// Instantiate a Workbook object | |
// Open an Excel file | |
Workbook workbook = new Workbook(dataDir + "sample-document-properties.xlsx"); | |
// Retrieve a list of all custom document properties of the Excel file | |
Aspose.Cells.Properties.CustomDocumentPropertyCollection customProperties = workbook.Worksheets.CustomDocumentProperties; | |
// Adding a custom document property to the Excel file | |
Aspose.Cells.Properties.DocumentProperty publisher = customProperties.Add("Publisher", "Aspose"); | |
// Saving resultant spreadsheet | |
workbook.Save(dataDir + "out_sample-document-properties.xlsx"); |
「コンテンツにリンク」カスタムプロパティの構成方法
特定の範囲のコンテンツにリンクされたカスタムプロパティを作成するには、CustomDocumentPropertyCollection.AddLinkToContentメソッドを呼び出してプロパティ名とソースを渡します。 プロパティがコンテンツにリンクされているかどうかを確認するにはDocumentProperty.IsLinkedToContentプロパティを使用できます。 また、DocumentPropertyクラスのSourceプロパティを使用してソース範囲を取得することも可能です。
この例では、シンプルなテンプレートのMicrosoft Excelファイルを使用します。 ワークブックには、MyRangeと表記された名前付き範囲があり、セルの値を参照しています。
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
// Instantiate an object of Workbook | |
// Open an Excel file | |
Workbook workbook = new Workbook(dataDir + "sample-document-properties.xlsx"); | |
// Retrieve a list of all custom document properties of the Excel file | |
Aspose.Cells.Properties.CustomDocumentPropertyCollection customProperties = workbook.Worksheets.CustomDocumentProperties; | |
// Add link to content. | |
customProperties.AddLinkToContent("Owner", "MyRange"); | |
// Accessing the custom document property by using the property name | |
Aspose.Cells.Properties.DocumentProperty customProperty1 = customProperties["Owner"]; | |
// Check whether the property is lined to content | |
bool islinkedtocontent = customProperty1.IsLinkedToContent; | |
// Get the source for the property | |
string source = customProperty1.Source; | |
// Save the file | |
workbook.Save(dataDir + "out_sample-document-properties.xlsx"); |
カスタムプロパティを削除する方法
Aspose.Cellsを使用してカスタムプロパティを削除するには、DocumentPropertyCollection.Removeメソッドを呼び出して削除するドキュメントプロパティの名前を渡します。
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
// Instantiate a Workbook object | |
// Open an Excel file | |
Workbook workbook = new Workbook(dataDir + "sample-document-properties.xlsx"); | |
// Retrieve a list of all custom document properties of the Excel file | |
Aspose.Cells.Properties.DocumentPropertyCollection customProperties = workbook.Worksheets.CustomDocumentProperties; | |
// Removing a custom document property | |
customProperties.Remove("Publisher"); | |
// Save the file | |
workbook.Save(dataDir + "out_sample-document-properties.xlsx"); |