根据条件的颜色单元 [英] Color cell according to condition

查看:94
本文介绍了根据条件的颜色单元的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表,当标题中的单元格和行中的单元格在数据源的某个列表中成对出现时,我必须设置背景色.例如:列:"AUD,USD"行:"BRL,CZK"

I have a table in which I have to set background color when the cell in header and cell in row appear as pair in a certain list in data source. For example: column : "AUD, USD" row : "BRL, CZK"

是BRL,我检查是否存在于数据源"AUD-BRL"的列表中.如果需要的话,我需要涂成绿色

in the cell of column AUD and row is BRL I check if exists in the list in datasource "AUD-BRL" and if so I need to color in a green

现在,我想这样做:列和行将在列表中.我浏览了两个列表,然后在这些索引中为单元格上色.

Now, I thought to do it in this way: columns and rows will be in lists. I go over both lists and then color in those indexes the cell.

这样我就可以为整个表使用一个函数,而不必从每个单元格调用函数(总共有1200个单元格).该怎么办?

So that I will have one function for whole table and not have to call from each cell to function (There are 1200 cells overall). How can that be done?

推荐答案

Fede MG的答案是正确的.

The answer from Fede MG is correct.

如果我正确理解了您的问题,则要向表详细信息行中的所有单元格添加突出显示规则.不幸的是,我认为在BIRT中实现此目标有点麻烦.

If I understand your question correctly, you want to add a highlighting rule to all cells in the table detail row. Unfortunately I think it is a bit cumbersome to achieve this in BIRT.

我假设您的桌子有像COL_VALUE_1,...,COL_VALUE_9这样的绑定用于单元格值,而对COL_TITLE_1,...,COL_TITLE_9这样的绑定用于列标题.

I assume that your table has e.g. bindings like COL_VALUE_1, ..., COL_VALUE_9 for the cell values and COL_TITLE_1, ..., COL_TITLE_9 for the column headers.

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

Furthermore I assume a bit of experience with using Javascript in BIRT.

我这样做的方式:

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

For each detail cell I create a onCreate event script with code like this:

highlightDetailCell(this, row, 1);

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

... where 1 is the column number. E.g. this is the code for the first column, for the second column i replace the 1 with 2 and so on. One can quickly do this with copy&paste.

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

Next I implement the logic in a function inside the onInitialize script of the report like this:

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的简称).

This is the basic idea. If you want to add the script to many cells, it might be a lot of work to do this by hand. In fact it is possible to attach the call to the highlightDetailCell function with a script (of course, this is BIRT :-). You should read the documentation and just tinker with the Design Engine API (DE API for short).

但是请注意,编写和调试这样的脚本可能比完成为1200个单元格添加和编辑单行代码的驴工作还要复杂得多!

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

What I once did was basically this (in the onFactory event of the report item):

// 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( );

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

Things are more complicated if you have to handle merged cells.

您甚至可以向单元格中添加内容(我以这种方式动态创建了一个完整的矩阵).

You could even add content to the cell (I created a whole matrix dynamically this way).

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

The script does not exactly what you want (add the script to each cell), but I leave this as an exercise...

将动态修改的报表设计保存在设计器中以查看结果也很有帮助:

It is also helpful to save the dynamically modified report design for opening in the designer, to see the outcome:

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

HTH

这篇关于根据条件的颜色单元的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆