Jump to content
Sign in to follow this  
boomingranny

Array to Web Browser

Recommended Posts

boomingranny

I often have my scripts output as html pages, as i like being able to use tables and color...

below is a quick idea i had no more than 20 minutes ago, turned into autoitscript :P

Basically, you run _ArrayToWebBrowser, input a array, and the output is a table, in your web browser (or whatever the default app for .htm is).

the advantage of this is you can add html to your display to add color or whatever (as below)

;
;demo code - generate an array and add some sample data into it
;
 Dim $array[5][4]
   $array[0][0] = "<td bgcolor=#AAAAFF>name</td>"
   $array[0][1] = "<td bgcolor=#AAAAFF>Favorite Color</td>"
   $array[0][2] = "<td bgcolor=#AAAAFF>Age</td>"
   $array[1][0] = "Bob"
   $array[2][0] = "Ernie"
   $array[3][0] = "Bert"
   $array[4][0] = "Troy"
   $array[1][1] = "<font color=red>red</font>"
   $array[2][1] = "<font color=green>green</font>"
   $array[3][1] = "<font color=blue>blue</font>"
   $array[4][1] = "<font color=black>black</font>"
   For $i = 1 To 4
    $array[$i][2] = "<font color=#"& Hex (Random (1,16777215,1),6)&">"&Random (1, 99,1)&"</font>"
   Next

_ArrayToWebBrowser ($array,"People",1,3,"aa11cc",2,2,"DDDDFF")
;
;end of demo code
;

;input an array, outputs a web page to your screen
Func _ArrayToWebBrowser ($array,$title = @ScriptName,$headers=2,$border=1,$boardercolor = "000000",$cellpadding=1,$cellspacing=1,$bgcolor="")
    $htmlfile = FileOpen (@ScriptDir &"\" &$title &".htm",2)
    FileWriteLine ($htmlfile,"<html><head><title>"&$title&"</title></head><body>")
    FileWrite ($htmlfile, _ArrayToHTMLTable($array,$headers,$border,$boardercolor ,$cellpadding,$cellspacing,$bgcolor))
    FileWriteLine ($htmlfile, "</body></html>")
    FileClose($htmlfile)
    ShellExecute (@ScriptDir&"\"&$title &".htm")
EndFunc

;inputs a 1d or 2d array, outputs a table in html code
;to customize the html for a specific cell, add html code into it, eg. $array[0][0]  = "<td color=#123123><b>text</b></td>"
;and the script will replace its internal <td></td> code with yours
;headers: 0 = no headers, 1 = top headers, 2 = both headers
;other variables act like their html equivalent
Func _ArrayToHTMLTable($array,$headers=2,$border=1,$boardercolor = "000000",$cellpadding=1,$cellspacing=1,$bgcolor="")
;correct input values
    if StringLeft ($boardercolor,1) <> "#" and $boardercolor <> "" then $boardercolor = "#" & $boardercolor
    if StringLeft ($bgcolor,1) <> "#" and $bgcolor <> "" then $bgcolor = "#" & $bgcolor


;0 columns in the array are always printed in bold
    $htmlcode = "<table border="&Chr(34) &$border&Chr(34)&" boardercolor="&Chr(34) &$border&Chr(34)&" cellspacing="&Chr(34) &$cellspacing&Chr(34)&" cellpadding="&Chr(34) &$cellpadding&Chr(34)&" bgcolor="&Chr(34) &$bgcolor&Chr(34)&">"&@CRLF
    If IsArray ($array) Then
        Select   
            Case UBound($array,0) = 1;1D arrays
                For $i = 0 To UBound($array,1)-1
                    $bold = ""
                        $unbold = ""
                    If $i = 0 and $headers >= 1 Then;add bold code for headers
                        $bold = "<b>"
                        $unbold = "</b>"
                    EndIf
                    $htmlcode &= "<tr><td>"&$bold&$array[$i]&$unbold&"</tr></td>" &@CRLF
                Next
            Case UBound($array,0) = 2;2D arrays
                For $iRow = 0 To UBound($array,1)-1
                    $htmlcode &= "<tr>" 
                    For $iColumn = 0 To UBound ($array,2)-1
                        $bold = ""
                        $unbold = ""
                        If ($iRow = 0 and $headers>=1) or ($iColumn = 0 and $headers=2)  Then
                            $bold = "<b>"
                            $unbold = "</b>"
                        Else
                            
                        EndIf
                        if StringLeft ($array[$iRow][$iColumn],3) = "<td" Then;if its already got td code then don't re-add
                            $htmlcode &= $array[$iRow][$iColumn]
                        Else
                            $htmlcode &= "<td>"&$bold&$array[$iRow][$iColumn]&$unbold&"</td>";add td code
                        endif
                        
                    Next
                    $htmlcode &= "</tr>"&@CRLF
                Next
            Case UBound($array,0) >= 3
                MsgBox (0, "Error", "Error, only 1d or 2d arrays are supported!")
        EndSelect
    Else
        MsgBox (0, "Error", "Error, only arrays are supported!")
    EndIf
    $htmlcode &= "</table>"&@CRLF
    Return $htmlcode
EndFunc
Edited by boomingranny

Share this post


Link to post
Share on other sites
gseller

Pretty cool! Might come in handy sometime.. Thanks!

Share this post


Link to post
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.