2色系規則と3色系規則の条件付き書式設定を追加する
Contents
[
Hide
]
2色系規則と3色系規則の条件付き書式設定は、FormatCondition.ColorScale.Is3ColorScaleプロパティによって異なります。このプロパティは2色系規則ではfalseであり、3色系規則ではtrueです。
2色系規則と3色系規則の条件付き書式設定を追加する
次のサンプルコードは、2色系規則と3色系規則の条件付き書式設定を追加します。これにより、出力Excelファイルが生成されます。
This file contains 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
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java | |
String dataDir = Utils.getDataDir(AddingTwoAndThreeColorScale.class); | |
// Create workbook | |
Workbook workbook = new Workbook(); | |
// Access first worksheet | |
Worksheet worksheet = workbook.getWorksheets().get(0); | |
// Add some data in cells | |
worksheet.getCells().get("A1").putValue("2-Color Scale"); | |
worksheet.getCells().get("D1").putValue("3-Color Scale"); | |
for (int i = 2; i <= 15; i++) { | |
worksheet.getCells().get("A" + i).putValue(i); | |
worksheet.getCells().get("D" + i).putValue(i); | |
} | |
// Adding 2-Color Scale Conditional Formatting | |
CellArea ca = CellArea.createCellArea("A2", "A15"); | |
int idx = worksheet.getConditionalFormattings().add(); | |
FormatConditionCollection fcc = worksheet.getConditionalFormattings().get(idx); | |
fcc.addCondition(FormatConditionType.COLOR_SCALE); | |
fcc.addArea(ca); | |
FormatCondition fc = worksheet.getConditionalFormattings().get(idx).get(0); | |
fc.getColorScale().setIs3ColorScale(false); | |
fc.getColorScale().setMaxColor(Color.getLightBlue()); | |
fc.getColorScale().setMinColor(Color.getLightGreen()); | |
// Adding 3-Color Scale Conditional Formatting | |
ca = CellArea.createCellArea("D2", "D15"); | |
idx = worksheet.getConditionalFormattings().add(); | |
fcc = worksheet.getConditionalFormattings().get(idx); | |
fcc.addCondition(FormatConditionType.COLOR_SCALE); | |
fcc.addArea(ca); | |
fc = worksheet.getConditionalFormattings().get(idx).get(0); | |
fc.getColorScale().setIs3ColorScale(true); | |
fc.getColorScale().setMaxColor(Color.getLightBlue()); | |
fc.getColorScale().setMidColor(Color.getYellow()); | |
fc.getColorScale().setMinColor(Color.getLightGreen()); | |
// Save the workbook | |
workbook.save(dataDir + "output.xlsx"); |