Having exported a list of my Teams into a CSV file (see previous post) I want to be able to read this table.
The CSV file looks like: first row is for the header. Then each following row contains information for each item in the list.
Properties of each item are ordered column-wise.
I want given a value for one property to get the value for another property.
Example:
TeamName := ReadCsv(CsvFile,"MailNickName",MailNickName,"DisplayName")
Code : Read Csv Table
This the function I have coded for this challenge:
ReadCsv(CsvFile,InProp,InVal,OutProp){
; OutProp := ReadCsv(FilePath,InProp,InVal,OutProp)
RowMatch := 0
Loop, read, %CsvFile%
{
RowCount := A_Index
Loop, parse, A_LoopReadLine, CSV
{
If (RowCount = 1) { ; first row= header
If (A_LoopField == InProp)
InCol := A_Index
If (A_LoopField == OutProp)
OutCol := A_Index
If Not(!OutCol OR !InCol)
break
continue
}
If (A_Index = InCol) {
If (A_LoopField == InVal) {
RowMatch := RowCount
If (InCol > OutCol)
return OutVal
}
}
If (A_Index = OutCol) {
OutVal := A_LoopField
If (RowMatch>0) ; not empty
return OutVal
}
}
}
} ; end of function
You can download it from my GitHub ahk/lib/ReadCsv
It makes use of the Loop, read and Loop,parse CSV AutoHotkey native functions.
It is optimized to stop reading as soon as the proper match was found, means it won't import the whole file as this alternative nice solution in the AHK forum / Tables.ahk in GitHub
Code Example: Get Team Name
This is the final function to get the Team name from a SharePoint url or a Team url or directly the Team GroupId
Teams_GetTeamName(sInput) {
; TeamName := Teams_GetTeamName(GroupId)
; TeamName := Teams_GetTeamName(SharePointUrl)
; TeamName := Teams_GetTeamName(TeamUrl)
CsvFile = %A_ScriptDir%\Teams_list.csv
If !FileExist(CsvFile) {
Teams_ExportTeams()
}
If RegExMatch(sInput,"/teams/(team_[^/]*)/[^/]*/(.*)",sMatch) {
MailNickName := sMatch1
TeamName := ReadCsv(CsvFile,"MailNickName",MailNickName,"DisplayName")
return TeamName
} Else If RegExMatch(sInput,"\?groupId=([^&]*)",sMatch)
GroupId := sMatch1
Else
GroupId := sInput
TeamName := ReadCsv(CsvFile,"GroupId",GroupId,"DisplayName")
return TeamName
} ; end of function
No comments:
Post a Comment