-
Notifications
You must be signed in to change notification settings - Fork 72
/
Copy pathADODB_GetXLData.ahk
100 lines (86 loc) · 4.53 KB
/
ADODB_GetXLData.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
;GXD_Base := A_ScriptDir . "\SampleData.xls"
;GXD_Sheet := "Sheet1"
;clipboard := GetXLData(GXD_Base, GXD_Sheet,, "Units", "BETWEEN", 20, "AND",,, 50)
;clipboard := GetXLData(GXD_Base, GXD_Sheet, "Region,Rep,Unit Cost,", "Units", "BETWEEN", "20", "AND",,, 60)
;clipboard := GetXLData(GXD_Base, GXD_Sheet,, "Units", "BETWEEN", 30, "AND",,, 50)
;clipboard := GetXLData(GXD_Base, GXD_Sheet,, "Units", "IN", "(50, 60, 90)")
;clipboard := GetXLData(GXD_Base, GXD_Sheet,, "Units", ">", 90)
;clipboard := GetXLData(GXD_Base, GXD_Sheet, "Region, Rep,Unit Cost,", "Units", "BETWEEN", 30, "AND",,, 60)
;clipboard := GetXLData(GXD_Base, GXD_Sheet, "Region, Rep,Unit Cost,", "Region", "=", "Quebec", "AND", "Rep", "=", "Jones")
;clipboard := GetXLData(GXD_Base, GXD_Sheet, "Region, Rep,Unit Cost,", "Units", "LIKE", "3%")
GetXLData(GXD_Base, GXD_Sheet, GXD_ColsToGet = "*", GXD_ColToVerif1 = "", GXD_Filtre1 = "", GXD_ValToComp1 = "", GXD_Link = "", GXD_ColToVerif2 = "", GXD_Filtre2 = "", GXD_ValToComp2 = "") {
adOpenStatic = 3
adLockOptimistic = 3
adCmdText = 1
adApproxPosition := 0x4000
GXD_Sheet := GXD_Sheet . "$"
If (GXD_ColsToGet != "*")
{
GXD_LColsToGet := StrLen(GXD_ColsToGet)
If SubStr(GXD_ColsToGet, StrLen(GXD_ColsToGet), 1) = ","
GXD_ColsToGet := SubStr(GXD_ColsToGet, 1, StrLen(GXD_ColsToGet) - 1)
If InStr(GXD_ColsToGet, ",")
{
If InStr(GXD_ColsToGet, ", ")
StringReplace, GXD_ColsToGet, GXD_ColsToGet, `,%A_SPACE%, `,, All
StringSplit, GXD_ColsContent, GXD_ColsToGet, `,]
Loop, %GXD_ColsContent0%
{
If InStr(GXD_ColsContent%A_Index%, A_Space)
GXD_ColsContent%A_Index% := "``" . GXD_ColsContent%A_Index% . "``"
GXD_ColsContent := GXD_ColsContent . GXD_ColsContent%A_Index% . ", "
}
}
GXD_ColsToGet := GXD_ColsContent
If SubStr(GXD_ColsToGet, StrLen(GXD_ColsToGet)-1, 2) = ", "
GXD_ColsToGet := SubStr(GXD_ColsToGet, 1, StrLen(GXD_ColsToGet) - 2)
}
Loop, 2
{
If GXD_ColToVerif%A_Index%
{
If InStr(GXD_ColToVerif%A_Index%, A_Space)
GXD_ColToVerif%A_Index% := "``" . GXD_ColToVerif%A_Index% . "``"
}
If GXD_ValToComp%A_Index%
{
If (InStr(GXD_ValToComp%A_Index%, "%") or InStr(GXD_ValToComp%A_Index%, "_") or InStr(GXD_ValToComp%A_Index%, "-") or InStr(GXD_ValToComp%A_Index%, "!") or InStr(GXD_ValToComp%A_Index%, "[") or InStr(GXD_ValToComp%A_Index%, "]"))
GXD_ValToComp%A_Index% := "'" . GXD_ValToComp%A_Index% . "'"
If GXD_ValToComp%A_Index% is alpha
GXD_ValToComp%A_Index% := "'" . GXD_ValToComp%A_Index% . "'"
}
}
If (GXD_ColToVerif1 and GXD_ValToComp2 and GXD_ColToVerif2 and GXD_Filtre2)
Request := "SELECT " . GXD_ColsToGet . " FROM [" . GXD_Sheet . "]" . " WHERE " . GXD_ColToVerif1 . " " . GXD_Filtre1 . " " . GXD_ValToComp1 . " " . GXD_Link . " " . GXD_ColToVerif2 . " " . GXD_Filtre2 . " " . GXD_ValToComp2
If (GXD_ColToVerif1 and GXD_ValToComp2 and !GXD_ColToVerif2 and !GXD_Filtre2)
Request := "SELECT " . GXD_ColsToGet . " FROM [" . GXD_Sheet . "]" . " WHERE " . GXD_ColToVerif1 . " " . GXD_Filtre1 . " " . GXD_ValToComp1 . " " . GXD_Link . " " . GXD_ValToComp2
If (GXD_ColToVerif1 and !GXD_ValToComp2 and !GXD_ColToVerif2 and !GXD_Filtre2)
Request := "SELECT " . GXD_ColsToGet . " FROM [" . GXD_Sheet . "]" . " WHERE " . GXD_ColToVerif1 . " " . GXD_Filtre1 . " " . GXD_ValToComp1
If (!GXD_ColToVerif1 and !GXD_ValToComp2 and !GXD_ColToVerif2 and !GXD_Filtre2)
Request := "SELECT " . GXD_ColsToGet . " FROM [" . GXD_Sheet . "]"
objConnection := ComObjCreate("ADODB.Connection")
objRecordSet := ComObjCreate("ADODB.Recordset")
objConnection.Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" . GXD_Base . ";Extended Properties=""Excel 8.0;HDR=Yes;"";")
objRecordSet.Open(Request, objConnection, adOpenStatic, adLockOptimistic, adCmdText)
GXD_NumRec := objRecordSet.RecordCount
GXD_pFields := objRecordSet.Fields
GXD_NumFiels := GXD_pFields.Count
Loop %GXD_NumRec%
{
Loop %GXD_NumFiels%
{
If A_Index < %GXD_NumFiels%
GXD_Records := GXD_Records . GXD_pFields.Item(A_Index-1).Value . ";"
Else
GXD_Records := GXD_Records . GXD_pFields.Item(A_Index-1).Value
}
If A_Index < %GXD_NumRec%
GXD_Records := GXD_Records . "|"
objRecordSet.MoveNext
}
objRecordSet.Close()
objConnection.Close()
objRecordSet := ""
objConnection := ""
Return GXD_Records
}