Excel および ODS ファイルの条件付き書式を設定する。

紹介

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

Aspose.Cells for Python via .NET は、セルに条件付き書式をランタイムで適用することをサポートしています。詳しく説明します。また、Excelが色スケール条件付き書式に使用する色の計算方法も解説します。

条件付き書式の適用

Aspose.Cells for Python via .NET は、いくつかの方法で条件付き書式をサポートしています:

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

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

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

コピー メソッドを使用

Aspose.Cells for Python via .NET は、開発者がワークシート内のセル間で条件付き書式設定をコピーできるように、Range.copy()メソッドを提供します。

from aspose.cells import Workbook
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET
# The path to the documents directory.
dataDir = RunExamples.GetDataDir(".")
# Creating a file stream containing the Excel file to be opened
fstream = open(dataDir + "Book1.xlsx", "rb")
# Opening the Excel file through the file stream
workbook = Workbook(fstream)
# Accessing the first worksheet in the Excel file
worksheet = workbook.worksheets[0]
# Copying conditional format settings from cell "A1" to cell "B1"
# worksheet.CopyConditionalFormatting(0, 0, 0, 1);
TotalRowCount = 0
for i in range(len(workbook.worksheets)):
sourceSheet = workbook.worksheets[i]
sourceRange = sourceSheet.cells.max_display_range
destRange = worksheet.cells.create_range(sourceRange.first_row + TotalRowCount, sourceRange.first_column, sourceRange.row_count, sourceRange.column_count)
destRange.copy(sourceRange)
TotalRowCount = sourceRange.row_count + TotalRowCount
# Saving the modified Excel file
workbook.save(dataDir + "output.xls")
# Closing the file stream to free all resources
fstream.close()

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

Aspose.Cells for Python via .NET では、実行時に条件付き書式を追加および削除できます。以下のコード例は、条件付き書式を設定する方法を示しています:

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

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

Microsoft Excel 2007は、より高度な条件付き書式を追加しており、Aspose.Cells for Python via .NETもこれをサポートしています。ここでの例は、シンプルな書式設定の使用方法を示し、Microsoft Excel 2007の例ではより高度な条件付き書式の適用方法を示しています。

from aspose.cells import CellArea, FormatConditionType, OperatorType, Workbook
from aspose.pydrawing import Color
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET
# The path to the documents directory.
dataDir = RunExamples.GetDataDir(".")
filePath = dataDir + "Book1.xlsx"
# Instantiating a Workbook object
workbook = Workbook()
sheet = workbook.worksheets[0]
# Adds an empty conditional formatting
index = sheet.conditional_formattings.add()
fcs = sheet.conditional_formattings[index]
# Sets the conditional format range.
ca = CellArea()
ca.start_row = 0
ca.end_row = 0
ca.start_column = 0
ca.end_column = 0
fcs.add_area(ca)
ca = CellArea()
ca.start_row = 1
ca.end_row = 1
ca.start_column = 1
ca.end_column = 1
fcs.add_area(ca)
# Adds condition.
conditionIndex = fcs.add_condition(FormatConditionType.CELL_VALUE, OperatorType.BETWEEN, "=A2", "100")
# Adds condition.
conditionIndex2 = fcs.add_condition(FormatConditionType.CELL_VALUE, OperatorType.BETWEEN, "50", "100")
# Sets the background color.
fc = fcs[conditionIndex]
fc.style.background_color = Color.red
# Saving the Excel file
workbook.save(dataDir + "output.xls")

フォントの設定

from aspose.cells import SaveFormat, Workbook
from os import os, path
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET
# The path to the documents directory.
dataDir = RunExamples.GetDataDir(".")
# Create directory if it is not already present.
IsExists = path.isdir(dataDir)
if notIsExists:
os.makedirs(dataDir)
# Instantiating a Workbook object
workbook = Workbook()
# Adding a new worksheet to the Excel object
i = workbook.worksheets.add()
# Obtaining the reference of the newly added worksheet by passing its sheet index
worksheet = workbook.worksheets[i]
# Accessing the "A1" cell from the worksheet
cell = worksheet.cells.get("A1")
# Adding some value to the "A1" cell
cell.put_value("Hello Aspose!")
# Obtaining the style of the cell
style = cell.get_style()
# Setting the font weight to bold
style.font.is_bold = True
# Applying the style to the cell
cell.set_style(style)
# Saving the Excel file
workbook.save(dataDir + "book1.out.xls", SaveFormat.EXCEL_97_TO_2003)

境界線の設定

from aspose.cells import BorderType, CellArea, CellBorderType, FormatConditionType, OperatorType, Workbook
from aspose.pydrawing import Color
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET
# The path to the documents directory.
dataDir = RunExamples.GetDataDir(".")
# Instantiating a Workbook object
workbook = Workbook()
sheet = workbook.worksheets[0]
# Adds an empty conditional formatting
index = sheet.conditional_formattings.add()
fcs = sheet.conditional_formattings[index]
# Sets the conditional format range.
ca = CellArea()
ca.start_row = 0
ca.end_row = 5
ca.start_column = 0
ca.end_column = 3
fcs.add_area(ca)
# Adds condition.
conditionIndex = fcs.add_condition(FormatConditionType.CELL_VALUE, OperatorType.BETWEEN, "50", "100")
# Sets the background color.
fc = fcs[conditionIndex]
fc.style.borders.get(BorderType.LEFT_BORDER).line_style = CellBorderType.DASHED
fc.style.borders.get(BorderType.RIGHT_BORDER).line_style = CellBorderType.DASHED
fc.style.borders.get(BorderType.TOP_BORDER).line_style = CellBorderType.DASHED
fc.style.borders.get(BorderType.BOTTOM_BORDER).line_style = CellBorderType.DASHED
fc.style.borders.get(BorderType.LEFT_BORDER).color = Color.from_argb(0, 255, 255)
fc.style.borders.get(BorderType.RIGHT_BORDER).color = Color.from_argb(0, 255, 255)
fc.style.borders.get(BorderType.TOP_BORDER).color = Color.from_argb(0, 255, 255)
fc.style.borders.get(BorderType.BOTTOM_BORDER).color = Color.from_argb(255, 255, 0)
workbook.save(dataDir + "output.xlsx")

パターンの設定

from aspose.cells import BackgroundType, CellArea, FormatConditionType, OperatorType, Workbook
from aspose.pydrawing import Color
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET
# The path to the documents directory.
dataDir = RunExamples.GetDataDir(".")
# Instantiating a Workbook object
workbook = Workbook()
sheet = workbook.worksheets[0]
# Adds an empty conditional formatting
index = sheet.conditional_formattings.add()
fcs = sheet.conditional_formattings[index]
# Sets the conditional format range.
ca = CellArea()
ca.start_row = 0
ca.end_row = 5
ca.start_column = 0
ca.end_column = 3
fcs.add_area(ca)
# Adds condition.
conditionIndex = fcs.add_condition(FormatConditionType.CELL_VALUE, OperatorType.BETWEEN, "50", "100")
fc = fcs[conditionIndex]
fc.style.pattern = BackgroundType.REVERSE_DIAGONAL_STRIPE
fc.style.foreground_color = Color.from_argb(255, 255, 0)
fc.style.background_color = Color.from_argb(0, 255, 255)
workbook.save(dataDir + "output.xlsx")

高度なトピック