コメントとノートの管理

紹介

コメントはセルに追加情報を追加するために使用されます。Aspose.Cellsでは、セルにコメントを追加するための2つの方法が提供されます。最初の方法は、デザイナーファイルで手動でコメントを作成することです。これらのコメントはその後、Aspose.Cellsを使用してインポートされます。2番目は、Aspose.Cells APIを使用してランタイムでコメントを追加することです。このトピックでは、Aspose.Cells APIを使用してセルにコメントを追加する方法について説明します。コメントの書式設定についても説明します。

コメントの追加

Comments セレクションの追加 メソッド ( Worksheet オブジェクトにカプセル化されています) を呼び出して、セルにコメントを追加します。新しい Comment オブジェクトは、コメントインデックスを渡して Comments コレクションからアクセスできます。 Comment オブジェクトにアクセスした後に、 Comment オブジェクトの Note プロパティを使ってコメントノートをカスタマイズします。

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = "";
// Create directory if it is not already present.
File file = new File(dataDir);
if(!file.exists())
file.mkdir();
// Instantiating a Workbook object
Workbook workbook = new Workbook();
// Adding a new worksheet to the Workbook object
int sheetIndex = workbook.getWorksheets().add();
// Obtaining the reference of the newly added worksheet by passing its sheet index
Worksheet worksheet = workbook.getWorksheets().get(sheetIndex);
// Adding a comment to "F5" cell
int commentIndex = worksheet.getComments().add("F5");
// Accessing the newly added comment
com.aspose.cells.Comment comment = worksheet.getComments().get(commentIndex);
// Setting the comment note
comment.setNote( "Hello Aspose!");
// Saving the Excel file
workbook.save(dataDir + "book1.out.xls");

コメントの書式設定

コメントの高さ、幅、フォント設定を構成することで、コメントの外観を書式設定することも可能です。

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = "";
// Create directory if it is not already present.
File file = new File(dataDir);
if(!file.exists())
file.mkdir();
// Instantiating a Workbook object
Workbook workbook = new Workbook();
// Adding a new worksheet to the Workbook object
int sheetIndex = workbook.getWorksheets().add();
// Obtaining the reference of the newly added worksheet by passing its sheet index
Worksheet worksheet = workbook.getWorksheets().get(sheetIndex);
// Adding a comment to "F5" cell
int commentIndex = worksheet.getComments().add("F5");
// Accessing the newly added comment
com.aspose.cells.Comment comment = worksheet.getComments().get(commentIndex);
// Setting the comment note
comment.setNote( "Hello Aspose!");
// Setting the font size of a comment to 14
comment.getFont().setSize(14);
// Setting the font of a comment to bold
comment.getFont().setBold(true);
// Setting the height of the font to 10
comment.setHeightCM(10);
// Setting the width of the font to 2
comment.setWidthCM(2);
// Saving the Excel file
workbook.save(dataDir + "book1.out.xls");

コメントに画像を追加する

Microsoft Excel 2007では、セルコメントの背景として画像を使用することもできます。Excel 2007では、次の手順を実行することでこれが実現されます。(すでにセルにコメントを追加していることを前提とします。)

  1. コメントを含むセルを右クリックします。
  2. 表示/非表示を選択し、コメント内のテキストをクリアします。
  3. コメントの境界線をクリックして選択します。
  4. 書式、次にコメントを選択します。
  5. 色と線タブで、リストを展開します。
  6. 塗りつぶしの効果をクリックします。
  7. タブで、ピクチャを選択をクリックします。
  8. 画像を探し、選択します。
  9. すべてのダイアログが閉じるまでOKをクリックします。

Aspose.Cellsもこの機能を提供します。以下は、セル"A1"に画像を背景として設定したコメントを追加し、からXLSXファイルをスクラッチから作成するコードサンプルです。

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = "";
// Create directory if it is not already present.
File file = new File(dataDir);
if(!file.exists())
file.mkdir();
// Instantiating a Workbook object
Workbook workbook = new Workbook();
// Get a reference of comments collection with the first sheet
com.aspose.cells.CommentCollection comments = workbook.getWorksheets().get(0).getComments();
// Add a comment to cell A1
int commentIndex = comments.add(0, 0);
com.aspose.cells.Comment comment = comments.get(commentIndex);
comment.setNote("First note.");
comment.getFont().setName("Times New Roman");
// Load an image
String filename = dataDir + "image.jpg";
byte[] result = null;
java.nio.channels.FileChannel fc = null;
try {
fc = new java.io.RandomAccessFile(filename, "r").getChannel();
java.nio.MappedByteBuffer byteBuffer = fc.map(java.nio.channels.FileChannel.MapMode.READ_ONLY, 0,
fc.size()).load();
System.out.println(byteBuffer.isLoaded());
result = new byte[(int) fc.size()];
if (byteBuffer.remaining() > 0) {
byteBuffer.get(result, 0, byteBuffer.remaining());
}
} catch (IOException e) {
throw e;
} finally {
try {
fc.close();
} catch (IOException e) {
throw e;
}
}
// Set image data to the shape associated with the comment
comment.getCommentShape().getFill().setImageData(result);
// Saving the Excel file
workbook.save(dataDir + "book1.out.xlsx");

高度なトピック