The command appends data to an existing file. Does the file "Powerdata.csv" exist? A file that simply contains column headings would be a place to start.
Announcement
Collapse
No announcement yet.
Can I save device values into a Excel spreadsheet ?
Collapse
X
-
The method to save/append a file in the script is old VBScript stuff and does not create the file if missing.
You can update the script as below:
Code:Public Sub Main(ByVal Parms As Object) Dim strFileName As String = "w:\HS4 Data\Powerdata.csv" Dim strFileName2 As String = "c:\HS4 Data\Powerdata.csv" Dim strData As String = "" Dim dteDate As Date = DateTime.Now Dim decTRain As Decimal = hs.DeviceValueEx(2593) Dim GridPowerKWh As Decimal = hs.DeviceValueEx(3335) Dim SolarPowerKWh As Integer = hs.DeviceValue(3334) strData = dteDate.ToShortDateString & "," & dteDate.DayOfYear & "," & decTRain.ToString & "," & GridPowerKWh.ToString & "," & SolarPowerKWh.ToString & vbCrLf My.Computer.FileSystem.WriteAllText(strFileName2, strData, True) End Sub
Comment
-
Originally posted by mikee123 View PostI have tried adapting your script. But I am getting a error
Running script DataValuestoExcel.vb :Exception has been thrown by the target of an invocation.
Comment
-
Originally posted by jon00 View PostThe method to save/append a file in the script is old VBScript stuff and does not create the file if missing.
You can update the script as below:
Code:Public Sub Main(ByVal Parms As Object) Dim strFileName As String = "w:\HS4 Data\Powerdata.csv" Dim strFileName2 As String = "c:\HS4 Data\Powerdata.csv" Dim strData As String Dim dteDate As Date = NOW Dim strDay As String Dim decTRain As Decimal = hs.DeviceValueEx(2593) Dim GridPowerKWh As Decimal = hs.DeviceValueEx(3335) Dim SolarPowerKWh As Integer = hs.DeviceValueEx(3334) strData = dteDate.ToShortDateString & "," & dteDate.DayOfYear & "," & decTRain.ToString & "," & GridPowerKWh.ToString & "," & SolarPowerKWh.ToString & "," My.Computer.FileSystem.WriteAllText(strFileName, strData, True) My.Computer.FileSystem.WriteAllText(strFileName2, strData, True) hs.WriteLog("Write to Ecel", "Power data exported to Excel") End Sub
Comment
-
Originally posted by mikee123 View Post
How / where would I do that ?
You should always have exception handling, especially during development. I.e.
Code:Try your code here Catch e As Exception Console.WriteLine("Exception caught: {0}", e) End Try
Comment
-
Originally posted by jon00 View PostAs I posted before, your strdata line should be:
Code:strData = dteDate.ToShortDateString & "," & dteDate.DayOfYear & "," & decTRain.ToString & "," & GridPowerKWh.ToString & "," & SolarPowerKWh.ToString & vbCrLf
Comment
-
Originally posted by alexbk66 View Post
Read about exceptions. https://www.tutorialspoint.com/vb.ne...n_handling.htm
You should always have exception handling, especially during development. I.e.
Code:Try your code here Catch e As Exception Console.WriteLine("Exception caught: {0}", e) End Try
- Likes 1
Comment
Comment