ExcelとODSファイルの条件付き書式を設定する

紹介

条件付き書式は、高度な機能で、セルまたはセル範囲に書式を適用し、その書式がセルの値や式の値に応じて変化します。例えば、セルの値が500より大きい場合にのみ太字を表示することができます。 条件を満たすと指定された書式がセルに適用され、条件を満たさない場合は標準の書式が使用されます。Microsoft Excelでは、書式を選択し、次に条件付き書式をクリックして条件付き書式のダイアログを開きます。

Aspose.Cells はセルに条件付き書式を実行時に適用することをサポートしています。この記事ではその方法を説明します。また、Excel が色スケールの条件付き書式に使用する色の計算方法も説明します。

条件付き書式の適用

Aspose.Cells はいくつかの方法で条件付き書式をサポートしています。

  • デザイナー スプレッドシートを使用
  • コピー メソッドを使用
  • 実行時に条件付き書式を作成

デザイナー スプレッドシートを使用

開発者は、Microsoft Excel で条件付き書式を含むデザイナー スプレッドシートを作成し、そのスプレッドシートを Aspose.Cells で開くことができます。 Aspose.Cells は、デザイナー スプレッドシートを読み込み、保持し、条件付き書式の設定を保持します。

コピー メソッドを使用

Aspose.Cells は、ワークシート内のセルから別のセルへ条件付き書式設定をコピーすることができます。

const AsposeCells = require("aspose.cells.node");
const path = require("path");
// The path to the documents directory.
const dataDir = path.join(__dirname, "data");
// Open a template Excel file
const workbook = new AsposeCells.Workbook(path.join(dataDir, "book1.xlsx"));
// Get the first worksheet in the workbook
const sheet = workbook.getWorksheets().get(0);
// Get the A1 cell
const cell = sheet.getCells().get("A1");
// Get the conditional formatting result object
const cfr = cell.getConditionalFormattingResult();
// Get the icon set
const icon = cfr.getConditionalFormattingIcon();
// Create the image file based on the icon's image data
require("fs").writeFileSync(path.join(dataDir, "imgIcon.out.jpg"), icon.getImageData());

ランタイムで条件付き書式を適用

Aspose.Cells では、条件付き書式をランタイムで追加および削除することができます。以下のコードサンプルでは、条件付き書式の設定方法を示しています。

  1. ワークブックをインスタンス化してください。
  2. 空の条件付き書式を追加してください。
  3. 書式を適用する範囲を設定してください。
  4. 書式の条件を定義してください。
  5. ファイルを保存します。

この後に、フォント設定や罫線設定、パターン設定などの他の小さな例が続きます。

Microsoft Excel 2007はより高度な条件付き書式を追加し、Aspose.Cellsもサポートしています。ここでは簡単な書式設定の例を示し、Microsoft Excel 2007の例ではより高度な条件付き書式の適用方法を示します。

const AsposeCells = require("aspose.cells.node");
const path = require("path");
// The path to the documents directory.
const dataDir = path.join(__dirname, "data");
const filePath = path.join(dataDir, "Book1.xlsx");
// Instantiating a Workbook object
const workbook = new AsposeCells.Workbook();
const sheet = workbook.getWorksheets().get(0);
// Adds an empty conditional formatting
const index = sheet.getConditionalFormattings().getCount();
const fcs = sheet.getConditionalFormattings().get(index);
// Sets the conditional format range.
let ca = AsposeCells.CellArea.createCellArea(0, 0, 0, 0);
fcs.addArea(ca);
ca = AsposeCells.CellArea.createCellArea(1, 1, 1, 1);
fcs.addArea(ca);
// Adds condition.
const conditionIndex = fcs.addCondition(AsposeCells.FormatConditionType.CellValue, AsposeCells.OperatorType.Between, "=A2", "100");
// Adds condition.
const conditionIndex2 = fcs.addCondition(AsposeCells.FormatConditionType.CellValue, AsposeCells.OperatorType.Between, "50", "100");
// Sets the background color.
const fc = fcs.get(conditionIndex);
fc.getStyle().setBackgroundColor(AsposeCells.Color.Red);
// Saving the Excel file
workbook.save(path.join(dataDir, "output.xls"));

フォントの設定

const AsposeCells = require("aspose.cells.node");
const path = require("path");
// The path to the documents directory.
const dataDir = path.join(__dirname, "data");
// Open a template Excel file
const workbook = new AsposeCells.Workbook(path.join(dataDir, "book1.xlsx"));
// Get the first worksheet in the workbook
const sheet = workbook.getWorksheets().get(0);
// Get the A1 cell
const cell = sheet.getCells().get("A1");
// Get the conditional formatting result object
const cfr = cell.getConditionalFormattingResult();
// Get the icon set
const icon = cfr.getConditionalFormattingIcon();
// Create the image file based on the icon's image data
require("fs").writeFileSync(path.join(dataDir, "imgIcon.out.jpg"), icon.getImageData());

境界線の設定

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();
const sheet = workbook.getWorksheets().get(0);
// Adds an empty conditional formatting
const index = sheet.getConditionalFormattings().add();
const fcs = sheet.getConditionalFormattings().get(index);
// Sets the conditional format range.
const ca = AsposeCells.CellArea.createCellArea(0, 0, 5, 3);
fcs.addArea(ca);
// Adds condition.
const conditionIndex = fcs.addCondition(AsposeCells.FormatConditionType.CellValue, AsposeCells.OperatorType.Between, "50", "100");
// Sets the background color.
const fc = fcs.get(conditionIndex);
fc.getStyle().getBorders().get(AsposeCells.BorderType.LeftBorder).setLineStyle(AsposeCells.CellBorderType.Dashed);
fc.getStyle().getBorders().get(AsposeCells.BorderType.RightBorder).setLineStyle(AsposeCells.CellBorderType.Dashed);
fc.getStyle().getBorders().get(AsposeCells.BorderType.TopBorder).setLineStyle(AsposeCells.CellBorderType.Dashed);
fc.getStyle().getBorders().get(AsposeCells.BorderType.BottomBorder).setLineStyle(AsposeCells.CellBorderType.Dashed);
fc.getStyle().getBorders().get(AsposeCells.BorderType.LeftBorder).setColor(new AsposeCells.Color(0, 255, 255));
fc.getStyle().getBorders().get(AsposeCells.BorderType.RightBorder).setColor(new AsposeCells.Color(0, 255, 255));
fc.getStyle().getBorders().get(AsposeCells.BorderType.TopBorder).setColor(new AsposeCells.Color(0, 255, 255));
fc.getStyle().getBorders().get(AsposeCells.BorderType.BottomBorder).setColor(new AsposeCells.Color(255, 255, 0));
workbook.save(path.join(dataDir, "output.xlsx"));

パターンの設定

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();
const sheet = workbook.getWorksheets().get(0);
// Adds an empty conditional formatting
const index = sheet.getConditionalFormattings().add();
const fcs = sheet.getConditionalFormattings().get(index);
// Sets the conditional format range.
const ca = AsposeCells.CellArea.createCellArea(0, 0, 5, 3);
fcs.addArea(ca);
// Adds condition.
const conditionIndex = fcs.addCondition(AsposeCells.FormatConditionType.CellValue, AsposeCells.OperatorType.Between, "50", "100");
const fc = fcs.get(conditionIndex);
fc.getStyle().setPattern(AsposeCells.BackgroundType.ReverseDiagonalStripe);
fc.getStyle().setForegroundColor(new AsposeCells.Color(255, 255, 0));
fc.getStyle().setBackgroundColor(new AsposeCells.Color(0, 255, 255));
workbook.save(path.join(dataDir, "output.xlsx"));

高度なトピック