ワークシートに条件付き書式設定を適用する
この記事では、ワークシートのセル範囲に条件付き書式設定を追加する方法について詳しく説明しています。
条件付き書式設定は、Microsoft Excelの高度な機能であり、セルの値や数式の値に応じて書式を適用し、その書式を変更することを可能にします。たとえば、セルの背景が赤くなるとマイナスの値が強調表示されたり、テキストの色が緑色になるとプラスの値が強調表示されます。セルの値が条件を満たしている場合、書式が適用されます。セルの値が条件を満たしていない場合、セルのデフォルトの書式が使用されます。
Microsoft Office Automationで条件付き書式設定を適用することは可能ですが、その欠点があります。たとえば、セキュリティ、安定性、拡張性、速度などの理由や問題が存在します。他のソリューションを探す主な理由は、Microsoft自体がソフトウェアソリューション向けにOffice Automationを強く推奨していないことです。
この記事では、Aspose.Cells APIを使用して、わずか数行のコードでセルに条件付き書式設定を追加するコンソールアプリケーションを作成する方法を示しています。
セルの値に基づいた条件付き書式を適用するためのAspose.Cellsの使用
- Aspose.Cellsをダウンロードしてインストールします。
- Aspose.Cells for Python via .NETをダウンロードします。
- 開発コンピュータにインストールします。 すべてのAsposeのコンポーネントは、インストールされると評価モードで動作します。評価モードには時間制限はなく、生成された文書にウォーターマークを注入するだけです。
- プロジェクトを作成します。 Visual Studio.NETを起動し、新しいコンソールアプリケーションを作成します。この例ではPythonのコンソールアプリケーションを作成していますが、VB.NETでも使用できます。
- 参照を追加します。 プロジェクトにAspose.Cellsの参照を追加します。
- *セルの値に基づいて条件付き書式を適用します。 以下はそのタスクを達成するために使用されるコードです。セルに条件付き書式を適用します。
from aspose.cells import CellArea, FormatConditionType, OperatorType, SaveFormat, Workbook | |
from aspose.pydrawing import Color | |
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() | |
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) | |
# Adds condition. | |
conditionIndex = 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.out.xls", SaveFormat.AUTO) |
上記のコードを実行すると、出力ファイル(output.xls)の最初のワークシートのセル“A1”に条件付き書式が適用されます。A1のセル値に応じて条件付き書式が適用されます。A1のセル値が50から100の間の場合、条件付き書式が適用されて背景色が赤になります。
Aspose.Cellsを使用してセルの値に基づいた条件付き書式を適用する
- 数式に応じて条件付き書式を適用する(コードスニペット) 以下はそのタスクを達成するためのコードです。B3に条件付き書式を適用します。
from aspose.cells import CellArea, FormatConditionType, SaveFormat, Workbook | |
from aspose.pydrawing import Color | |
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() | |
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 = CellArea() | |
ca.start_row = 2 | |
ca.end_row = 2 | |
ca.start_column = 1 | |
ca.end_column = 1 | |
fcs.add_area(ca) | |
# Adds condition. | |
conditionIndex = fcs.add_condition(FormatConditionType.EXPRESSION) | |
# Sets the background color. | |
fc = fcs[conditionIndex] | |
fc.formula1 = "=IF(SUM(B1:B2)>100,TRUE,FALSE)" | |
fc.style.background_color = Color.red | |
sheet.cells.get("B3").formula = "=SUM(B1:B2)" | |
sheet.cells.get("C4").put_value("If Sum of B1:B2 is greater than 100, B3 will have RED background") | |
# Saving the Excel file | |
workbook.save(dataDir + "output.out.xls", SaveFormat.AUTO) |
上記のコードを実行すると、出力ファイル(output.xls)の最初のワークシートのセル“B3”に条件付き書式が適用されます。適用された条件付き書式は、B1とB2の値を合計する数式に依存します。