C++でコメントやノートを管理する
紹介
コメントはセルに追加情報を追加するために使用されます。Aspose.Cellsでは、セルにコメントを追加するための2つの方法が提供されます。最初の方法は、デザイナーファイルで手動でコメントを作成することです。これらのコメントはその後、Aspose.Cellsを使用してインポートされます。2番目は、Aspose.Cells APIを使用してランタイムでコメントを追加することです。このトピックでは、Aspose.Cells APIを使用してセルにコメントを追加する方法について説明します。コメントの書式設定についても説明します。
コメントの追加
CommentsコレクションのAddメソッド(Worksheetオブジェクト内にカプセル化)を呼び出すことによって、セルにコメントを追加します。新しいCommentオブジェクトは、コメントインデックスを渡してCommentsコレクションからアクセスできます。Commentオブジェクトにアクセスした後、CommentオブジェクトのGetNote()プロパティを使用してコメントノートをカスタマイズします。
#include <iostream>
#include "Aspose.Cells.h"
using namespace Aspose::Cells;
int main()
{
Aspose::Cells::Startup();
// Source directory path
U16String srcDir(u"..\\Data\\01_SourceDirectory\\");
// Output directory path
U16String outDir(u"..\\Data\\02_OutputDirectory\\");
// Create a new workbook
Workbook workbook;
// Add a new worksheet to the workbook
int32_t sheetIndex = workbook.GetWorksheets().Add();
// Get the reference of the newly added worksheet
Worksheet worksheet = workbook.GetWorksheets().Get(sheetIndex);
// Add a comment to cell "F5"
int32_t commentIndex = worksheet.GetComments().Add(u"F5");
// Access the newly added comment
Comment comment = worksheet.GetComments().Get(commentIndex);
// Set the comment note
comment.SetNote(u"Hello Aspose!");
// Save the Excel file
U16String outputPath = outDir + u"book1.out.xls";
workbook.Save(outputPath);
std::cout << "Excel file saved successfully!" << std::endl;
Aspose::Cells::Cleanup();
}
コメントの書式設定
コメントの高さ、幅、フォント設定を構成することで、コメントの外観を書式設定することも可能です。
#include <iostream>
#include "Aspose.Cells.h"
using namespace Aspose::Cells;
int main()
{
Aspose::Cells::Startup();
// Source directory path
U16String srcDir(u"..\\Data\\01_SourceDirectory\\");
// Output directory path
U16String outDir(u"..\\Data\\02_OutputDirectory\\");
// Create workbook
Workbook workbook;
// Adding a new worksheet to the Workbook object
int32_t 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
int32_t commentIndex = worksheet.GetComments().Add(u"F5");
// Accessing the newly added comment
Comment comment = worksheet.GetComments().Get(commentIndex);
// Setting the comment note
comment.SetNote(u"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(outDir + u"book1.out.xls");
std::cout << "Excel file saved successfully!" << std::endl;
Aspose::Cells::Cleanup();
}
コメントに画像を追加する
Microsoft Excel 2007では、セルコメントの背景として画像を使用することもできます。Excel 2007では、次の手順を実行することでこれが実現されます。(すでにセルにコメントを追加していることを前提とします。)
- コメントを含むセルを右クリックします。
- 表示/非表示を選択し、コメント内のテキストをクリアします。
- コメントの境界線をクリックして選択します。
- 書式、次にコメントを選択します。
- 色と線タブで、色リストを展開します。
- 塗りつぶしの効果をクリックします。
- 図タブで、ピクチャを選択をクリックします。
- 画像を探し、選択します。
- すべてのダイアログが閉じるまでOKをクリックします。
Aspose.Cellsもこの機能を提供します。以下は、セル"A1"に画像を背景として設定したコメントを追加し、からXLSXファイルをスクラッチから作成するコードサンプルです。
#include <Aspose.Cells.h>
#include <fstream>
#include <vector>
#include <iostream>
using namespace Aspose::Cells;
int main()
{
Aspose::Cells::Startup();
U16String srcDir(u"../Data/01_SourceDirectory/");
U16String outDir(u"../Data/02_OutputDirectory/");
Workbook workbook;
WorksheetCollection worksheets = workbook.GetWorksheets();
Worksheet sheet = worksheets.Get(0);
CommentCollection comments = sheet.GetComments();
int32_t commentIndex = comments.Add(0, 0);
Comment comment = comments.Get(commentIndex);
comment.SetNote(u"First note.");
Font commentFont = comment.GetFont();
commentFont.SetName(u"Times New Roman");
U16String imagePath = srcDir + u"logo.jpg";
std::vector<uint8_t> imageData;
std::ifstream file(imagePath.ToUtf8(), std::ios::binary | std::ios::ate);
if (file)
{
std::streamsize size = file.tellg();
file.seekg(0, std::ios::beg);
imageData.resize(size);
file.read(reinterpret_cast<char*>(imageData.data()), size);
}
Vector<uint8_t> data(imageData.data(), static_cast<int32_t>(imageData.size()));
CommentShape shape = comment.GetCommentShape();
shape.GetFill().SetImageData(data);
U16String outputPath = outDir + u"book1.out.xlsx";
workbook.Save(outputPath, SaveFormat::Xlsx);
std::cout << "Workbook with image comment created successfully." << std::endl;
Aspose::Cells::Cleanup();
return 0;
}