1

我有一個表格作爲報告。我想爲每列設置一個隨機的backgroundcolor。SSRS:從單元格獲取背景顏色

爲此,我創建了一個自定義腳本:

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() 直到在這裏完美的作品,但現在我想同樣的顏色對於列的其餘部分...所以我puted在表達「= Fields!myField.BackgroundColor」但這不會工作...

我不知道如何解決此問題...

非常感謝您的幫助:-)

+0

這是未測試...但由於報表引擎通常從左至右書寫自上而下,因此您可以在自定義代碼功能上方存儲LastColorUsed公共變量。在您的自定義代碼中創建一個名爲LastColorUsed()的函數,該函數返回最後使用顏色的變量。這樣你可以將表達式寫成隨機或最後使用的顏色。 –

回答

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)。

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 
+0

謝謝你的回答,但這不起作用,我爲幾個單元格獲得了不同的顏色。整個柱子不一樣。 – A3eXy

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)) 

再次感謝:)

相關問題