It is currently March 28th, 2024, 7:23 pm

Import CSV File Into 2D Array

Get help with creating, editing & fixing problems with skins
User avatar
jsmorley
Developer
Posts: 22628
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: Import CSV File Into 2D Array

Post by jsmorley »

So zoso51:

Something like this:

Skin:

Code: Select all

[Rainmeter]
Update=1000
DynamicWindowSize=1
AccurateText=1

[Variables]
NumRows=0

[Lua]
Measure=Script
ScriptFile=Test.lua
Disabled=1

[MeterCount]
Meter=String
FontSize=11
FontWeight=400
FontColor=255,255,255,255
SolidColor=47,47,47,255
Padding=5,5,5,5
AntiAlias=1
DynamicVariables=1
Text=#NumRows#

[MeterField1]
Meter=String
Y=5R
FontSize=11
FontWeight=400
FontColor=255,255,255,255
SolidColor=47,47,47,255
Padding=5,5,5,5
AntiAlias=1
DynamicVariables=1
Text=[&Lua:GetField(1,'nameField1')]
Test.lua:

Code: Select all

function Initialize()

	FilePath = SKIN:MakePathAbsolute("myCSV.csv")
	
	arrayList = {}

	for line in io.lines(FilePath) do
		numberField1, nameField1, typeField1, nameField2, nameField3, dateField1, dateField2, dateField3, dateField4, answerField1 = line:match('%s*(.-),%s*(.-),%s*(.-),%s*(.-),%s*(.-),%s*(.-),%s*(.-),%s*(.-),%s*(.-),%s*(.-)$')
		arrayList[#arrayList + 1] = { numberField1=numberField1, nameField1=nameField1, typeField1=typeField1, nameField2=nameField2, nameField3=nameField3, dateField1=dateField1, dateField2=dateField2, dateField3=dateField3, dateField4=dateField4, answerField1=answerField1 }
	end
	
	SKIN:Bang('!SetVariable', 'NumRows', #arrayList)
	
end

function GetField(indexArg, fieldArg)

	return arrayList[indexArg][fieldArg]

end
myCSV.csv:

Code: Select all

01234,BobsBobShop,Service,Mike,Tim,2019/04/20,2019/05/20,N/A,2019/06/10,YES
05778,MikesMikeShop,Design&Build,John,Paul,2019/04/20,2019/05/20,,2019/06/10,YES
02487,BillsBillShop,Build,Tom,Tim,2019/03/10,N/A,2019/04/08,2019/06/10,NO
02147,TomsTomShop,Design,Eric,Tim,2019/04/20,2019/05/20,2019/06/10,N/A,NO
1.jpg
You do not have the required permissions to view the files attached to this post.
User avatar
jsmorley
Developer
Posts: 22628
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: Import CSV File Into 2D Array

Post by jsmorley »

dvo:

See the code in my post directly above this one...
zoso51
Posts: 8
Joined: October 15th, 2019, 2:28 pm

Re: Import CSV File Into 2D Array

Post by zoso51 »

Thanks!
This is an example of one of my meters:

Code: Select all

[meterLabelJob1DesignReview]
Meter=String
DynamicVariables=1
MeterStyle=styleLeftText
X=120
Y=60
W=190
H=14
Text=[&Lua:GetField(1,'DesignReviewApproved')]
It seems to be working fine. I'm adding a single meter for each data point and generating a table of information. See here:
Image

I guess I'm a little confused on syntax in Rainmeter...

So lets say I wanted to do something simple like:

Code: Select all

ProjNum=04001
if ProjNum>4000 Then
ProjNum=ProjNum+10
For rainmeter the syntax would be...?

Code: Select all

ProjNum >4000 ? ProjNum+10 : ProjNum 
But do I have to put that into a measure meter? I can't just do that in the variable section or inside a meter string?


Another questions...
What I'm also trying to do is take a number and create a file location from it to create a hot link

Code: Select all

[meterLabelJob1Num]
Meter=String
DynamicVariables=1
MeterStyle=styleLeftText
X=80
Y=60
W=190
H=14
Text=[&Lua:GetField(1,'ProjectNumber')]
InlineSetting=None
InlinePattern=.*
MouseOverAction=[!SetOption meterLabelJob1 InlineSetting "Underline"][!UpdateMeter *][!Redraw]
MouseLeaveAction=[!SetOption meterLabelJob1 InlineSetting "None"][!UpdateMeter *][!Redraw]
LeftMouseUpAction=["C:\AEPDM\Projects\08000-08999\08127\"]
However this is hard coded, so what I want to do would be use some logic statements generate the right link, for example:
If the project number was 08127
C:\AEPDM\Projects\08000-08999\08127\

if the number was 10542
C:\AEPDM\Projects\10000-10999\10542\

I was going to use some if statements but after typing this out I'm thinking it might be easier to concatenate and just pull the first two digits from the number and insert them and then the full number at the end...

C:\AEPDM\Projects\##000-##999\#####\

Maybe this is something that I should do in vba and export it to the csv file already as a full link and then just read that variable in.
User avatar
jsmorley
Developer
Posts: 22628
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: Import CSV File Into 2D Array

Post by jsmorley »

zoso51 wrote: October 21st, 2019, 8:45 pm I guess I'm a little confused on syntax in Rainmeter...

So lets say I wanted to do something simple like:

Code: Select all

ProjNum=04001
if ProjNum>4000 Then
ProjNum=ProjNum+10
For rainmeter the syntax would be...?

Code: Select all

ProjNum >4000 ? ProjNum+10 : ProjNum 
But do I have to put that into a measure meter? I can't just do that in the variable section or inside a meter string?

It depends a bit on what you intend to do with that value. Can you elaborate just a bit?
zoso51
Posts: 8
Joined: October 15th, 2019, 2:28 pm

Re: Import CSV File Into 2D Array

Post by zoso51 »

jsmorley wrote: October 21st, 2019, 11:03 pm It depends a bit on what you intend to do with that value. Can you elaborate just a bit?
So that was just a very generic example to try to understand the if statements more. What I'm actually trying to do is I'm going to get the number of rows from my array

Code: Select all

function Getrows()

	return(#arrayList)

end
and set it to a variable

Code: Select all

NumberOfJobs=&Lua:Getrows()
Then in my meters section I'm going to hardcode meters for lets say 10 projects maximum and I would like to use if statements to show or not show the meters. Below isn't the right structure for the if statements for Rainmeter but it's what I would like to do and is how I would generally write it in other languages. Is there some way to toggle meters on or off in this way based on a logical statement? If I just change the visibility of the meter will the background still grow as if there is a meter there? If I only have two rows I wouldn't wan the background to grow as if there were 10 rows.

Code: Select all

If NumberOfJobs >0 Then

[meterLabelJob1Designer]
Meter=String
DynamicVariables=1
MeterStyle=styleLeftText
X=10
Y=60
W=190
H=14
Text=[&Lua:GetField(1,'Lead')]


[meterLabelJob1Num]
Meter=String
DynamicVariables=1
MeterStyle=styleLeftText
X=80
Y=60
W=190
H=14
Text=[&Lua:GetField(1,'ProjectNumber')]
InlineSetting=None
InlinePattern=.*
MouseOverAction=[!SetOption meterLabelJob1 InlineSetting "Underline"][!UpdateMeter *][!Redraw]
MouseLeaveAction=[!SetOption meterLabelJob1 InlineSetting "None"][!UpdateMeter *][!Redraw]
LeftMouseUpAction=["C:\AEPDM\Projects\08000-08999\08127\"]


[meterLabelJob1DesignReview]
Meter=String
DynamicVariables=1
MeterStyle=styleLeftText
X=120
Y=60
W=190
H=14
Text=[&Lua:GetField(1,'DesignReviewApproved')]
Endif

If NumberOfJobs >1 Then

[meterLabelJob2Designer]
Meter=String
DynamicVariables=1
MeterStyle=styleLeftText
X=10
Y=60
W=190
H=14
Text=[&Lua:GetField(2,'Lead')]


[meterLabelJob2Num]
Meter=String
DynamicVariables=1
MeterStyle=styleLeftText
X=80
Y=60
W=190
H=14
Text=[&Lua:GetField(2,'ProjectNumber')]
InlineSetting=None
InlinePattern=.*
MouseOverAction=[!SetOption meterLabelJob1 InlineSetting "Underline"][!UpdateMeter *][!Redraw]
MouseLeaveAction=[!SetOption meterLabelJob1 InlineSetting "None"][!UpdateMeter *][!Redraw]
LeftMouseUpAction=["C:\AEPDM\Projects\08000-08999\08127\"]


[meterLabelJob2DesignReview]
Meter=String
DynamicVariables=1
MeterStyle=styleLeftText
X=120
Y=60
W=190
H=14
Text=[&Lua:GetField(2,'DesignReviewApproved')]

Endif

Does that make sense?
zoso51
Posts: 8
Joined: October 15th, 2019, 2:28 pm

Re: Import CSV File Into 2D Array

Post by zoso51 »

dvo wrote: October 21st, 2019, 9:17 pm first add them in your csv then add the fields and line match and add them in your return arrayList to call them ... :welcome: the rest will be a formula with a calc
the link wil be something like this: LeftMouseDownAction="C:\AEPDM\Projects\[&Lua:GetField(1,'ProjectNumber')]" [!Redraw]
but if you want it exact you have to wait for JSMorley :welcome:
Yeah that seems to be the easiest way, I have to change my vba code to export this but once I do I think I should be able to get this work. I'll probably just export the full string and add a value to my array so it is something like LeftMouseDownAction="[&Lua:GetField(1,'ProjectLink)]" [!Redraw]

Thanks!
User avatar
jsmorley
Developer
Posts: 22628
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: Import CSV File Into 2D Array

Post by jsmorley »

Does that make sense?
I think so. I would look at something like this:

Skin:

Code: Select all

[Rainmeter]
Update=1000
DynamicWindowSize=1
AccurateText=1

[Variables]
NumRows=0

[Lua]
Measure=Script
ScriptFile=Test.lua
Disabled=1

[MeterBackground]
Meter=Shape
Shape=Rectangle 1,1,200,([&MeterField[#NumRows]:Y] + [&MeterField[#NumRows]:H] + 10) | StrokeWidth 1 | StrokeColor 255,255,255,255 | Fill Color 47,47,47,255
DynamicVariables=1

[MeterField1]
Meter=String
Group=Group1
X=10
Y=10
FontSize=11
FontWeight=400
FontColor=255,255,255,255
SolidColor=47,47,47,255
AntiAlias=1
DynamicVariables=1
Hidden=1
Text=[&Lua:GetField(1,'nameField1')]

[MeterField2]
Meter=String
Group=Group2
X=0r
Y=5R
FontSize=11
FontWeight=400
FontColor=255,255,255,255
SolidColor=47,47,47,255
AntiAlias=1
DynamicVariables=1
Hidden=1
Text=[&Lua:GetField(2,'nameField1')]

[MeterField3]
Meter=String
Group=Group3
X=0r
Y=5R
FontSize=11
FontWeight=400
FontColor=255,255,255,255
SolidColor=47,47,47,255
AntiAlias=1
DynamicVariables=1
Hidden=1
Text=[&Lua:GetField(3,'nameField1')]

[MeterField4]
Meter=String
Group=Group4
X=0r
Y=5R
FontSize=11
FontWeight=400
FontColor=255,255,255,255
SolidColor=47,47,47,255
AntiAlias=1
DynamicVariables=1
Hidden=1
Text=[&Lua:GetField(4,'nameField1')]

[MeterField5]
Meter=String
Group=Group5
X=0r
Y=5R
FontSize=11
FontWeight=400
FontColor=255,255,255,255
SolidColor=47,47,47,255
AntiAlias=1
DynamicVariables=1
Hidden=1
Text=[&Lua:GetField(5,'nameField1')]
Test.lua:

Code: Select all

function Initialize()

	FilePath = SKIN:MakePathAbsolute("myCSV.csv")
	
	arrayList = {}

	for line in io.lines(FilePath) do
		numberField1, nameField1, typeField1, nameField2, nameField3, dateField1, dateField2, dateField3, dateField4, answerField1 = line:match('%s*(.-),%s*(.-),%s*(.-),%s*(.-),%s*(.-),%s*(.-),%s*(.-),%s*(.-),%s*(.-),%s*(.-)$')
		arrayList[#arrayList + 1] = { numberField1=numberField1, nameField1=nameField1, typeField1=typeField1, nameField2=nameField2, nameField3=nameField3, dateField1=dateField1, dateField2=dateField2, dateField3=dateField3, dateField4=dateField4, answerField1=answerField1 }
	end
	
	SKIN:Bang('!SetVariable', 'NumRows', #arrayList)
	
end

function GetField(indexArg, fieldArg)

	if indexArg <= #arrayList then
	
		SKIN:Bang('!ShowMeterGroup', 'Group'..indexArg)
		return arrayList[indexArg][fieldArg]
	
	else
	
		SKIN:Bang('!HideMeterGroup', 'Group'..indexArg)
		return ''
		
	end

end
myCSV.csv:

Code: Select all

01234,BobsBobShop,Service,Mike,Tim,2019/04/20,2019/05/20,N/A,2019/06/10,YES
05778,MikesMikeShop,Design&Build,John,Paul,2019/04/20,2019/05/20,,2019/06/10,YES
02487,BillsBillShop,Build,Tom,Tim,2019/03/10,N/A,2019/04/08,2019/06/10,NO
02147,TomsTomShop,Design,Eric,Tim,2019/04/20,2019/05/20,2019/06/10,N/A,NO

1.jpg


So the approach is to really not focus on how many rows there actually are in the array, but rather to just "hide" all the meters displaying them, and only if they exist in the array, then "show" them. To do this, we set them all with Hidden=1 to start with, and in the Lua script we either !ShowMeterGroup the group of meters associated with that single row, or !HideMeterGroup them. The "background" will use the position and height of the last visible meter in a (formula), along with 10 pixels of extra cosmetic space at the end, in a dynamic way to determine how big it should be.

So in this example, I am asking for 5 rows from the array, but there are currently only 4.

Note that I am simplifying things quite a bit by having consistent names for Groups and Meters so I can just append the indexArg number to them in the Lua to get a match.

The way I am building the height value in the Shape meter may not be entirely intuitive, as it does some pretty arcane "nesting" of section variables and regular variables. Check this out:

https://docs.rainmeter.net/manual/variables/section-variables/#MeterParameters
https://docs.rainmeter.net/manual/variables/nesting-variables/

As an aside, if the number of rows in the array can ever be "more" than the number of meters you have designed to show them, then you will want to check against some maxRows variable in the Lua, and set the NumRows variable accordingly.
You do not have the required permissions to view the files attached to this post.
zoso51
Posts: 8
Joined: October 15th, 2019, 2:28 pm

Re: Import CSV File Into 2D Array

Post by zoso51 »

That is a really elegant solution! After a bunch of manual reading I think I understand it and see how everything works, might need to spend a little bit more time on that nesting stuff. Every meter on the row will be the same group and I just show and hide each group, number of rows determines my final meter and background size...I'll add in some logic in the lua script that if the number of rows is greater than my number of hard coded meters to show a final meter that just shows an error saying something like "not all projects showing" Or something like that.

I think you have given me the tools to get a working prototype of this meter, I'll have to dig into it over the next few days and I'll hopefully I'll be posting the results soon!

Thanks so much for the help.
User avatar
jsmorley
Developer
Posts: 22628
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: Import CSV File Into 2D Array

Post by jsmorley »

zoso51 wrote: October 22nd, 2019, 4:39 pm That is a really elegant solution! After a bunch of manual reading I think I understand it and see how everything works, might need to spend a little bit more time on that nesting stuff. Every meter on the row will be the same group and I just show and hide each group, number of rows determines my final meter and background size...I'll add in some logic in the lua script that if the number of rows is greater than my number of hard coded meters to show a final meter that just shows an error saying something like "not all projects showing" Or something like that.

I think you have given me the tools to get a working prototype of this meter, I'll have to dig into it over the next few days and I'll hopefully I'll be posting the results soon!

Thanks so much for the help.
Glad to help!