Я создал парсер, который округляет числа в тексте, используя код Visual Basi c. Но округление чисел работает только в SSRS и не работает при загрузке ( см. Среднее значение xxx )
Это мой код VB:
Public Function RoundAllNumbers(inputText As String) As String
Dim result = inputText
Dim patterns = {New PatternType With {.Pattern = "Current Usage (-?\d+\.\d+) is negative", .Template = "Current Usage {0} is negative", .Precision = 2},
New PatternType With {.Pattern = "Current Usage, (-?\d+\.\d+) KWH", .Template = "Current Usage, {0} KWH", .Precision = 2},
New PatternType With {.Pattern = "Budget of (-?\d+\.\d+) KWH", .Template = "Budget of {0} KWH", .Precision = 2},
New PatternType With {.Pattern = "usage of (-?\d+\.\d+)", .Template = "usage of {0}", .Precision = 2},
New PatternType With {.Pattern = "average of (-?\d+\.\d+)", .Template = "average of {0}", .Precision = 0},
New PatternType With {.Pattern = "Current demand (-?\d+\.\d+)", .Template = "Current demand {0}", .Precision = 0},
New PatternType With {.Pattern = "demand of (-?\d+\.\d+)", .Template = "demand of {0}", .Precision = 0},
New PatternType With {.Pattern = "usage amount of (-?\d+\.\d+)", .Template = "usage amount of {0}", .Precision = 2}}
Dim formulaPattern = New PatternType With {.Pattern = "Usage \/ \(Demand \* Service hours\) \* (-?\d+\.?\d*) = (-?\d+\.?\d*) kWh \/ \((-?\d+\.?\d*) kW \* (-?\d+) Hours\)", .Template = "Usage / (Demand * Service hours) * {0} = {1} kWh / ({2} kW * {3} Hours)"}
Dim stdDeviation = New PatternType With {.Pattern = "standard deviation(\s-?\d\s\*)?\s(-?\d+\.\d+)", .Template = "standard deviation {0}{1}", .Precision = 0}
For Each pattern As PatternType In patterns
result = ProcessPattern(result, pattern)
Next
result = ProcessFormulaPattern(result, formulaPattern)
Return ProcessstdDeviationPattern(result, stdDeviation)
End Function
Private Function ProcessstdDeviationPattern(inputText As String, pattern As PatternType) As String
Dim r = New System.Text.RegularExpressions.Regex(pattern.Pattern)
Dim match = r.Match(inputText)
Dim inputToRoundedDict = New System.Collections.Generic.Dictionary(Of String, String)()
While match.Success
Dim inputSnippet = match.Groups(0).ToString()
inputSnippet = System.Text.RegularExpressions.Regex.Replace(inputSnippet, "\*", "\*")
If Not inputToRoundedDict.ContainsKey(inputSnippet) Then
Dim parsedDouble = Double.Parse(match.Groups(2).ToString())
Dim roundedDouble = Math.Round(parsedDouble, pattern.Precision)
Dim roundedSnippet = String.Format(pattern.Template, {match.Groups(1).ToString(), roundedDouble})
inputToRoundedDict.Add(inputSnippet, roundedSnippet)
End If
match = match.NextMatch()
End While
Dim result As String = inputText
For Each kvp As System.Collections.Generic.KeyValuePair(Of String, String) In inputToRoundedDict
result = System.Text.RegularExpressions.Regex.Replace(result, kvp.Key, kvp.Value)
Next
Return result
End Function
Private Function ProcessFormulaPattern(inputText As String, pattern As PatternType) As String
Dim r = New System.Text.RegularExpressions.Regex(pattern.Pattern)
Dim match = r.Match(inputText)
Dim inputToRoundedDict = New System.Collections.Generic.Dictionary(Of String, String)()
While match.Success
Dim inputSnippet = match.Groups(0).ToString()
inputSnippet = System.Text.RegularExpressions.Regex.Replace(inputSnippet, "\*", "\*")
inputSnippet = System.Text.RegularExpressions.Regex.Replace(inputSnippet, "\(", "\(")
inputSnippet = System.Text.RegularExpressions.Regex.Replace(inputSnippet, "\)", "\)")
inputSnippet = System.Text.RegularExpressions.Regex.Replace(inputSnippet, "\/", "\/")
If Not inputToRoundedDict.ContainsKey(inputSnippet) Then
Dim parsedUsage = Double.Parse(match.Groups(2).ToString())
Dim parsedDemand = Double.Parse(match.Groups(3).ToString())
Dim roundedUsage = Math.Round(parsedUsage, 2)
Dim roundedDemand = Math.Round(parsedDemand, 0)
Dim roundedSnippet = String.Format(pattern.Template, {match.Groups(1).ToString(), roundedUsage, roundedDemand, match.Groups(4).ToString()})
inputToRoundedDict.Add(inputSnippet, roundedSnippet)
End If
match = match.NextMatch()
End While
Dim result As String = inputText
For Each kvp As System.Collections.Generic.KeyValuePair(Of String, String) In inputToRoundedDict
result = System.Text.RegularExpressions.Regex.Replace(result, kvp.Key, kvp.Value)
Next
Return result
End Function
Class PatternType
Public Pattern As String
Public Template As String
Public Precision As Integer
End Class
Private Function ProcessPattern(inputText As String, pattern As PatternType) As String
Dim r = New System.Text.RegularExpressions.Regex(pattern.Pattern)
Dim match = r.Match(inputText)
Dim inputToRoundedDict = New System.Collections.Generic.Dictionary(Of String, String)()
While match.Success
Dim inputSnippet = match.Groups(0).ToString()
If Not inputToRoundedDict.ContainsKey(inputSnippet) Then
Dim parsedDouble = Double.Parse(match.Groups(1).ToString())
Dim roundedDouble = Math.Round(parsedDouble, pattern.Precision)
Dim roundedSnippet = String.Format(pattern.Template, roundedDouble)
inputToRoundedDict.Add(inputSnippet, roundedSnippet)
End If
match = match.NextMatch()
End While
Dim result As String = inputText
For Each kvp As System.Collections.Generic.KeyValuePair(Of String, String) In inputToRoundedDict
result = System.Text.RegularExpressions.Regex.Replace(result, kvp.Key, kvp.Value)
Next
Return result
End Function
Возможно, проблема возникает из-за того, что я использую класс в коде. Хотя я не нашел каких-либо ограничений VB в SSRS для inte rnet
В чем может быть причина того, что код просыпается только при просмотре отчета в SSRS? У меня был код округления в этом отчете, и он работал. Но после моего обновления он работает только в одном месте.