搬家第二天-31.Wincc V7.3 MSHFGrid 读取表格数据后增加最大值最小值平均值计算
作者:互联网
我们在利用MSHFGrid读取表格数据后,有时候希望能够在末尾增加三行,读取最大值最小值平均值数据,下面介绍如何实现着一个功能。
一 准备工作
假设SQL Server中有这样一张表格
Wincc画面上有一个MSHFGrid控件,名字叫做MSHFGrid,有一个按钮,按钮VBS脚本如下:
Sub OnClick(ByVal Item)
Dim conn
Dim ssql
Dim ors
Dim ocom
Dim scon
Dim MSHFGrid
Dim ADODC
Dim PCName
Dim i,j,recordcount
Dim max,min,avg,sum
PCName=HMIRuntime.Tags("@LocalMachineName").Read
scon="Provider = SQLOLEDB.1;Integrated Security=SSPI;Persist SecurityInfo=False;Initial Catalog =MyDB;Data Source = " &PCName & "\WINCC"
ssql="select * from charttable"
Set conn=CreateObject("ADODB.Connection")
conn.ConnectionString=scon
conn.Cursorlocation=3
conn.open
Set ors=CreateObject("ADODB.RecordSet")
Set ocom=CreateObject("ADODB.Command")
ocom.commandtype=1
Set ocom.ActiveConnection=conn
ocom.CommandText=ssql
Set ors=ocom.Execute
Set MSHFGrid=ScreenItems("MSHFGrid")
Set MSHFGrid.DataSource=ors
MSHFGrid.Refresh
MSHFGrid.colwidth(1)=2500
Set ors=Nothing
conn.close
Set conn=Nothing
'下面的代码增加最大值最小值平均值计算
recordcount=MSHFGrid.Rows
MSHFGrid.Rows=MSHFGrid.Rows + 3
MSHFGrid.TextMatrix(recordcount,1)="最小值"
MSHFGrid.TextMatrix(recordcount + 1,1)="最大值"
MSHFGrid.TextMatrix(recordcount + 2,1)="平均值"
For j=2 To MSHFGrid.cols - 1
max=CDbl(MSHFGrid.TextMatrix(1,j))
min=CDbl(MSHFGrid.TextMatrix(1,j))
sum=0.0
MSHFGrid.colwidth(j)=1900
For i=1 To recordcount - 1
sum= sum + CDbl(MSHFGrid.TextMatrix(i,j))
If max <= CDbl(MSHFGrid.TextMatrix(i,j)) Then
max = CDbl(MSHFGrid.TextMatrix(i,j))
End If
If min >= CDbl(MSHFGrid.TextMatrix(i,j)) Then
min = CDbl(MSHFGrid.TextMatrix(i,j))
End If
Next
MSHFGrid.TextMatrix(recordcount,j) = min
MSHFGrid.TextMatrix(recordcount + 1,j) = max
avg=sum/(recordcount-1)
MSHFGrid.TextMatrix(recordcount + 2,j) = avg
Next
End Sub
保存后运行,就能够看到需要的效果了。需要注意的是平均值计算可能遇到除不尽的情况,小数位数较多,可以考虑转换成整数显示使用CInt()函数。
标签:V7.3,recordcount,max,31,CDbl,Wincc,min,TextMatrix,MSHFGrid 来源: https://www.cnblogs.com/fishingsriver/p/14350359.html