SSRS: Get backgroundcolor from Cell

前端 未结 3 1162
孤独总比滥情好
孤独总比滥情好 2021-01-23 09:51

I\'ve got a report as a table. I would like to set for each column a random backgroundcolor.

For this i created a Custom Script:

Public Function GetColor         


        
相关标签:
3条回答
  • 2021-01-23 10:18

    @Chris Thank you this is what i needed. Because my Columns were dynamic I had to write another function to get a Number:

    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
    

    In the Cell i wrote this:

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

    Thanks again :)

    0 讨论(0)
  • 2021-01-23 10:21
    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
    
    0 讨论(0)
  • 2021-01-23 10:22

    What you need to do is to remember the colour setting for the column so you can reuse it in headers, footers, etc. We can achieve this with an array which returns a new colour first time called and the same colour for that index if that colour was previously assigned.

    Here is the custom code:

    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
    

    Then in your cells, you simply call the GetColor function passing the colour index number that you want the cell to have. For instance, the first column's header, detail, and footer cells' BackgroundColor property expression would all be:

    =Code.GetColor(0)
    

    Then you just use a different colour index for each column.

    Make sure you dimension the array with enough room to store all your colours (or you could also ReDim your array each time you add a new colour if you want to use a dynamic array instead).

    0 讨论(0)
提交回复
热议问题