1

我有一份报告作为表格。我想为每一列设置一个随机的背景色。

为此,我创建了一个自定义脚本:

Public Function GetColor()

    Dim intHighNumber AS Decimal = 255
    Dim intLowNumber AS Decimal = 100

    Dim NewColor AS String
    Dim Red AS Decimal = Int((intHighNumber - intLowNumber + 1) * Rnd + intLowNumber)
    Dim Green AS Decimal = Int((intHighNumber - intLowNumber + 1) * Rnd + intLowNumber)
    Dim Blue AS Decimal = Int((intHighNumber - intLowNumber + 1) * Rnd + intLowNumber)


    NewColor = "#" & Hex(Red) & Hex(Green) & Hex(Blue)

    Return NewColor
End Function

在第一个单元格中,我将填充表达式设置为: =code.GetColor() 直到这里它工作完美,但现在我希望列的其余部分使用相同的颜色......所以我输入了表达式“=Fields!myField .BackgroundColor”,但这不起作用......

我不知道如何解决这个问题...

非常感谢你的帮助 :-)

4

3 回答 3

0
Private string _LastColorUser="" 

Public Function LastColorUsed()
  Return _LastColorUsed
End Function

Public Function GetColor()

    Dim intHighNumber AS Decimal = 255
    Dim intLowNumber AS Decimal = 100

    Dim NewColor AS String
    Dim Red AS Decimal = Int((intHighNumber - intLowNumber + 1) * Rnd + intLowNumber)
    Dim Green AS Decimal = Int((intHighNumber - intLowNumber + 1) * Rnd + intLowNumber)
    Dim Blue AS Decimal = Int((intHighNumber - intLowNumber + 1) * Rnd + intLowNumber)


    NewColor = "#" & Hex(Red) & Hex(Green) & Hex(Blue)

    _LastColorUser=NewColor;  

    Return NewColor
End Function
于 2013-10-29T02:38:19.980 回答
0

您需要做的是记住列的颜色设置,以便您可以在页眉、页脚等中重用它。我们可以通过一个数组来实现这一点,该数组在第一次调用时返回一种新颜色,如果该索引返回相同的颜色颜色是先前指定的。

这是自定义代码:

Dim Colors(0 to 9) As String

Function GetColor(Color As Integer) As String
  Dim intHighNumber AS Decimal = 255 
  Dim intLowNumber AS Decimal = 100 
  Dim ThisColor AS String 

  if (Colors(Color) <> "") Then
    ThisColor = Colors(Color) 
  else 
    Dim Red AS Decimal = Int((intHighNumber - intLowNumber + 1) * Rnd + intLowNumber) 
    Dim Green AS Decimal = Int((intHighNumber - intLowNumber + 1) * Rnd + intLowNumber) 
    Dim Blue AS Decimal = Int((intHighNumber - intLowNumber + 1) * Rnd + intLowNumber) 
    ThisColor = "#" & Hex(Red) & Hex(Green) & Hex(Blue) 
    Colors(Color) = ThisColor
  End If 

  Return ThisColor 
End Function

然后在您的单元格中,您只需调用GetColor传递您希望单元格具有的颜色索引号的函数。例如,第一列的页眉、详细信息和页脚单元格的BackgroundColor属性表达式都是:

=Code.GetColor(0)

然后,您只需为每列使用不同的颜色索引。

确保为数组提供足够的空间来存储所有颜色(或者,ReDim如果您想使用动态数组,则每次添加新颜色时也可以使用数组)。

于 2013-10-29T03:17:30.823 回答
0

@Chris 谢谢你这是我需要的。因为我的列是动态的,所以我不得不编写另一个函数来获取一个数字:

Public Function GetColumnNumber(ByVal parameter as Parameter,ByVal SSID As String) as String
      For i as integer = 0 to parameter.Count-1
         If CStr(parameter.Value(i)) = SSID THEN
          Return i
         END IF
      Next
End Function

在单元格中,我写道:

=code.GetColor(code.GetColumnNumber(Parameters!SSID,Fields!SSID.Value))

再次感谢 :)

于 2013-10-29T10:56:54.677 回答