Over the years I’ve done a lot in Excel. Often I leverage my Excel function Library which is, at best, a work in progress. I commissioned Maestrith, author of AHK Studio, to review it. We worked through what did & didn’t work plus optimized a few things.
And now we just converted it all to v2! So now our download has the Excel Function library for both v1 and v2
I had wanted to be able to make a function where I pass Methods or Properties in COM. (this makes it much more flexible and eases updating / maintaining of code.) Unfortunately it was not as straight-forward as I’d hoped.
Thankfully maestrith, Author of AutoHotkey Studio, had already solved this problem and shared the solution with me! The code which shows how to pass Methods or Properties in COM is listed below. While in the video I demonstrate connecting to IE and perform web scraping, the solution is equally applicable to any COM object like Excel, etc.
pwb := WBGet()
Property:="OuterHTML"
Get_ID(pwb,"site-description","OuterHTML")
Get_ID(PXL,ID,Property){
ID_Var:= PXL.document.GetElementByID(ID)
MsgBox % ID_Var[Property]
}
;~ MsgBox % pwb.document.GetElementByID("site-description").OuterHTML
;~ MsgBox % pwb.document.GetElementByID("site-description").InnerHTML
return
pwb := WBGet()
MsgBox % WebScraping_Get(pwb,"class","widget_tag_cloud","0","")
MsgBox % WebScraping_Get(pwb,"id","tag_cloud-3",,"oh")
;***********Getting data from page*******************
WebScraping_Get(PXL,Method,Thing,Index="",Property="OuterHTML"){
;***********Set Methods and Properties to static so do not have to rebuild each time it is called*******************
static Methods:={id:"getElementByID",class:"getElementsByClassName",tag:"getElementsByTagName",name:"GetElementsByName"}
static Properties:={it:"InnerTEXT",ot:"OuterText",ih:"InnerHTML",oh:"OuterHTML",vl:"Value",si:"selectedIndex",ck:"checked"}
Doc:= PXL.document
if (method !="id")
return Doc[Methods[Method]](Thing)[Index][Properties[Property]] ;Arrays
else
return Doc[Methods[Method]](Thing)[Properties[Property]] ;Not an Array
}
return
Demonstration Video to pass Methods or Properties in COM
AutoHotkey Variadic Functions can be incredibly handy when you don’t know the exact number of parameters. The below example shows some simple math where you can pass any number of parameters to be added. It also demonstrates how you can use MaxIndex() to determine how many parameters were passed to the function.
Browser_Forward::Reload
Browser_Back::
;***********variadic function*******************
MsgBox % math(1,2)
MsgBox % math(1,2,3)
MsgBox % math(1,2,3,4)
math(x*){
tot:=0
for k, y in X
tot:=y + tot
loop, % x.MaxIndex() ;Loop over the # of parameters that exist
Indiv.= x[A_Index] "+"
return SubStr(Indiv,1,(StrLen(Indiv)-1)) "=" tot ;use substr to remove the last + sign
}