-
Notifications
You must be signed in to change notification settings - Fork 72
/
Copy pathExtractTableData.ahk
122 lines (103 loc) · 3.89 KB
/
ExtractTableData.ahk
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
ExtractTableData( FilePath, HeadingsArray, Delimiter, SaveDir )
{
static htmObj
if !IsObject( htmObj )
htmObj := ComObjCreate( "HTMLfile" )
else
htmObj.Close()
tablesArray := {}
tablesDataArray := {}
FileRead, HTML, % FilePath
htmObj.Write( HTML )
tablesCollection := htmObj.getElementsByTagName( "table" )
tablesCount := tablesCollection.length
For Each, Value in HeadingsArray
{
tableNumber := 0
HeadingName := Each
HeadingNumbers := Value.1
RowNumbers := Value.2
loop % tablesCount
{
tableObj := tablesCollection[ a_index-1 ]
if InStr( tableObj.innerText, HeadingName )
{
tableNumber++
tableBodyObj := tableObj.getElementsByTagName( "tbody" )
tableColumnHeadingObj := tableBodyObj[ 0 ].firstChild.getElementsByTagName( "th" )
tableRowObj := tableBodyObj[ 0 ].getElementsByTagName( "tr" )
tableCaption := tableBodyObj[ 0 ].previousSibling.innerText
tableColumnHeadingCount := tableColumnHeadingObj.length
tableDataRowCount := tableRowObj.length-1 ; table data rows minus the heading row
loop % tableColumnHeadingCount
{
tableColumnHeadingValue := tableColumnHeadingObj[ a_index-1 ].innerText
columnNumber := a_index-1
if ( tableColumnHeadingValue ~= "^" HeadingName )
{
loop % tableDataRowCount
{
tableDataObj := tableRowObj[ a_index ].getElementsByTagName( "td" )
tableData := tableDataObj[ columnNumber ].innerText
tablesArray[ RegExReplace( Trim( tableColumnHeadingValue ), "\W", "_" ), tableNumber, a_index ] := { tableData: tableData, tableCaption : tableCaption }
}
}
}
}
}
HeadingName := RegExReplace( HeadingName, "\W", "_" )
if !( HeadingNumbers.length() || IsObject( HeadingNumbers ) || RowNumbers.length() || IsObject( RowNumbers ) )
{
tableCaption := tablesArray[ HeadingName ][ HeadingNumbers ][ RowNumbers ].tableCaption
tableArrayValue := tablesArray[ HeadingName ][ HeadingNumbers ][ RowNumbers ].tableData
tablesDataString .= ( tableArrayValue != "" ? tableCaption " ~ " HeadingName ": " tableArrayValue Delimiter : "" )
}
else if ( HeadingNumbers.length() || IsObject( HeadingNumbers ) ) && !( RowNumbers.length() || IsObject( RowNumbers ) )
{
For i in HeadingNumbers
{
tableCaption := tablesArray[ HeadingName ][ i ][ RowNumbers ].tableCaption
tableArrayValue := tablesArray[ HeadingName ][ i ][ RowNumbers ].tableData
tablesDataString .= ( tableArrayValue != "" ? tableCaption " ~ " HeadingName ": " tableArrayValue Delimiter : "" )
}
}
else if !( HeadingNumbers.length() || IsObject( HeadingNumbers ) ) && ( RowNumbers.length() || IsObject( RowNumbers ) )
{
For i in RowNumbers
{
tableCaption := tablesArray[ HeadingName ][ HeadingNumbers ][ i ].tableCaption
tableArrayValue := tablesArray[ HeadingName ][ HeadingNumbers ][ i ].tableData
tablesDataString .= ( tableArrayValue != "" ? tableCaption " ~ " HeadingName ": " tableArrayValue Delimiter : "" )
}
}
else if ( HeadingNumbers.length() || IsObject( HeadingNumbers ) ) && ( RowNumbers.length() || IsObject( RowNumbers ) )
{
For h in HeadingNumbers
{
For r in RowNumbers
{
tableCaption := tablesArray[ HeadingName ][ h ][ r ].tableCaption
tableArrayValue := tablesArray[ HeadingName ][ h ][ r ].tableData
tablesDataString .= ( tableArrayValue != "" ? tableCaption " ~ " HeadingName ": " tableArrayValue Delimiter : "" )
}
}
}
}
SplitPath, % FilePath, FileNameExt,,, FileName
if !StrLen( tablesDataString )
{
Msgbox 0x10, Whoops!, % "No Table Data Found in: " FileNameExt
return true
}
else
{
SaveFile := SaveDir "\" FileName ".txt"
if FileExist( SaveFile )
{
FileDelete % SaveFile
}
FileAppend, % Trim( tablesDataString, Delimiter ), % SaveFile
TrayTip,, % "Table Data Written To: " FileName ".txt"
}
return tablesDataString
}