vb.net - SSRS Visual Basic 代码仅适用于报告服务
问题描述
我制作了一个解析器,它使用 Visual Basic 代码对文本中的数字进行舍入。但是数字四舍五入仅在 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
可能出现问题是因为我在代码中使用了类。虽然我在网上没有发现 SSRS 中的 VB 有任何限制
仅在 SSRS 上查看报告时代码才起作用的原因是什么?我之前在此报告中有四舍五入代码,它有效。但是在我更新之后,它只能在一个地方工作。
解决方案
您的 VB 代码很可能使用了服务器上未启用的权限。要完成这项工作,您必须深入研究服务器设置并准确确定涉及哪些权限。根据我的经验,这很难实现和维护。
另一种方法是将此代码转换为数据库中的存储过程。这样,当您部署报告时,它将继续工作。
推荐阅读
- vb.net - 错误 [HY000] [mysql][ODBC 3.51 driver][mysqld-5.5.5-10.4.14-mariadb]column 'nofak' 不能为空
- json - 颤振:我如何获得列表
从 JSON 响应? - nginx - nginx-prometheus-exporter 容器无法连接到 nginx
- swift - 在 UIImageView 上添加部分遮罩
- node.js - 每当我尝试在 VS Code 中运行任何与 npm 相关的命令时,都会出现路径错误:[找不到模块]
- python - 如何将深度图从 [400,400] 大小调整为 [60,60] 大小?
- android - 将 Firestore 数据检索到 Recylerview 不起作用
- flutter - Flutter 将循环变量合并到小部件中
- javascript - 从子域迁移 wordpress 后 JS 和 CSS 无法正常工作
- python - 如何使用以下 2 个表中的字段从表中搜索数据