我对条件格式的顺序有疑问 我正在尝试制作一个具有 20 个条件格式的新 Excel 工作表 它们在某些单元格中重叠,但是当我打开工作表时,顺序颠倒了,我尝试颠倒代码中的范围
for i in (0..19).rev() {
,但它不起作用,我也尝试使用硬代码而不是循环,并按顺序播放代码和它总是给我相同的结果,我做错了什么吗?
let color_array: [(u32, u32); 20] = [
(0x0000ff, 0xffff00),
(0x80ff80, 0x000000),
(0xffff00, 0x3a00ff),
(0x0000ff, 0xffffff),
(0xff0000, 0xffffff),
(0x80ffff, 0x000000),
(0xff80ff, 0x000000),
(0xffff80, 0x000000),
(0x000000, 0xffffff),
(0xc0c0c0, 0xffffff),
(0x008000, 0xffffff),
(0x0000a0, 0xffffff),
(0x804000, 0xffffff),
(0x800080, 0xffffff),
(0xff8040, 0xffffff),
(0x8080c0, 0xffffff),
(0x808040, 0xffffff),
(0x808080, 0xffffff),
(0x408080, 0xffffff),
(0x8080c0, 0xffffff),
];
//
......
//
for i in 0..19 {
let form = Format::new()
.set_background_color(Color::RGB(color_array[i].0))
.set_font_color(Color::RGB(color_array[i].1))
.set_border_color(Color::RGB(BORDER_COLOR))
.set_border_top(FormatBorder::Thin)
.set_border_bottom(FormatBorder::Thin)
.set_bold()
.set_align(FormatAlign::Center);
let rule = format!("$W4>={}", i + 1);
let conditional = ConditionalFormatFormula::new()
.set_rule(rule.as_str())
.set_format(form);
worksheet.add_conditional_format(3, i as u16, MAX_ROWS, 22, &conditional)?;
}
*完整程序:
use rust_xlsxwriter::*;
fn main() -> Result<(), XlsxError> {
const MAX_ROWS: u32 = 5000;
//Colors
let color_array: [(u32, u32); 20] = [
(0x0000ff, 0xffff00),
(0x80ff80, 0x000000),
(0xffff00, 0x3a00ff),
(0x0000ff, 0xffffff),
(0xff0000, 0xffffff),
(0x80ffff, 0x000000),
(0xff80ff, 0x000000),
(0xffff80, 0x000000),
(0x000000, 0xffffff),
(0xc0c0c0, 0xffffff),
(0x008000, 0xffffff),
(0x0000a0, 0xffffff),
(0x804000, 0xffffff),
(0x800080, 0xffffff),
(0xff8040, 0xffffff),
(0x8080c0, 0xffffff),
(0x808040, 0xffffff),
(0x808080, 0xffffff),
(0x408080, 0xffffff),
(0x8080c0, 0xffffff),
];
const BORDER_COLOR: u32 = 0xc0c0c0;
// Create a new Excel file object.
let mut workbook = Workbook::new();
// Add a worksheet to the workbook.
let worksheet = workbook.add_worksheet();
//Set worksheet name
worksheet.set_name("WBS")?;
//Add heading data
worksheet.write(2, 20, "WBS Code")?;
worksheet.write(2, 21, "WBS Name")?;
worksheet.write(2, 22, "WBS Level")?;
//Add first level data
worksheet.write(3, 20, "Project Code")?;
worksheet.write(3, 21, "Project Name")?;
worksheet.write(3, 22, "1")?;
//first level format *Complete Later*
let first_level_format = Format::new()
.set_background_color(Color::RGB(color_array[0].0))
.set_font_color(Color::RGB(color_array[0].1))
.set_font_size(12)
.set_border_color(Color::RGB(BORDER_COLOR))
.set_border_top(FormatBorder::Thin)
.set_border_bottom(FormatBorder::Thin)
.set_bold();
worksheet.set_range_format(3, 0, 3, 22, &first_level_format)?;
//special first level format *Complete Later*
let s_first_level_format = Format::new()
.set_background_color(Color::RGB(color_array[0].0))
.set_font_color(Color::RGB(color_array[0].1))
.set_font_size(12)
.set_border_color(Color::RGB(BORDER_COLOR))
.set_border_top(FormatBorder::Thin)
.set_border_left(FormatBorder::Thin)
.set_bold();
worksheet.set_cell_format(3, 0, &s_first_level_format)?;
//Conditional format 1
for i in 0..19 {
// println!("{i}");
let form = Format::new()
.set_background_color(Color::RGB(color_array[i].0))
.set_font_color(Color::RGB(color_array[i].1))
.set_border_color(Color::RGB(BORDER_COLOR))
.set_border_top(FormatBorder::Thin)
.set_border_bottom(FormatBorder::Thin)
.set_bold()
.set_align(FormatAlign::Center);
let rule = format!("$W4>={}", i + 1);
let conditional = ConditionalFormatFormula::new()
.set_rule(rule.as_str())
.set_format(form);
worksheet.add_conditional_format(3, i as u16, MAX_ROWS, 22, &conditional)?;
}
//set columns width
worksheet.set_column_range_width(0, 19, 0.5)?;
worksheet.set_column_range_width(20, 22, 13)?;
worksheet.set_column_width(21, 50)?;
// Save the file to disk.
workbook.save("test.xlsx")?;
Ok(())
}
我尝试制作一个包含 20 个条件格式的 Excel 工作表和订单,但我无法收到此订单
深入研究问题后,我试图找出根本原因,看看是什么因素影响了这个问题
我发现它与
add_conditional_format()
方法和规则被排序为行/列顺序而不是插入顺序仅与嵌套范围
我提出了一个 GitHub 问题,该问题已在版本“0.79.1”中修复 该问题出现在版本“0.79.0”中
这是更改日志: