根据条件给单元格上色

Color cell according to condition

我有一个 table,当 header 中的单元格和行中的单元格成对出现在数据源的某个列表中时,我必须在其中设置背景颜色。 例如: 专栏:“澳元,美元” 行 : "BRL, CZK"

在 AUD 列的单元格中,行是 BRL 我检查数据源“AUD-BRL”中的列表中是否存在,如果存在,我需要用绿色着色

现在,我想这样做: 列和行将在列表中。 我检查了两个列表,然后在这些索引单元格中着色。

这样我就可以为整个 table 使用一个函数,而不必从每个单元格调用函数(总共有 1200 个单元格)。 怎么做到的?

转到要设置格式的单元格(也适用于行或列等元素),在“属性 编辑器”上转到“突出显示”并单击“添加...”。您将看到一个对话框,您可以在其中输入突出显示的条件以及在条件为真时要在元素上应用的样式。

Screenshot here

Fede MG的回答是正确的

如果我对您的问题的理解正确,您希望向 table 详细信息行中的 所有 个单元格添加突出显示规则。不幸的是,我认为在 BIRT 中实现这一点有点麻烦。

我假设您的 table 有例如单元格值的 COL_VALUE_1、...、COL_VALUE_9 和 headers.[=19= 列的 COL_TITLE_1、...、COL_TITLE_9 等绑定]

此外,我假设有一些在 BIRT 中使用 Javascript 的经验。

我这样做的方式是这样的:

对于每个详细单元格,我使用如下代码创建了一个 onCreate 事件脚本:

highlightDetailCell(this, row, 1);

... 其中 1 是列号。例如。这是第一列的代码,对于第二列,我将 1 替换为 2,依此类推。可以通过复制和粘贴快速完成此操作。

接下来,我在报告的 onInitialize 脚本中的一个函数中实现逻辑,如下所示:

function highlightDetailCell(item, row, colnum) {
    var colTitle = row["COL_TITLE_" + colnum];
    var colValue = row["COL_VALUE_" + colnum];
    var highlight = use_your_logic_to_decide(colTitle, colValue);
    if (highlight) {
        item.get_Style().backgroundColor = "yellow";
    }
}

这是基本思路。如果要将脚本添加到多个单元格,手动执行此操作可能需要大量工作。事实上,可以使用脚本将调用附加到 highlightDetailCell 函数(当然,这是 BIRT :-)。您应该阅读文档并修改 Design Engine API(简称 DE API)。

但请注意,编写和调试这样的脚本可能比将 one-liner 添加和编辑到 1200 个单元格的驴子工作还要多!

我曾经做的基本上是这样的(在report项的onFactory事件中):

// This code is a simplified version that modifies just the first cell,
// However it should point you into the right direction.

// Some preparation
importPackage(Packages.org.eclipse.birt.report.model.api);
var myconfig = reportContext.getReportRunnable().getReportEngine().getConfig();
var de = DataEngine.newDataEngine( myconfig, null );
var elementFactory = reportContext.getDesignHandle().getElementFactory();

// Find the item you want to modify (in my case, a "Grid Item").
// Note that for tables, the structure is probably a bit different.
// E.G. tables have header, detail and footer rows, 
// while grids just have rows.
var containerGrid = reportContext.getDesignHandle().findElement("Layout MATRIX");

// Get the first row
var row0 = containerGrid.getRows().get(0);
// Do something with the first cell (:
var cell = row0.getCells().get(0).getContent();
cell.setStringProperty("paddingTop", "1pt");
cell.setStringProperty("paddingLeft", "1pt");
cell.setStringProperty("paddingRight", "1pt");
cell.setStringProperty("paddingBottom", "1pt");
cell.setStringProperty("borderBottomColor", "#000000");
cell.setStringProperty("borderBottomStyle", "solid");
cell.setStringProperty("borderBottomWidth", "thin");
cell.setStringProperty("borderTopColor", "#000000");
cell.setStringProperty("borderTopStyle", "solid");
cell.setStringProperty("borderTopWidth", "thin");
cell.setStringProperty("borderLeftColor", "#000000");
cell.setStringProperty("borderLeftStyle", "solid");
cell.setStringProperty("borderLeftWidth", "thin");
cell.setStringProperty("borderRightColor", "#000000");
cell.setStringProperty("borderRightStyle", "solid");
cell.setStringProperty("borderRightWidth", "thin");

// When you're finished:
de.shutdown( );

如果必须处理合并的单元格,事情会更复杂。

您甚至可以向单元格添加内容(我通过这种方式动态创建了整个矩阵)。

该脚本并不完全符合您的要求(将脚本添加到每个单元格),但我将其留作练习...

把动态修改的报表设计保存下来,在设计器中打开,看看效果:

reportContext.getDesignHandle().saveAs("c:/temp/modified_report.rptdesign");

HTH