Zedna Posted December 10, 2010 Posted December 10, 2010 (edited) There is _ArrayToString() in standard Array.au3 include file but it's only for one dimensional arrays.In one of my projects I needed it for two dimensional arrays obtained from Internet Explorer (by IE UDF)so I have created _ArrayToString2D() and derived _ArrayToHtml2D()Func _ArrayToString2D(Const ByRef $avArray, $sDelimCol = "|", $sDelimRow = @CRLF, $iStart = 0, $iEnd = 0) Func _ArrayToHtml2D(Const ByRef $avArray, $attrib = 'border="1"', $iStart = 0, $iEnd = 0) Here it is also with simple example:expandcollapse popup#Include <IE.au3> ;~ #Include <Array.au3> $html_src = _ '<html>' & _ '<table border="1">' & _ '<tr>' & _ '<td>row 1 col 1</td>' & _ '<td>row 1 col 2</td>' & _ '</tr>' & _ '<tr>' & _ '<td>row 2 col 1</td>' & _ '<td>row 2 col 2</td>' & _ '</tr>' & _ '<tr>' & _ '<td>row 3 col 1</td>' & _ '<td>row 3 col 2</td>' & _ '</tr>' & _ '</table>' & _ '</html>' $oIE = _IECreate("about:blank") _IEBodyWriteHTML($oIE, $html_src) $oTable = _IETableGetCollection ($oIE, 0) $aTableData = _IETableWriteToArray($oTable, True) ;~ _ArrayDisplay($aTableData, 'Table') $csv = _ArrayToString2D($aTableData, ';', @CRLF) FileSaveAndOpen('_ArrayToString2D.csv', $csv) $html = _ArrayToHtml2D($aTableData) $html = '<html>' & @CRLF & $html & '</html>' FileSaveAndOpen('_ArrayToHtml2D.html', $html) Func FileSaveAndOpen($file, $content) $file = @TempDir & '\' & $file FileDelete($file) FileWrite($file, $content) ShellExecute($file) EndFunc ; based on _ArrayToString(), unlike _ArrayToString() this works for two dimensional array (only) Func _ArrayToString2D(Const ByRef $avArray, $sDelimCol = "|", $sDelimRow = @CRLF, $iStart = 0, $iEnd = 0) If Not IsArray($avArray) Then Return SetError(1, 0, "") If UBound($avArray, 0) <> 2 Then Return SetError(2, 0, "") Local $sResult, $iUBound = UBound($avArray) - 1 ; Bounds checking If $iEnd < 1 Or $iEnd > $iUBound Then $iEnd = $iUBound If $iStart < 0 Then $iStart = 0 If $iStart > $iEnd Then Return SetError(3, 0, "") ; Combine For $i = $iStart To $iEnd ; rows For $j = 0 To UBound($avArray,2) - 1 ; columns $sResult &= $avArray[$i][$j] & $sDelimCol Next $sResult = StringTrimRight($sResult, StringLen($sDelimCol)) $sResult &= $sDelimRow Next Return StringTrimRight($sResult, StringLen($sDelimRow)) EndFunc ; based on _ArrayToString2D() Func _ArrayToHtml2D(Const ByRef $avArray, $attrib = 'border="1"', $iStart = 0, $iEnd = 0) If Not IsArray($avArray) Then Return SetError(1, 0, "") If UBound($avArray, 0) <> 2 Then Return SetError(2, 0, "") Local $sResult, $iUBound = UBound($avArray) - 1 Local $row, $sDelimCol = "</td>" & @CRLF, $sDelimRow = '</tr>' & @CRLF ; Bounds checking If $iEnd < 1 Or $iEnd > $iUBound Then $iEnd = $iUBound If $iStart < 0 Then $iStart = 0 If $iStart > $iEnd Then Return SetError(3, 0, "") $sResult = '<table ' & $attrib & '>' & @CRLF ; Combine For $i = $iStart To $iEnd ; rows $row = '<tr>' & @CRLF For $j = 0 To UBound($avArray,2) - 1 ; columns $row &= '<td>' & $avArray[$i][$j] & $sDelimCol Next $sResult &= $row & $sDelimRow Next Return $sResult & '</table>' & @CRLF EndFuncEDIT: removed commas from $html_src Edited December 10, 2010 by Zedna Resources UDF ResourcesEx UDF AutoIt Forum Search
czardas Posted December 10, 2010 Posted December 10, 2010 (edited) That's really cool. There has been a similar discussion in But your script is very interesting and practical. Thanks.EditI just noticed that the excel example didn't come out exactly the same as the html table. Perhaps you need to check that. Edited December 10, 2010 by czardas operator64 ArrayWorkshop
Zedna Posted December 10, 2010 Author Posted December 10, 2010 (edited) EditI just noticed that the excel example didn't come out exactly the same as the html table. Perhaps you need to check that.For me it works OK.You may try to remove "," (commas) from $html_srcso instead of "row 1, col 1" put only "row 1 col 1".--> I removed it from example in first postAlso look at CSV file in TEMP directory if it's OK, maybe it's only some display problem in Excel. Edited December 10, 2010 by Zedna Resources UDF ResourcesEx UDF AutoIt Forum Search
czardas Posted December 11, 2010 Posted December 11, 2010 I changed the separator in line 28 from a semicolon to a comma, and now it works perfectly. I guess csv files are interpreted differently depending on settings in excel. I'll have to investigate that. operator64 ArrayWorkshop
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