Jump to content

Text to Excel


Hawkwing
 Share

Recommended Posts

Not perfect, but I think it works.

#include<GuiConstants.au3>
#include<ButtonConstants.au3>
#include<Excel.au3>

Local $radiofile, $radiotext, $tof, $text, $path, $delimiter, $pathinput, $textedit, $delimiterinput, $outputpath, $outputpathinput, $excelfile

Dim $array[1][1]

Opt("guioneventmode", 1)

$step = 0
continue()

While 1
    Sleep(100)
WEnd

Func continue()
    $step += 1
    If $step = 1 Then
        guikill()
        $guimain = GUICreate("Text to Excel", 194, 53, -1, -1)
        $radiofile = GUICtrlCreateRadio("Text File (must have .txt extension)", 5, 5, 181, 21)
        GUICtrlSetState(-1, $GUI_CHECKED)
        $radiotext = GUICtrlCreateRadio("Text", 5, 25, 86, 21)
        $next = GUICtrlCreateButton("Continue", 120, 25, 66, 21)
        GUICtrlSetOnEvent($next, "continue")
        GUISetOnEvent($GUI_EVENT_CLOSE, "end")
        GUISetState(@SW_SHOW)
    EndIf
    If $step = 2 Then
        If GUICtrlRead($radiofile) = $GUI_CHECKED Then
            guikill()
            $tof = "file"
            $guimain = GUICreate("Text to Excel", 311, 56, -1, -1)
            GUISetOnEvent($GUI_EVENT_CLOSE, "end", $guimain)
            $pathinput = GUICtrlCreateInput("", 5, 30, 301, 21)
            $pathlabel = GUICtrlCreateLabel("Path of Text File", 5, 5, 81, 17)
            $back = GUICtrlCreateButton("Back", 125, 4, 86, 21)
            GUICtrlSetOnEvent($back, "back")
            $next = GUICtrlCreateButton("Continue", 214, 4, 86, 21)
            GUICtrlSetOnEvent($next, "continue")
            GUISetState(@SW_SHOW)
        ElseIf GUICtrlRead($radiotext) = $GUI_CHECKED Then
            guikill()
            $tof = "text"
            $guimain = GUICreate("Text to Excel", 412, 302, -1, -1)
            $textedit = GUICtrlCreateEdit("", 0, 0, 411, 271)
            $next = GUICtrlCreateButton("Continue", 320, 275, 86, 21)
            GUICtrlSetOnEvent($next, "continue")
            $back = GUICtrlCreateButton("Back", 5, 275, 86, 21)
            GUICtrlSetOnEvent($back, "back")
            GUISetOnEvent($GUI_EVENT_CLOSE, "end")
            GUISetState(@SW_SHOW)
        EndIf
    EndIf
    If $step = 3 Then
        If $tof = "text" Then $text = GUICtrlRead($textedit)
        If $tof = "file" Then $text = FileRead(GUICtrlRead($pathinput))
        guikill()
        $guimain = GUICreate("Text to Excel", 286, 76, -1, -1)
        $delimiterlabel = GUICtrlCreateLabel("Delimiter (what to split the text by)", 4, 5, 273, 17)
        $delimiterinput = GUICtrlCreateInput("", 5, 25, 276, 21)
        $back = GUICtrlCreateButton("Back", 4, 50, 86, 21)
        GUICtrlSetOnEvent($back, "back")
        $next = GUICtrlCreateButton("Continue", 194, 50, 86, 21)
        GUICtrlSetOnEvent($next, "continue")
        GUISetOnEvent($GUI_EVENT_CLOSE, "end")
        GUISetState(@SW_SHOW)
    EndIf
    If $step = 4 Then
        $delimiter = GUICtrlRead($delimiterinput)
        guikill()
        $guimain = GUICreate("Text to Excel", 311, 76, -1, -1)
        $back = GUICtrlCreateButton("Back", 5, 50, 86, 21)
        GUICtrlSetOnEvent($back, "back")
        $next = GUICtrlCreateButton("Continue", 220, 50, 86, 21)
        GUICtrlSetOnEvent($next, "continue")
        $outputpathlabel = GUICtrlCreateLabel("Output file path", 5, 5, 76, 17)
        $outputpathinput = GUICtrlCreateInput("", 5, 25, 301, 21)
        GUISetOnEvent($GUI_EVENT_CLOSE, "end")
        GUISetState(@SW_SHOW)
    EndIf
    If $step = 5 Then
        $outputpath = GUICtrlRead($outputpathinput)
        guikill()
        texttoarray()
        $guimain = GUICreate("Text to Excel", 176, 76, -1, -1)
        $donelabel = GUICtrlCreateLabel("Done!", 53, 5, 68, 33)
        GUICtrlSetFont(-1, 18, 400, 0, "MS Sans Serif")
        $openexcel = GUICtrlCreateButton("Open Excel File", 5, 45, 81, 26)
        GUICtrlSetOnEvent($openexcel, "openexcelclose")
        $quit = GUICtrlCreateButton("Quit", 90, 45, 81, 26)
        GUICtrlSetOnEvent($quit, "quit")
        GUISetOnEvent($GUI_EVENT_CLOSE, "end")
        GUISetState(@SW_SHOW)
    EndIf
EndFunc

Func texttoarray()
    $tarray = StringSplit($text, @CRLF, 1)
    For $i = 1 To $tarray[0]
        $avTmp = StringSplit($tarray[$i], ", ", 1)
    
        $array[0][0] += 1
        If UBound($avTmp)-1 > UBound($array) Then
            ReDim $array[$array[0][0]+1][UBound($avTmp)]
        Else
            ReDim $array[$array[0][0]+1][UBound($array, 2)]
        EndIf
        For $j = 0 To UBound($avTmp)-1
            $array[$array[0][0]][$j] = $avTmp[$j]
        Next
    Next
    arraytoexcel()
EndFunc

Func arraytoexcel()
    $excelfile = _excelbooknew(0)
    _excelwritesheetfromarray($excelfile, $array, 1, 1)
EndFunc

Func openexcelclose()
    _excelbooksaveas($excelfile, $outputpath, "xls")
    _excelbookclose($excelfile, 0, 0)
    _excelbookopen($outputpath & ".xls")
    Exit
EndFunc

Func quit()
    _excelbooksaveas($excelfile, $outputpath, "xls")
    Exit
EndFunc

Func back()
    $step -= 2
    continue()
EndFunc

Func guikill()
    GUIDelete()
EndFunc

Func end()
    Exit
EndFunc

The Wheel of Time turns, and Ages come and pass, leaving memories that become legend. Legend fades to myth, and even myth is long forgotten when the Age that gave it birth comes again.

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...