添加二色比例和三色比例条件格式
Contents
[
Hide
]
双色标度和三色标度条件格式的添加方式相同,不同之处在于 ColorScale.setIs3ColorScale(boolean) 方法。对于双色标度,此方法为 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
const path = require("path"); | |
const AsposeCells = require("aspose.cells.node"); | |
// The path to the documents directory. | |
const dataDir = path.join(__dirname, "data"); | |
const filePath = path.join(dataDir, "sample.xlsx"); | |
// Create workbook | |
const workbook = new AsposeCells.Workbook(); | |
// Access first worksheet | |
const 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 (let 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 | |
let ca = AsposeCells.CellArea.createCellArea("A2", "A15"); | |
let idx = worksheet.getConditionalFormattings().add(); | |
let fcc = worksheet.getConditionalFormattings().get(idx); | |
fcc.addCondition(AsposeCells.FormatConditionType.ColorScale); | |
fcc.addArea(ca); | |
let fc = worksheet.getConditionalFormattings().get(idx).get(0); | |
fc.getColorScale().setIs3ColorScale(false); | |
fc.getColorScale().setMaxColor(AsposeCells.Color.LightBlue); | |
fc.getColorScale().setMinColor(AsposeCells.Color.LightGreen); | |
// Adding 3-Color Scale Conditional Formatting | |
ca = AsposeCells.CellArea.createCellArea("D2", "D15"); | |
idx = worksheet.getConditionalFormattings().add(); | |
fcc = worksheet.getConditionalFormattings().get(idx); | |
fcc.addCondition(AsposeCells.FormatConditionType.ColorScale); | |
fcc.addArea(ca); | |
fc = worksheet.getConditionalFormattings().get(idx).get(0); | |
fc.getColorScale().setIs3ColorScale(true); | |
fc.getColorScale().setMaxColor(AsposeCells.Color.LightBlue); | |
fc.getColorScale().setMidColor(AsposeCells.Color.Yellow); | |
fc.getColorScale().setMinColor(AsposeCells.Color.LightGreen); | |
// Save the workbook | |
workbook.save(path.join(dataDir, "output_out.xlsx")); |