s-s-rS 字段表达式更改单元格的背景颜色

Posted

技术标签:

【中文标题】s-s-rS 字段表达式更改单元格的背景颜色【英文标题】:s-s-rS Field Expression to change the background color of the Cell 【发布时间】:2011-06-26 23:20:57 【问题描述】:

我正在尝试在报告中为单元格编写字段表达式,我必须根据单元格中的字符串值更改单元格的背景颜色。例如:如果列中包含值“已批准”,则单元格应显示绿色背景色。我尝试了以下方法:

= IIF(fields!column.value = "Approved", "Green")

= IIF(Fields!column.Value, "Approved", "Green")

两者都不起作用..我知道我在语法中遗漏了一些东西..可能我没有在语法中将绿色指代背景颜色。请帮忙!

【问题讨论】:

如果 hop 解决了您的问题,您应该接受他的回答 【参考方案1】:

试试这个:=IIF(Fields!Column.Value = "Approved", "Green", "No Color")

【讨论】:

【参考方案2】:

试试这个:=IIF(fields!column.value =Condition,"Red","Black")

【讨论】:

【参考方案3】:
=IIF(Fields!ADPAction.Value.ToString().ToUpper().Contains("FAIL"),"Red","White")

二进制测试也需要转成大写比较。

【讨论】:

您好,欢迎来到 SO,很高兴看到您从答案开始。当你像代码一样格式化代码时,代码更具可读性,只需缩进 4 个空格!【参考方案4】:

您可以使用 SWITCH() 函数来评估多个标准来为单元格着色。节点<BackgroundColor> 是单元格填充,<Color> 是字体颜色。

表达式:

=SWITCH(
    (
        Fields!Usage_Date.Value.Contains("TOTAL") 
        AND (Fields!User_Name.Value.Contains("TOTAL"))
    ), "Black"
    ,(
        Fields!Usage_Date.Value.Contains("TOTAL") 
        AND NOT(Fields!User_Name.Value.Contains("TOTAL"))
    ), "#595959"
    ,(
        NOT(Fields!Usage_Date.Value.Contains("TOTAL")) 
        AND Fields!User_Name.Value.Contains("TOTAL") 
        AND Fields!OLAP_Cube.Value.Contains("TOTAL") 
    ), "#c65911"
    ,(
        NOT(Fields!Usage_Date.Value.Contains("TOTAL")) 
        AND Fields!User_Name.Value.Contains("TOTAL") 
        AND NOT(Fields!OLAP_Cube.Value.Contains("TOTAL")) 
    ), "#ed7d31"
    ,true, "#e7e6e6"
    )

'Daily Totals... CellFill.&[Dark Orange]-[#c65911], TextBold.&[True]'Daily Totals... CellFill.&[Dark Orange]-[#c65911], TextBold.&[True]
'Daily Cube Totals... CellFill.&[Medium Orange]-[#eb6e19]
'Daily User List... CellFill.&[Light Grey]-[#e7e6e6]
'Date Totals All Users Total... CellFill.&[Black]-["black"], TextColor.&[Light Orange]-[#ed7d31]
'Date Totals Per User... CellFill.&[Dark Grey]-[#595959], TextColor.&[Yellow]-["yellow"]
'(ALL OTHER CONDITIONS)
'Daily User List... CellFill.&[Light Grey]-[#e7e6e6]

报告定义文件中的 XML 节点 (s-s-rS-2016 / VS-2015):

                <TablixRow>
                  <Height>0.2in</Height>
                  <TablixCells>
                    <TablixCell>
                      <CellContents>
                        <Textbox Name="Usage_Date1">
                          <CanGrow>true</CanGrow>
                          <KeepTogether>true</KeepTogether>
                          <Paragraphs>
                            <Paragraph>
                              <TextRuns>
                                <TextRun>
                                  <Value>=Fields!Usage_Date.Value</Value>
                                  <Style>
                                    <FontSize>8pt</FontSize>
                                    <FontWeight>=SWITCH(
    (
        NOT(Fields!Usage_Date.Value.Contains("TOTAL")) 
        AND Fields!User_Name.Value.Contains("TOTAL") 
        AND Fields!OLAP_Cube.Value.Contains("TOTAL") 
    ), "Bold"
    ,true, "Normal"
    )</FontWeight>
                                    <Color>=SWITCH(
    (
        Fields!Usage_Date.Value.Contains("TOTAL") 
        AND (Fields!User_Name.Value.Contains("TOTAL"))
    ), "#ed7d31"
    ,(
        Fields!Usage_Date.Value.Contains("TOTAL") 
        AND NOT(Fields!User_Name.Value.Contains("TOTAL"))
    ), "Yellow"
    ,(
        NOT(Fields!Usage_Date.Value.Contains("TOTAL")) 
        AND Fields!User_Name.Value.Contains("TOTAL") 
        AND Fields!OLAP_Cube.Value.Contains("TOTAL") 
    ), "Black"
    ,(
        NOT(Fields!Usage_Date.Value.Contains("TOTAL")) 
        AND Fields!User_Name.Value.Contains("TOTAL") 
        AND NOT(Fields!OLAP_Cube.Value.Contains("TOTAL")) 
    ), "Black"
    ,true, "Black"
    )

'Daily Totals... CellFill.&amp;[Dark Orange]-[#c65911], TextBold.&amp;[True]'Daily Totals... CellFill.&amp;[Dark Orange]-[#c65911], TextBold.&amp;[True]
'Daily Cube Totals... CellFill.&amp;[Medium Orange]-[#eb6e19]
'Daily User List... CellFill.&amp;[Light Grey]-[#e7e6e6]
'Date Totals All Users Total... CellFill.&amp;[Black]-["black"], TextColor.&amp;[Light Orange]-[#ed7d31]
'Date Totals Per User... CellFill.&amp;[Dark Grey]-[#595959], TextColor.&amp;[Yellow]-["yellow"]
'(ALL OTHER CONDITIONS)
'Daily User List... CellFill.&amp;[Light Grey]-[#e7e6e6]</Color>
                                  </Style>
                                </TextRun>
                              </TextRuns>
                              <Style />
                            </Paragraph>
                          </Paragraphs>
                          <rd:DefaultName>Usage_Date1</rd:DefaultName>
                          <Style>
                            <Border>
                              <Color>LightGrey</Color>
                              <Style>Solid</Style>
                            </Border>
                            <BackgroundColor>=SWITCH(
    (
        Fields!Usage_Date.Value.Contains("TOTAL") 
        AND (Fields!User_Name.Value.Contains("TOTAL"))
    ), "Black"
    ,(
        Fields!Usage_Date.Value.Contains("TOTAL") 
        AND NOT(Fields!User_Name.Value.Contains("TOTAL"))
    ), "#595959"
    ,(
        NOT(Fields!Usage_Date.Value.Contains("TOTAL")) 
        AND Fields!User_Name.Value.Contains("TOTAL") 
        AND Fields!OLAP_Cube.Value.Contains("TOTAL") 
    ), "#c65911"
    ,(
        NOT(Fields!Usage_Date.Value.Contains("TOTAL")) 
        AND Fields!User_Name.Value.Contains("TOTAL") 
        AND NOT(Fields!OLAP_Cube.Value.Contains("TOTAL")) 
    ), "#ed7d31"
    ,true, "#e7e6e6"
    )

'Daily Totals... CellFill.&amp;[Dark Orange]-[#c65911], TextBold.&amp;[True]'Daily Totals... CellFill.&amp;[Dark Orange]-[#c65911], TextBold.&amp;[True]
'Daily Cube Totals... CellFill.&amp;[Medium Orange]-[#eb6e19]
'Daily User List... CellFill.&amp;[Light Grey]-[#e7e6e6]
'Date Totals All Users Total... CellFill.&amp;[Black]-["black"], TextColor.&amp;[Light Orange]-[#ed7d31]
'Date Totals Per User... CellFill.&amp;[Dark Grey]-[#595959], TextColor.&amp;[Yellow]-["yellow"]
'(ALL OTHER CONDITIONS)
'Daily User List... CellFill.&amp;[Light Grey]-[#e7e6e6]</BackgroundColor>
                            <PaddingLeft>2pt</PaddingLeft>
                            <PaddingRight>2pt</PaddingRight>
                          </Style>
                        </Textbox>
                        <rd:Selected>true</rd:Selected>
                      </CellContents>
                    </TablixCell>

【讨论】:

【参考方案5】:

IIF(Fields!column.Value = "Approved", "Green") 的问题在于您缺少第三个参数。正确的语法是 IIF( [一些布尔表达式], [布尔表达式为真时的结果], [布尔值为假时的结果])

试试这个

=IIF(Fields!Column.Value = "Approved", "Green", "No Color")

这里是表达式示例列表Expression Examples in Reporting Services

【讨论】:

非常感谢您的回复。它解决了语法部分。但是当我预览报告时,我看到字符串值“已批准”被字符串值“绿色”替换。我希望背景颜色改变不是字符串值。 无论您使用的是报表生成器还是BIDS,当您在属性窗口中单击查看单元格的属性时,应该有一个背景颜色属性。那是你从上面输入你的表达式的地方。 啊..这解决了要求..非常感谢您的帮助..非常感谢..【参考方案6】:

利用颜色和背景色属性为您的查询编写表达式。将以下内容添加到您要满足的颜色属性的表达式选项中)

例子

=iif(fields!column.value = "Approved", "Green","<other color>")

iif需要3个值,第一个是相关的Column,第二个是处理True,第三个是处理iif语句的False

【讨论】:

以上是关于s-s-rS 字段表达式更改单元格的背景颜色的主要内容,如果未能解决你的问题,请参考以下文章

s-s-rS 单元格格式

s-s-rS 动态改变单元格背景和字体颜色

更改单元格的背景颜色

更改每个其他单元格的 UItableViewCell 背景颜色

更改tableView中偶数单元格的背景颜色

iOS UITableView:更改表格的背景颜色会覆盖单元格的背景颜色