Node.jsとC++を使用したコメントとメモの管理

紹介

コメントはセルに追加情報を付加するために使用されます。Aspose.Cells for Node.js via C++はセルにコメントを追加する二つの方法を提供します。一つはデザイナーファイルで手動でコメントを作成し、それをAspose.Cellsでインポートする方法です。もう一つは、実行時にAspose.Cells APIを使用してコメントを追加する方法です。このトピックでは、Aspose.Cells APIを使ったセルへのコメント追加方法について説明します。コメントの書式設定も解説します。

コメントの追加

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

const path = require("path");
const AsposeCells = require("aspose.cells.node");

// The path to the documents directory.
const dataDir = path.join(__dirname, "data");

// Instantiating a Workbook object
const workbook = new AsposeCells.Workbook();

// Adding a new worksheet to the Workbook object
const sheetIndex = workbook.getWorksheets().add();

// Obtaining the reference of the newly added worksheet by passing its sheet index
const worksheet = workbook.getWorksheets().get(sheetIndex);

// Adding a comment to "F5" cell
const commentIndex = worksheet.getComments().add("F5");

// Accessing the newly added comment
const comment = worksheet.getComments().get(commentIndex);

// Setting the comment note
comment.setNote("Hello Aspose!");

// Saving the Excel file
workbook.save(path.join(dataDir, "book1.out.xls"));

コメントの書式設定

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

const fs = require("fs");
const path = require("path");
const AsposeCells = require("aspose.cells.node");

// The path to the documents directory.
const dataDir = path.join(__dirname, "data");

// Instantiating a Workbook object
const workbook = new AsposeCells.Workbook();

// Adding a new worksheet to the Workbook object
const sheetIndex = workbook.getWorksheets().add();

// Obtaining the reference of the newly added worksheet by passing its sheet index
const worksheet = workbook.getWorksheets().get(sheetIndex);

// Adding a comment to "F5" cell
const commentIndex = worksheet.getComments().add("F5");

// Accessing the newly added comment
const 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().setIsBold(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(path.join(dataDir, "book1.out.xls"));

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

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

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

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

const path = require("path");
const fs = require("fs");
const AsposeCells = require("aspose.cells.node");

// The path to the documents directory.
const dataDir = path.join(__dirname, "data");

// Instantiate a Workbook
const workbook = new AsposeCells.Workbook();

// Get a reference of comments collection with the first sheet
const comments = workbook.getWorksheets().get(0).getComments();

// Add a comment to cell A1
const commentIndex = comments.add(0, 0);
const comment = comments.get(commentIndex);
comment.setNote("First note.");
comment.getFont().setName("Times New Roman");

// Load an image into stream
const bmpPath = path.join(dataDir, "logo.jpg");
const bmpData = fs.readFileSync(bmpPath);

// Set image data to the shape associated with the comment
comment.getCommentShape().getFill().setImageData(bmpData);

// Save the workbook
workbook.save(path.join(dataDir, "book1.out.xlsx"), AsposeCells.SaveFormat.Xlsx);

高度なトピック