添加二色比例和三色比例条件格式
Contents
[
Hide
]
二色比例和三色比例条件格式的添加方式相同,只是它们由 FormatCondition.color_scale.is_3_color_scale 属性区分。此属性对于二色比例条件格式为 false,对于三色比例条件格式为 true。
以下示例代码添加了二色比例和三色比例条件格式。它生成了 输出 Excel 文件。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from aspose.cells import CellArea, FormatConditionType, 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(".") | |
# Create workbook | |
workbook = Workbook() | |
# Access first worksheet | |
worksheet = workbook.worksheets[0] | |
# Add some data in cells | |
worksheet.cells.get("A1").put_value("2-Color Scale") | |
worksheet.cells.get("D1").put_value("3-Color Scale") | |
for i in range(2, 15 + 1): | |
worksheet.cells.get("A" + str(i)).put_value(i) | |
worksheet.cells.get("D" + str(i)).put_value(i) | |
# Adding 2-Color Scale Conditional Formatting | |
ca = CellArea.create_cell_area("A2", "A15") | |
idx = worksheet.conditional_formattings.add() | |
fcc = worksheet.conditional_formattings[idx] | |
fcc.add_condition(FormatConditionType.COLOR_SCALE) | |
fcc.add_area(ca) | |
fc = worksheet.conditional_formattings[idx][0] | |
fc.color_scale.is_3_color_scale = False | |
fc.color_scale.max_color = Color.light_blue | |
fc.color_scale.min_color = Color.light_green | |
# Adding 3-Color Scale Conditional Formatting | |
ca = CellArea.create_cell_area("D2", "D15") | |
idx = worksheet.conditional_formattings.add() | |
fcc = worksheet.conditional_formattings[idx] | |
fcc.add_condition(FormatConditionType.COLOR_SCALE) | |
fcc.add_area(ca) | |
fc = worksheet.conditional_formattings[idx][0] | |
fc.color_scale.is_3_color_scale = True | |
fc.color_scale.max_color = Color.light_blue | |
fc.color_scale.mid_color = Color.yellow | |
fc.color_scale.min_color = Color.light_green | |
# Save the workbook | |
workbook.save(dataDir + "output_out.xlsx") |