sigil Posted March 4, 2010 Share Posted March 4, 2010 I have a 400 x 250 Excel sheet, and I'd like to read its contents into an AutoIt array (using COM of course). But making individual _ExcelReadCell calls in nested For..Next loops, which is what _ExcelReadSheetToArray() does, is really slow. I'm hoping there's a way I can just read the whole sheet as a range "R1C1:R400C250" and give that range to AutoIt as a 2D array. I'm having a little trouble finding what I need in VBA, though. Just calling excelObject.range("cellA:cellB") doesn't return an array, it returns a Range object. Is there a way I can return an array of cell values, or do I have to loop through them all to build that array in a way that will let me pass it to AutoIt? Link to comment Share on other sites More sharing options...
jchd Posted March 4, 2010 Share Posted March 4, 2010 It seems to me that _ExcelReadSheetToArray() is close to your best bet. Each value grabbed from Excel has to be instanciated in an AutoIt variant anyway and it's possible that's where cycles go. Remember that you can store anything in an AutoIt array, so variant ctor has to be called on the fly. I may be overlooking a faster way, but that's how I see things. This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe hereRegExp tutorial: enough to get startedPCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta. SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt) Link to comment Share on other sites More sharing options...
dani Posted March 4, 2010 Share Posted March 4, 2010 If you don't want to use _ExcelReadSheetToArray() you can try using the COM model of Excel yourself in one way or another. Dig through the Excel Object Model Reference, it contains everything you need. Link to comment Share on other sites More sharing options...
PsaltyDS Posted March 4, 2010 Share Posted March 4, 2010 Hmm... don't have Excel here to test with, but I'm curious if something like this might return an array variant directly from Excel: $aArray = $oExcel.Activesheet.Range("A1:B20").Resize(2,20) If IsArray($aArray) Then _ArrayDisplay($aArray, "$aArray") Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law Link to comment Share on other sites More sharing options...
sigil Posted March 4, 2010 Author Share Posted March 4, 2010 Hmm... don't have Excel here to test with, but I'm curious if something like this might return an array variant directly from Excel: $aArray = $oExcel.Activesheet.Range("A1:B20").Resize(2,20) If IsArray($aArray) Then _ArrayDisplay($aArray, "$aArray") IsArray() returned False for this code. Resize returns a Range object, not a variant. Link to comment Share on other sites More sharing options...
PsaltyDS Posted March 4, 2010 Share Posted March 4, 2010 How about value of the range? $aArray = $oExcel.Activesheet.Range("A1:B20").Value If IsArray($aArray) Then _ArrayDisplay($aArray, "$aArray") I found several VBA examples on Google where a variable is declared as a variant and set to the value(s) of a range, which should be an array. (Sorry, I would answer my own question, but I'm happily living in OOo-land now and don't have Excel on this box.) Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law Link to comment Share on other sites More sharing options...
sigil Posted March 4, 2010 Author Share Posted March 4, 2010 How about value of the range? $aArray = $oExcel.Activesheet.Range("A1:B20").Value If IsArray($aArray) Then _ArrayDisplay($aArray, "$aArray") I found several VBA examples on Google where a variable is declared as a variant and set to the value(s) of a range, which should be an array. (Sorry, I would answer my own question, but I'm happily living in OOo-land now and don't have Excel on this box.) Wow. Hours of coding have officially been saved. Many thanks. Link to comment Share on other sites More sharing options...
PsaltyDS Posted March 4, 2010 Share Posted March 4, 2010 Wow.Hours of coding have officially been saved.Many thanks.I can't tell if that's sarcastic or it worked... Saving a big "Whoot!" only for if it worked. Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law Link to comment Share on other sites More sharing options...
Tvern Posted March 4, 2010 Share Posted March 4, 2010 I can't tell if that's sarcastic or it worked... Saving a big "Whoot!" only for if it worked. You get a cookie! Can't believe how well that works. Link to comment Share on other sites More sharing options...
jchd Posted March 4, 2010 Share Posted March 4, 2010 Can you elaborate and post a small working example? If there's a way to have the standard UDF work significantly better, that would be useful. Also, perhaps a smart solution could benefit other COM applications as well, and their correcponding UDF here. This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe hereRegExp tutorial: enough to get startedPCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta. SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt) Link to comment Share on other sites More sharing options...
PsaltyDS Posted March 4, 2010 Share Posted March 4, 2010 You get a cookie! Can't believe how well that works. Whoo Hoo! When I get somewhere with Excel loaded, I'll look into using that technique to tweak _ExcelReadArray(), _ExcelWriteArray(), _ExcelReadSheetToArray() and _ExcelWriteSheetFromArray() for the performance improvement. Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law Link to comment Share on other sites More sharing options...
Tvern Posted March 4, 2010 Share Posted March 4, 2010 Can you elaborate and post a small working example? If there's a way to have the standard UDF work significantly better, that would be useful. Also, perhaps a smart solution could benefit other COM applications as well, and their correcponding UDF here. example: #Include <Excel.au3> #include <Array.au3> $oExcel = _ExcelBookOpen(@ScriptDir & "\test.xls",0) $aArray = $oExcel.Activesheet.Range("A1:B20").Value If IsArray($aArray) Then _ArrayDisplay($aArray, "$aArray") _ExcelBookClose($oExcel) Create test.xls in the same folder and put some data in it. Note that colums are returned as rows and vice versa. Infact the UDF might be slowed down by reformatting the array. Link to comment Share on other sites More sharing options...
PsaltyDS Posted March 4, 2010 Share Posted March 4, 2010 Note that colums are returned as rows and vice versa.Infact the UDF might be slowed down by reformatting the array.I believe there is a .Transpose method for that in the Excel scripting interface. More stuff to play with when I get a chance. Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law Link to comment Share on other sites More sharing options...
jchd Posted March 4, 2010 Share Posted March 4, 2010 How nice. Could be transposed to several COM stuff as well. This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe hereRegExp tutorial: enough to get startedPCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta. SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt) Link to comment Share on other sites More sharing options...
sigil Posted March 8, 2010 Author Share Posted March 8, 2010 I can't tell if that's sarcastic or it worked... Saving a big "Whoot!" only for if it worked. Sorry that wasn't clear. It worked very well. Thanks very much for helping me solve that problem. Link to comment Share on other sites More sharing options...
antonioj84 Posted October 12, 2016 Share Posted October 12, 2016 here is the proper way folks if i can save someone time #include <Excel.au3> #include <File.au3> #include <Array.au3> Local $oExcel =_Excel_Open() $datawb = _Excel_BookOpen($oExcel,@ScriptDir & "\test.xls") $datawb.worksheets("info").select $LastRow = $datawb.ActiveSheet.UsedRange.Rows.Count $mydata = _Excel_RangeRead($datawb, Default, "A1:AI" & $LastRow) If IsArray($mydata) Then _ArrayDisplay($mydata) _Excel_BookClose($datawb) Netol 1 Link to comment Share on other sites More sharing options...
water Posted October 12, 2016 Share Posted October 12, 2016 Most of the participating users have been offline for years And a lot has changed in AutoIt - especially since I have rewritten the Excel UDF Your example needs a few comment lines so that users can see that you "only" read a few columns from a spcific worksheet. AnonymousX 1 My UDFs and Tutorials: Spoiler UDFs: Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki Standard UDFs: Excel - Example Scripts - Wiki Word - Wiki Tutorials: ADO - Wiki WebDriver - Wiki Link to comment Share on other sites More sharing options...
antonioj84 Posted October 12, 2016 Share Posted October 12, 2016 hey Water, actually I can read everything, " I am not limited to a few columns"my range is from column A1 to AI Local $oExcel =_Excel_Open(false) ; instantiate and hide excel $datawb = _Excel_BookOpen($oExcel,@ScriptDir & "\s5315.xlsm", 0) ;assign data to $datawb $datawb.worksheets("info").select ; select info tab $LastRow = $datawb.ActiveSheet.UsedRange.Rows.Count ; determine last row $mydata = _Excel_RangeRead($datawb, Default, "A1:AI" & $LastRow) ; read range from A1 to AI ;If IsArray($mydata) Then _ArrayDisplay($mydata) ; display spreadsheet data in the array _Excel_BookClose($datawb,True) A1 to AI until the last row AnonymousX 1 Link to comment Share on other sites More sharing options...
water Posted October 12, 2016 Share Posted October 12, 2016 Then set the range to "Default" and the function returns all used cells. My UDFs and Tutorials: Spoiler UDFs: Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki Standard UDFs: Excel - Example Scripts - Wiki Word - Wiki Tutorials: ADO - Wiki WebDriver - Wiki Link to comment Share on other sites More sharing options...
antonioj84 Posted October 12, 2016 Share Posted October 12, 2016 (edited) "Then set the range to "Default" and the function returns all used cells." Hey Water , excellent point actually I change this $mydata = _Excel_RangeRead($datawb, Default, "A1:AI" & $LastRow) ; read range from A1 to AI to that $mydata = _Excel_RangeRead($datawb,Default,Default ) Edited October 12, 2016 by antonioj84 Netol 1 Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now