Node.jsを使ったC++によるセルの書式設定
紹介
GetStyleおよびSetStyleメソッドを使用してセルの書式設定する方法
セルに異なる種類の書式設定スタイルを適用して、背景色や前景色、枠線、フォント、水平および垂直の配置、インデントレベル、テキストの方向、回転角などを設定する。
GetStyle および SetStyle メソッドの使用方法
開発者が異なるセルに異なる書式スタイルを適用する必要がある場合は、Cell.getStyle()メソッドを使用してセルのStyleを取得し、スタイル属性を指定してから、Cell.setStyle(Style)メソッドを使用して書式設定を適用するのが良いです。以下は、このアプローチを示す例です。
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();
// Obtaining the reference of the first worksheet
const worksheet = workbook.getWorksheets().get(0);
// Accessing the "A1" cell from the worksheet
const cell = worksheet.getCells().get("A1");
// Adding some value to the "A1" cell
cell.putValue("Hello Aspose!");
// Defining a Style object
let style;
// Get the style from A1 cell
style = cell.getStyle();
// Setting the vertical alignment
style.setVerticalAlignment(AsposeCells.TextAlignmentType.Center);
// Setting the horizontal alignment
style.setHorizontalAlignment(AsposeCells.TextAlignmentType.Center);
// Setting the font color of the text
style.getFont().setColor(AsposeCells.Color.Green);
// Setting to shrink according to the text contained in it
style.setShrinkToFit(true);
// Setting the bottom border color to red
style.getBorders().get(AsposeCells.BorderType.BottomBorder).setColor(AsposeCells.Color.Red);
// Setting the bottom border type to medium
style.getBorders().get(AsposeCells.BorderType.BottomBorder).setLineStyle(AsposeCells.CellBorderType.Medium);
// Applying the style to A1 cell
cell.setStyle(style);
// Saving the Excel file
workbook.save(path.join(dataDir, "book1.out.xls"));
異なるセルのフォーマットにスタイルオブジェクトを使用する方法
開発者が複数のセルに同じ書式スタイルを適用したい場合は、Styleオブジェクトを使用できます。Styleオブジェクトの使用手順は以下のとおりです。
- WorkbookクラスのcreateStyle()メソッドを呼び出して、Styleオブジェクトを追加します。
- 新たに追加されたStyleオブジェクトにアクセスします。
- 望む書式設定を適用するために、Styleオブジェクトのプロパティ/属性を設定します。
- 設定済みのStyleオブジェクトを目的のセルに割り当てます。
このアプローチは、アプリケーションの効率を大幅に改善し、メモリも節約できます。
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 Excel object
const i = workbook.getWorksheets().add();
// Obtaining the reference of the first worksheet by passing its sheet index
const worksheet = workbook.getWorksheets().get(i);
// Accessing the "A1" cell from the worksheet
const cell = worksheet.getCells().get("A1");
// Adding some value to the "A1" cell
cell.putValue("Hello Aspose!");
// Adding a new Style
const style = workbook.createStyle();
// Setting the vertical alignment of the text in the "A1" cell
style.setVerticalAlignment(AsposeCells.TextAlignmentType.Center);
// Setting the horizontal alignment of the text in the "A1" cell
style.setHorizontalAlignment(AsposeCells.TextAlignmentType.Center);
// Setting the font color of the text in the "A1" cell
style.getFont().setColor(AsposeCells.Color.Green);
// Shrinking the text to fit in the cell
style.setShrinkToFit(true);
// Setting the bottom border color of the cell to red
style.getBorders().get(AsposeCells.BorderType.BottomBorder).setColor(AsposeCells.Color.Red);
// Setting the bottom border type of the cell to medium
style.getBorders().get(AsposeCells.BorderType.BottomBorder).setLineStyle(AsposeCells.CellBorderType.Medium);
// Assigning the Style object to the "A1" cell
cell.setStyle(style);
// Apply the same style to some other cells
worksheet.getCells().get("B1").setStyle(style);
worksheet.getCells().get("C1").setStyle(style);
worksheet.getCells().get("D1").setStyle(style);
// Saving the Excel file
workbook.save(path.join(dataDir, "book1.out.xls"));
Microsoft Excel 2007 の事前定義されたスタイルの使用方法
Microsoft Excel 2007 に異なる書式スタイルを適用する必要がある場合、Aspose.Cells API を使用してスタイルを適用します。以下に、セルに事前定義されたスタイルを適用するこのアプローチをデモンストレーションするための例が示されています。
const path = require("path");
const AsposeCells = require("aspose.cells.node");
// The path to the documents directory.
const dataDir = path.join(__dirname, "data");
// Instantiate a new Workbook.
const workbook = new AsposeCells.Workbook();
// Create a style object.
const style = workbook.createStyle();
// Input a value to A1 cell.
workbook.getWorksheets().get(0).getCells().get("A1").putValue("Test");
// Apply the style to the cell.
workbook.getWorksheets().get(0).getCells().get("A1").setStyle(style);
// Save the Excel 2007 file.
workbook.save(path.join(dataDir, "book1.out.xlsx"));
セル内の選択された文字の書式設定方法
フォント設定の取り扱いは、セル内のテキストの書式設定方法について説明していますが、セルの内容全体の書式設定方法のみを説明しています。しかし、選択した文字のみを書式設定する場合はどうすればよいでしょうか?
Aspose.Cellsもこの機能をサポートしています。このトピックでは、その機能を効果的に使用する方法について説明します。
選択された文字の書式設定方法
Aspose.Cellsは、Microsoft Excelファイルを表すWorkbookクラスを提供します。Workbookクラスは、Excelファイル内の各ワークシートにアクセスできるgetWorksheets()コレクションを含みます。ワークシートはWorksheetクラスによって表されます。Worksheetクラスは、getCells()コレクションを提供します。getCells()コレクションの各要素はCellクラスのオブジェクトを表します。
Cellクラスは、セル内の文字列の範囲を選択するためのcharacters(number, number)メソッドを提供し、以下のパラメータを取ります。
- 開始インデックス、選択を開始する文字のインデックス。
- 文字数、選択する文字数。
characters(number, number)メソッドは、開発者がセル内の文字列を形式設定できるようにするFontSettingクラスのインスタンスを返します。コード例のように、出力ファイルのA1セルには「Visit」という単語はデフォルトのフォントでフォーマットされ、「Aspose!」は太字で青色で表示されます。
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();
// Obtaining the reference of the first(default) worksheet by passing its sheet index
const worksheet = workbook.getWorksheets().get(0);
// Accessing the "A1" cell from the worksheet
const cell = worksheet.getCells().get("A1");
// Adding some value to the "A1" cell
cell.putValue("Visit Aspose!");
// Setting the font of selected characters to bold
const font = cell.characters(6, 7).getFont();
font.isBold = true;
// Setting the font color of selected characters to blue
font.color = AsposeCells.Color.Blue;
// Saving the Excel file
workbook.save(path.join(dataDir, "book1.out.xls"));
行および列の書式設定方法
時には、開発者は行または列に同じ書式を適用する必要があります。セルごとに書式を適用することは時間がかかるため、良い解決策ではありません。 この問題に対処するために、Aspose.Cells がこの記事で詳しく説明しているシンプルで高速な方法を提供します。
行および列の書式設定
Aspose.Cellsは、Microsoft Excelファイルを表すWorkbookクラスを提供します。Workbookクラスは、Excelファイル内の各ワークシートへのアクセスを可能にするgetWorksheets()コレクションを含みます。ワークシートはWorksheetクラスによって表されます。WorksheetクラスはgetCells()コレクションを提供し、getCells()コレクションはさらにgetRows()コレクションを提供します。
行の書式設定方法
getRows()コレクションの各アイテムはRowオブジェクトを表します。Rowオブジェクトは、行の書式設定に使用されるapplyStyle(Style, StyleFlag)メソッドを提供します。同じ書式を行に適用するには、Styleオブジェクトを使用します。以下にその使用方法を示します。
- WorkbookクラスにcreateStyle()メソッドを呼び出して、Styleオブジェクトを追加します。
- Styleオブジェクトのプロパティを設定して、書式設定を適用します。
- relevant属性をONにします。
- 設定済みのStyleオブジェクトをRowオブジェクトに割り当てます。
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();
// Obtaining the reference of the first (default) worksheet by passing its sheet index
const worksheet = workbook.getWorksheets().get(0);
// Adding a new Style to the styles
const style = workbook.createStyle();
// Setting the vertical alignment of the text in the "A1" cell
style.setVerticalAlignment(AsposeCells.TextAlignmentType.Center);
// Setting the horizontal alignment of the text in the "A1" cell
style.setHorizontalAlignment(AsposeCells.TextAlignmentType.Center);
// Setting the font color of the text in the "A1" cell
style.getFont().setColor(AsposeCells.Color.Green);
// Shrinking the text to fit in the cell
style.setShrinkToFit(true);
// Setting the bottom border color of the cell to red
style.getBorders().get(AsposeCells.BorderType.BottomBorder).setColor(AsposeCells.Color.Red);
// Setting the bottom border type of the cell to medium
style.getBorders().get(AsposeCells.BorderType.BottomBorder).setLineStyle(AsposeCells.CellBorderType.Medium);
// Creating StyleFlag
const styleFlag = new AsposeCells.StyleFlag();
styleFlag.setHorizontalAlignment(true);
styleFlag.setVerticalAlignment(true);
styleFlag.setShrinkToFit(true);
styleFlag.setBorders(true);
styleFlag.setFontColor(true);
// Accessing a row from the Rows collection
const row = worksheet.getCells().getRows().get(0);
// Assigning the Style object to the Style property of the row
row.applyStyle(style, styleFlag);
// Saving the Excel file
workbook.save(path.join(dataDir, "book1.out.xls"));
列のフォーマット方法
getCells()コレクションは、getColumns()コレクションも提供します。getColumns()コレクション内の各アイテムはColumnオブジェクトを表します。Rowオブジェクトと同様に、Columnオブジェクトも列の書式設定にapplyStyle(Style, StyleFlag)メソッドを提供します。
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();
// Obtaining the reference of the first (default) worksheet by passing its sheet index
const worksheet = workbook.getWorksheets().get(0);
// Adding a new Style to the styles
const style = workbook.createStyle();
// Setting the vertical alignment of the text in the "A1" cell
style.setVerticalAlignment(AsposeCells.TextAlignmentType.Center);
// Setting the horizontal alignment of the text in the "A1" cell
style.setHorizontalAlignment(AsposeCells.TextAlignmentType.Center);
// Setting the font color of the text in the "A1" cell
style.getFont().setColor(AsposeCells.Color.Green);
// Shrinking the text to fit in the cell
style.setShrinkToFit(true);
// Setting the bottom border color of the cell to red
style.getBorders().get(AsposeCells.BorderType.BottomBorder).setColor(AsposeCells.Color.Red);
// Setting the bottom border type of the cell to medium
style.getBorders().get(AsposeCells.BorderType.BottomBorder).setLineStyle(AsposeCells.CellBorderType.Medium);
// Creating StyleFlag
const styleFlag = new AsposeCells.StyleFlag();
styleFlag.setHorizontalAlignment(true);
styleFlag.setVerticalAlignment(true);
styleFlag.setShrinkToFit(true);
styleFlag.setBorders(true);
styleFlag.setFontColor(true);
// Accessing a column from the Columns collection
const column = worksheet.getCells().getColumns().get(0);
// Applying the style to the column
column.applyStyle(style, styleFlag);
// Saving the Excel file
workbook.save(path.join(dataDir, "book1.out.xls"));