ipmitool 发表于 2016-12-28 18:13:28

[已解决]list view 讀 txt 檔案數量不對程式產生錯誤

本帖最后由 ipmitool 于 2017-1-4 15:28 编辑

請問我的list view裡面有時候會有3個 txt 檔案, 但是我寫死4個變數, 因此偵測到3個txt檔案的時候, 產生錯誤. 不曉得這個要怎麼解決呢?



q410225 发表于 2016-12-28 19:30:29

本帖最后由 q410225 于 2016-12-28 19:47 编辑

说实话,看不懂,我运行了一下,一闪而过,出现这样一个错误

比如说这段

Func _add_report_items111()

        FileChangeDir("\")
        Local $path, $aFileList, $aNewList
        $path = "\"

        $aFileList = _FileListToArray($path, "*.txt")
_ArrayDisplay($aFileList)
        If IsArray($aFileList) Then
                ReDim $aNewList ;;;
                Local $sComboTxt = "All Member"
                GUICtrlSetData($idComboBox, "")
                For $i = 1 To UBound($aFileList) - 1 Step 1
                        $aNewList = $i
                        ;$aNewList[$i] = $aFileList[$i]
                        $aNewList[$i] = FileReadLine($aFileList[$i], 1)
                        $aNewList[$i] = FileReadLine($aFileList[$i], 2)
                        $aNewList[$i] = FileReadLine($aFileList[$i], 3)
                        $aNewList[$i] = FileReadLine($aFileList[$i], 4)
                        $aNewList[$i] = FileReadLine($aFileList[$i], 5)
                        $aNewList[$i] = FileReadLine($aFileList[$i], 6)
                        $aNewList[$i] = FileReadLine($aFileList[$i], 7)
                        $aNewList[$i] = FileReadLine($aFileList[$i], 8)

                        If Not StringInStr($sComboTxt, $aNewList[$i]) Then $sComboTxt &= "|" & $aNewList[$i]

                Next


按我的理解,是读取TXT文件到$aFileList 数组,
但你后面的FileReadLine($aFileList[$i], 1)又是什么意思

还有这个$path = "\",

chzj589 发表于 2016-12-28 20:03:00

本帖最后由 chzj589 于 2016-12-28 20:04 编辑

回复 1# ipmitool

把以下注释掉就能运行
;lseIf $aNewList2 = "Preparing" or $aNewList2 = "Testing" Then
;dlibRegister("Timer4")

ipmitool 发表于 2016-12-29 07:49:30

回复 3# chzj589


    可是這行是必要的, 因為要判斷第二欄的狀態...

ipmitool 发表于 2016-12-29 07:54:40

回复 2# q410225


    您好,
    FileReadLine($aFileList[$i], 1) 是讀取txt檔案的內容
   $path="\" 是要讀取txt檔案的位置嘍~

q410225 发表于 2016-12-29 10:43:47

回复 5# ipmitool


    能详细说一下整个程序的结构,实现的功能吗?

ipmitool 发表于 2016-12-29 11:40:06

回复 6# q410225




這是一個排程執行程式的工具, 如圖解說,
紅框框內的是會去讀取資料夾內的txt檔案,
並且讀取txt檔案的第一行內容, txt 第二行是 狀態有四種: ---, preparing, testing, done

程式每5秒鐘會去掃 ListView 中的 status 欄位,
當遇到第一個狀態為 ---, 就會去讀取左邊的欄位, 並且執行AC_PDU.exe

下一次5秒會再掃描一次Status, 如果第一個為Preparing或是Testing, 就不做任何動作,
等待Status為Done的時候, 再往下掃status, 遇到下一個 --- 的時候, 再執行左邊欄位 Warmboot.exe
直到所有狀態都為Done就停止計時器Total Time.

chzj589 发表于 2016-12-29 11:54:23

回复 7# ipmitool
对比两张图



我运行的图



这样好象没有读取配置文件testconfig.ini

q410225 发表于 2016-12-29 14:24:45

回复 7# ipmitool


你应该是考虑复杂了,

$aNewList这个数组很重要,因为他保存了TXT的数据

而后面的判断,只需要判断前一项是不是done和这一项是不是---



$j= UBound($aNewList)-1       
If $aNewList[$j]='done' Then
MsgBox(0,"","全部是done了,可以注销了")
EndIf

$aNewList='done'

For $j=1 To UBound($aNewList)-1       
        If $aNewList[$j]="---" And $aNewList[$j-1]="done" Then
               MsgBox(0,"","程序准备完成,可以运行了")       
                ExitLoop
        EndIf
Next


q410225 发表于 2016-12-29 14:40:01

本帖最后由 q410225 于 2016-12-29 15:37 编辑

应该和你心中想的有很大的出入,这还需要你自己完善了,

ipmitool 发表于 2016-12-29 14:47:39

回复 10# q410225


    大哥您改完之後我覺得更加复杂了...
    看不懂了...哈哈哈...

q410225 发表于 2016-12-29 15:01:15

回复 11# ipmitool

消息循环buttone1,我删除了一些,实际上,我想只保留
AdlibRegister('_add_report_items111', 59925 / 10)
的,但是怕和下面的函数有关联.

然后注释了_Checkitems1的注册,也就是_Checkitems1彻底没用啦,然后我在_add_report_items111函数的最下面加了我上面的判断语句,来代替_Checkitems1的作用

q410225 发表于 2016-12-29 15:45:01

本帖最后由 q410225 于 2016-12-29 19:08 编辑

回复 11# ipmitool

好了,就是这意思了, 你的意思我也差不多明白了,但实话说,这个程序的结构是不是有问题啊,总觉得不合理,





#include <ini.au3>
#include <File.au3>
#include <Date.au3>
#include <Array.au3>
#include <Excel.au3>
#include <Constants.au3>
#include <GuiListView.au3>
#include <GuiImageList.au3>
#include <GuiStatusBar.au3>
#include <FileConstants.au3>
#include <GUIConstantsEx.au3>
#include <MsgBoxConstants.au3>
#include <WindowsConstants.au3>

Global $g_hTimer0, $g_iSecs0, $g_iMins0, $g_iHour0, $g_sTime0
Global $g_hTimer1, $g_iSecs1, $g_iMins1, $g_iHour1, $g_sTime1
Global $g_hTimer2, $g_iSecs2, $g_iMins2, $g_iHour2, $g_sTime2
Global $g_hTimer3, $g_iSecs3, $g_iMins3, $g_iHour3, $g_sTime3
Global $g_hTimer4, $g_iSecs4, $g_iMins4, $g_iHour4, $g_sTime4

$Form1_1 = GUICreate("Test Schedule Management", 350, 330)
$hListView = GUICtrlCreateListView("", 5, 5, 335, 245) ;, $LVS_SHOWSELALWAYS, $LVS_EX_GRIDLINES + $LVS_EX_FULLROWSELECT + $LVS_EX_DOUBLEBUFFER


Global $sItemText ;this will store the text of the last right-clicked item.
Global $sItemTextFilename111
Global $sItemTextFilenameTester
Global $sItemTextFilename1
Global $sItemTextFilename4
Global $sItemTextFilename5
Global $sItemTextFilename7


Global $aNewList
$aNewList = 300
;===================================================================
$Button1 = GUICtrlCreateButton("View", 360, 15, 73, 33)
;$Button2 = GUICtrlCreateButton("Done", 300, 362, 73, 33)
$export = GUICtrlCreateButton("Export to Excel", 100, 562, 100, 33)
$export2 = GUICtrlCreateButton("Export to HTML", 220, 562, 100, 33)

$cInput_Filter = GUICtrlCreateInput("", 11, 620, 140, 20)
$cButton_Filter = GUICtrlCreateButton("Filter", 160, 618, 75, 23)
$cButton_All = GUICtrlCreateButton("Refresh", 360, 60, 73, 33)
GUICtrlCreateLabel('Input Test Case, Product, Serial Number or Tester to filter data', 11, 650, 300, 100)
;===================================================================
$idComboBox = GUICtrlCreateCombo("", 17, 1000, 135, 33)

;$analyze = GUICtrlCreateButton("Analyze", 940, 562, 100, 33)

Local $hGUI, $ahIcons, $StatusBar0
Local $aParts =

$StatusBar0 = _GUICtrlStatusBar_Create($Form1_1)
_GUICtrlStatusBar_SetParts($StatusBar0, $aParts)
_GUICtrlStatusBar_SetText($StatusBar0, 'Tool will auto execute!!!')
_GUICtrlStatusBar_SetText($StatusBar0, 'Foxconn@SIT1', 1)
_GUICtrlStatusBar_SetText($StatusBar0, " Version: v1.0 ", 2)

_GUICtrlListView_SetExtendedListViewStyle($hListView, BitOR($LVS_EX_FULLROWSELECT, $LVS_EX_GRIDLINES, $LVS_EX_SUBITEMIMAGES))



;;;;;;;;; Read testconfig.ini ;;;;;;;;;
;;;;;;;;; Read testconfig.ini ;;;;;;;;;
;;;;;;;;; Read testconfig.ini ;;;;;;;;;

$testerread = _IniReadSection_line(@ScriptDir & '\testconfig.ini', 'TestConfig')
$tester = $testerread

;;;;;;;;; Read testconfig.ini ;;;;;;;;;
;;;;;;;;; Read testconfig.ini ;;;;;;;;;
;;;;;;;;; Read testconfig.ini ;;;;;;;;;



GUISetState()

_GUICtrlListView_RegisterSortCallBack($hListView)

_GUICtrlListView_AddColumn($hListView, "File Name", 0) ;disappear file name
_GUICtrlListView_AddColumn($hListView, "Test Case", 140)
_GUICtrlListView_AddColumn($hListView, "Status", 80)
_GUICtrlListView_AddColumn($hListView, "Process Time", 100)


_GUICtrlListView_SetItemCount($hListView, 5000)


ControlClick("Test Schedule Management", "", "")


While 1

      GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY") ;Double click value get

      Switch GUIGetMsg()



                Case $GUI_EVENT_CLOSE
                        Exit (_GUICtrlListView_UnRegisterSortCallBack($hListView))
                        ExitLoop



                Case $hListView
                        _GUICtrlListView_SortItems($hListView, GUICtrlGetState($hListView))



                Case $Button1
                        $g_hTimer0 = TimerInit()
                                                $g_hTimer1=TimerInit()
                                                $g_hTimer2=TimerInit()
                                                $g_hTimer3=TimerInit()
                                                $g_hTimer4=TimerInit()
                        ;GUICtrlSetState($Button1,$gui_disable)
                        GUICtrlSetState($cButton_All, $gui_enable)

                        ProgressOn("Test Management Progress Meter", "Progress...", "0%")
                        Sleep(100)

                        ProgressSet(0, "0%")

                        ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 10mins time count
                        Dim $tl = GUICtrlCreateLabel('00 min, 00 sec', 10000, 10000, 150, 15)
                        GUISetState()
                        Dim $timer = TimerInit(), $Day = 0
                                               
                        AdlibRegister('Timer0', 500)
                        AdlibRegister("Timer1",500)
                        AdlibRegister("Timer2",500)
                        AdlibRegister("Timer3",500)
                        AdlibRegister("Timer4",500)
                        Local $hGUI, $ahIcons, $StatusBar1
                        Local $aParts =
                        $StatusBar1 = _GUICtrlStatusBar_Create($Form1_1)
                        _GUICtrlStatusBar_SetParts($StatusBar1, $aParts)
                        _GUICtrlStatusBar_SetText($StatusBar1, 'Tool will auto execute!!!')
                        _GUICtrlStatusBar_SetText($StatusBar1, 'Foxconn@SIT1', 1)
                        _GUICtrlStatusBar_SetText($StatusBar1, " Version: v1.0 ", 2)
                        ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 10 mins time count
         
                        Dim $OldList = "", $aNewList2 = ""
                        AdlibRegister('_add_report_items111', 5000) ;reflash every 10min is 59925
                        ProgressSet(100, "100%")
                        Sleep(500)
                        ProgressOff()



                Case $export
                        MsgBox(0, "Export", "Export list view data to Excel", 1)
                        exporttoexcel()



                Case $export2
                        FileChangeDir(@DesktopDir)
                        FileWrite("TestStatus_Export_" & @YEAR & @MON & @MDAY & @HOUR & @MIN & @SEC & ".html", _ListViewToHTMLTable($hListView))
                        MsgBox(0, "", "Dump html file to: " & @DesktopDir & "\" & "table" & @YEAR & @MON & @MDAY & @HOUR & @MIN & ".html", 1)


      EndSwitch
WEnd


Func Timer1()

      _TicksToTime(Int(TimerDiff($g_hTimer1)), $g_iHour1, $g_iMins1, $g_iSecs1)
      Local $sTime1 = $g_sTime1 ; save current time to be able to test and avoid flicker..
      $g_sTime1 = StringFormat("%02i:%02i:%02i", $g_iHour1, $g_iMins1, $g_iSecs1)
      If $sTime1 <> $g_sTime1 Then _GUICtrlListView_SetItemText($hListView, 1, $g_sTime1, 3)

EndFunc   ;==>Timer1



Func Timer2()

      _TicksToTime(Int(TimerDiff($g_hTimer2)), $g_iHour2, $g_iMins2, $g_iSecs2)
      Local $sTime2 = $g_sTime2 ; save current time to be able to test and avoid flicker..
      $g_sTime2 = StringFormat("%02i:%02i:%02i", $g_iHour2, $g_iMins2, $g_iSecs2)
      If $sTime2 <> $g_sTime2 Then _GUICtrlListView_SetItemText($hListView, 2, $g_sTime2, 3)

EndFunc   ;==>Timer2



Func Timer3()

      _TicksToTime(Int(TimerDiff($g_hTimer3)), $g_iHour3, $g_iMins3, $g_iSecs3)
      Local $sTime3 = $g_sTime3 ; save current time to be able to test and avoid flicker..
      $g_sTime3 = StringFormat("%02i:%02i:%02i", $g_iHour3, $g_iMins3, $g_iSecs3)
      If $sTime3 <> $g_sTime3 Then _GUICtrlListView_SetItemText($hListView, 3, $g_sTime3, 3)

EndFunc   ;==>Timer3



Func Timer4()

      _TicksToTime(Int(TimerDiff($g_hTimer4)), $g_iHour4, $g_iMins4, $g_iSecs4)
      Local $sTime4 = $g_sTime4 ; save current time to be able to test and avoid flicker..
      $g_sTime4 = StringFormat("%02i:%02i:%02i", $g_iHour4, $g_iMins4, $g_iSecs4)
      If $sTime4 <> $g_sTime4 Then _GUICtrlListView_SetItemText($hListView, 4, $g_sTime4, 3)

EndFunc   ;==>Timer4



Func _ListViewToHTMLTable($hList)
      Local $sHTML, $iColCount, $iRowCount
      $iColCount = _GUICtrlListView_GetColumnCount($hList)

      $sHTML = "<P></P><B>Test Management Tool Report</B>" & "<P></P>" & "<table width='1500' border='2' BGColor='#c4d6f6'>" & @LF & "<tr>" & @LF
      ;创建表头
      For $i = 0 To $iColCount - 1
                $aInfo = _GUICtrlListView_GetColumn($hList, $i)
                $sHTML = $sHTML & "<th scope='col' BGColor='#FFFFFF'>" & $aInfo & "</th>" & @LF
      Next
      ;添加数据
      $iRowCount = _GUICtrlListView_GetItemCount($hList)
      For $i = 0 To $iRowCount - 1
                $sHTML = $sHTML & "<tr>" & @LF
                For $j = 0 To $iColCount - 1
                        $aInfo = _GUICtrlListView_GetItem($hList, $i, $j)
                        $sHTML = $sHTML & "<th>" & $aInfo & "</th>" & @LF
                Next
                $sHTML = $sHTML & "</tr>" & @LF
      Next

      $sHTML = $sHTML & "</table>"
      ;MsgBox(0,"",$sHTMl)
      Return $sHTML
EndFunc   ;==>_ListViewToHTMLTable




Func _add_report_items111()

      FileChangeDir("\")
      Local $path, $aFileList, $aNewList
      $path = "\"

      $aFileList = _FileListToArray($path, "*.txt")

      If IsArray($aFileList) Then
                ReDim $aNewList ;;;
                Local $sComboTxt = "All Member"
                GUICtrlSetData($idComboBox, "")
                For $i = 1 To UBound($aFileList) - 1 Step 1
                        $aNewList = $i
                        ;$aNewList[$i] = $aFileList[$i]
                        $aNewList[$i] = FileReadLine($aFileList[$i], 1)
                        $aNewList[$i] = FileReadLine($aFileList[$i], 2)
                        $aNewList[$i] = "---"
                        $aNewList[$i] = FileReadLine($aFileList[$i], 4)
                        $aNewList[$i] = FileReadLine($aFileList[$i], 5)
                        $aNewList[$i] = FileReadLine($aFileList[$i], 6)
                        $aNewList[$i] = FileReadLine($aFileList[$i], 7)
                        $aNewList[$i] = FileReadLine($aFileList[$i], 8)

                        If Not StringInStr($sComboTxt, $aNewList[$i]) Then $sComboTxt &= "|" & $aNewList[$i]

                Next

                _GUICtrlListView_DeleteAllItems(GUICtrlGetHandle($hListView))
                _GUICtrlListView_AddArray($hListView, $aNewList)

                For $j = 0 To UBound($aNewList) - 1 Step 1
                        _GUICtrlListView_SetItemText($hListView, $j + 1, " ", 7)
                Next

;~                                                                     $aNewList=$aNewList2
                GUICtrlSetData($idComboBox, $sComboTxt, "All Member")
      EndIf
      $j = UBound($aNewList) - 1
      If $aNewList[$j] = 'done' Then
                MsgBox(0, "", "全部是done了,可以注销了")
                AdlibUnRegister('_add_report_items111')
      EndIf
      $aNewList = 'done'
      For $j = 1 To UBound($aNewList) - 1
                If $aNewList[$j] = "done" Then
                        AdlibUnRegister("Timer" & $j)
                        _GUICtrlListView_SetItemText($hListView, $j, "完成", 3)
                EndIf
                If $aNewList[$j] = "---" And $aNewList[$j - 1] = "done" Then
                        MsgBox(0, "", "程序准备完成,可以运行" & $aNewList[$j])
                        ExitLoop
                EndIf

      Next
EndFunc   ;==>_add_report_items111




Func WM_NOTIFY($hWnd, $iMsg, $iwParam, $ilParam)

      #forceref $hWnd, $iMsg, $iwParam
      Local $hWndFrom, $iIDFrom, $iCode, $tNMHDR, $hWndListView, $tInfo

      $hWndListView = GUICtrlGetHandle($hListView)
      $tNMHDR = DllStructCreate($tagNMHDR, $ilParam)
      $hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom"))
      $iIDFrom = DllStructGetData($tNMHDR, "IDFrom")
      $iCode = DllStructGetData($tNMHDR, "Code")
      ;$Index = GUICtrlRead($hListView)


      Switch $hWndFrom
                Case $hWndListView


                        Switch $iCode
                              Case $NM_DBLCLK
                                        $tInfo = DllStructCreate($tagNMITEMACTIVATE, $ilParam)
                                        $Index = DllStructGetData($tInfo, "Index")
                                        $DoubleClick = _GUICtrlListView_GetItemText($hListView, $Index, 1) ;GetItemText IP address
                                        MsgBox(0, "Message", $DoubleClick)


                              Case $NM_RCLICK
                                        $tInfo = DllStructCreate($tagNMITEMACTIVATE, $ilParam)
                                        $Index = DllStructGetData($tInfo, "Index")
                                        $SubItem = DllStructGetData($tInfo, "SubItem")
                                        ;$sItemText =_GUICtrlListView_GetItemText($hWndListView, $Index, $SubItem) ;GetItemText every location
                                        $sItemText = _GUICtrlListView_GetItemText($hListView, $Index, 12)
                                        $sItemTextFilename111 = _GUICtrlListView_GetItemText($hListView, $Index, 3)
                                        $sItemTextFilenameTester = _GUICtrlListView_GetItemText($hListView, $Index, 13)

                                        $sItemTextFilename1 = _GUICtrlListView_GetItemText($hListView, $Index, 1)
                                        $sItemTextFilename4 = _GUICtrlListView_GetItemText($hListView, $Index, 4)
                                        $sItemTextFilename5 = _GUICtrlListView_GetItemText($hListView, $Index, 5)
                                        $sItemTextFilename7 = _GUICtrlListView_GetItemText($hListView, $Index, 7)



                              Case $NM_CUSTOMDRAW
                                        Local $tCustDraw = DllStructCreate($tagNMLVCUSTOMDRAW, $ilParam)
                                        Local $iDrawStage = DllStructGetData($tCustDraw, "dwDrawStage")
                                        If $iDrawStage = $CDDS_PREPAINT Then Return $CDRF_NOTIFYITEMDRAW
                                        If $iDrawStage = $CDDS_ITEMPREPAINT Then Return $CDRF_NOTIFYSUBITEMDRAW
                                        Local $iItem = DllStructGetData($tCustDraw, "dwItemSpec")
                                        Local $iSubItem = DllStructGetData($tCustDraw, "iSubItem")
                                        Local $sComboRead = GUICtrlRead($idComboBox)

                        EndSwitch
      EndSwitch
      Return $GUI_RUNDEFMSG

EndFunc   ;==>WM_NOTIFY



Func _ShowText()
      If $sItemText <> "" Then MsgBox(0, "Test", $sItemText)
EndFunc   ;==>_ShowText



Func exporttoexcel() ;EXPORT TO EXCEL
      $col = 0
      $count = _GUICtrlListView_GetItemCount($hListView)
      GUICtrlSetState($export, $gui_enable)



      $excel = _ExcelBookNew()
      $oWorkbook = _Excel_BookNew($excel)


      _ExcelWriteCell($excel, "1", 2, 2)
      _ExcelWriteCell($excel, "2", 2, 3)
      _ExcelWriteCell($excel, "3", 2, 4)
      _ExcelWriteCell($excel, "4", 2, 5)
      _ExcelWriteCell($excel, "5", 2, 6)
      _ExcelWriteCell($excel, "6", 2, 7)
      _ExcelWriteCell($excel, "7", 2, 8)
      _ExcelWriteCell($excel, "8", 2, 9)
      _ExcelWriteCell($excel, "9", 2, 10)

      For $colexcel = 0 To $col

                $i = 0
                Do
                        _ExcelWriteCell($excel, _GUICtrlListView_GetItemText($hListView, $i, 0), 3 + $i, 1)
                        _ExcelWriteCell($excel, _GUICtrlListView_GetItemText($hListView, $i, 1), 3 + $i, 2)
                        _ExcelWriteCell($excel, _GUICtrlListView_GetItemText($hListView, $i, 2), 3 + $i, 3)
                        _ExcelWriteCell($excel, _GUICtrlListView_GetItemText($hListView, $i, 3), 3 + $i, 4)
                        _ExcelWriteCell($excel, _GUICtrlListView_GetItemText($hListView, $i, 4), 3 + $i, 5)
                        _ExcelWriteCell($excel, _GUICtrlListView_GetItemText($hListView, $i, 5), 3 + $i, 6)
                        _ExcelWriteCell($excel, _GUICtrlListView_GetItemText($hListView, $i, 6), 3 + $i, 7)
                        _ExcelWriteCell($excel, _GUICtrlListView_GetItemText($hListView, $i, 7), 3 + $i, 8)
                        _ExcelWriteCell($excel, _GUICtrlListView_GetItemText($hListView, $i, 8), 3 + $i, 9)

                        $i = $i + 1
                Until $i = $count
      Next


      MsgBox(64, "Message", "Export completed!" & @CRLF _
                         & " " & @CRLF _
                         & "Output excel file on your Desktop!", 2)

      _Excel_BookSaveAs($oWorkbook, @DesktopDir & "\TestStatus_Export_" & @YEAR & @MON & @MDAY & "_" & @HOUR & @MIN & @SEC & ".xlsx", Default, False)
      _Excel_Close($excel, True, True)

EndFunc   ;==>exporttoexcel



Func _ExcelBookNew($fVisible = 0)
      Local $oExcel = ObjCreate("Excel.Application")
      If Not IsObj($oExcel) Then Return SetError(1, 0, 0)
      If Not IsNumber($fVisible) Then Return SetError(2, 0, 0)
      If $fVisible > 1 Then $fVisible = 1
      If $fVisible < 0 Then $fVisible = 0
      With $oExcel
                .Visible = $fVisible
                .WorkBooks.Add
                .ActiveWorkbook.Sheets(1).Select()
      EndWith
      Return $oExcel


EndFunc   ;==>_ExcelBookNew



Func _ExcelWriteCell($oExcel, $sValue, $sRangeOrRow, $iColumn = 1)
      If Not IsObj($oExcel) Then Return SetError(1, 0, 0)
      If Not StringRegExp($sRangeOrRow, "", 0) Then
                If $sRangeOrRow < 1 Then Return SetError(2, 0, 0)
                If $iColumn < 1 Then Return SetError(2, 1, 0)
                $oExcel.Activesheet.Cells($sRangeOrRow, $iColumn).Value = $sValue
                Return 1
      Else
                $oExcel.Activesheet.Range($sRangeOrRow).Value = $sValue
                Return 1
      EndIf
EndFunc   ;==>_ExcelWriteCell



Func Timer0()

      _TicksToTime(Int(TimerDiff($g_hTimer0)), $g_iHour0, $g_iMins0, $g_iSecs0)
      Local $sTime0 = $g_sTime0 ; save current time to be able to test and avoid flicker..
      $g_sTime0 = StringFormat("Total Time: %02s hour, %02s min, %02s sec", $g_iHour0, $g_iMins0, $g_iSecs0)
      If $sTime0 <> $g_sTime0 Then _GUICtrlStatusBar_SetText($StatusBar1, $g_sTime0)

EndFunc   ;==>Timer0

ipmitool 发表于 2016-12-30 14:31:23

回复 13# q410225


    感謝您的幫忙, 快要成功了,
想問一下計時器的部分怪怪的, 沒有辦法這樣嗎 ---> $g_sTime & $j#include <ini.au3>
#include <File.au3>
#include <Date.au3>
#include <Array.au3>
#include <Excel.au3>
#include <Constants.au3>
#include <GuiListView.au3>
#include <GuiImageList.au3>
#include <GuiStatusBar.au3>
#include <FileConstants.au3>
#include <GUIConstantsEx.au3>
#include <MsgBoxConstants.au3>
#include <WindowsConstants.au3>

Global $g_hTimer0, $g_iSecs0, $g_iMins0, $g_iHour0, $g_sTime0
Global $g_hTimer1, $g_iSecs1, $g_iMins1, $g_iHour1, $g_sTime1
Global $g_hTimer2, $g_iSecs2, $g_iMins2, $g_iHour2, $g_sTime2
Global $g_hTimer3, $g_iSecs3, $g_iMins3, $g_iHour3, $g_sTime3
Global $g_hTimer4, $g_iSecs4, $g_iMins4, $g_iHour4, $g_sTime4

$Form1_1 = GUICreate("Test Schedule Management", 350, 330)
$hListView = GUICtrlCreateListView("", 5, 5, 335, 245) ;, $LVS_SHOWSELALWAYS, $LVS_EX_GRIDLINES + $LVS_EX_FULLROWSELECT + $LVS_EX_DOUBLEBUFFER


Global $sItemText ;this will store the text of the last right-clicked item.
Global $sItemTextFilename111
Global $sItemTextFilenameTester
Global $sItemTextFilename1
Global $sItemTextFilename4
Global $sItemTextFilename5
Global $sItemTextFilename7


Global $aNewList
$aNewList = 300
;===================================================================
$Button1 = GUICtrlCreateButton("View", 360, 15, 73, 33)
;$Button2 = GUICtrlCreateButton("Done", 300, 362, 73, 33)
$export = GUICtrlCreateButton("Export to Excel", 100, 562, 100, 33)
$export2 = GUICtrlCreateButton("Export to HTML", 220, 562, 100, 33)

$cInput_Filter = GUICtrlCreateInput("", 11, 620, 140, 20)
$cButton_Filter = GUICtrlCreateButton("Filter", 160, 618, 75, 23)
$cButton_All = GUICtrlCreateButton("Refresh", 360, 60, 73, 33)
GUICtrlCreateLabel('Input Test Case, Product, Serial Number or Tester to filter data', 11, 650, 300, 100)
;===================================================================
$idComboBox = GUICtrlCreateCombo("", 17, 1000, 135, 33)

;$analyze = GUICtrlCreateButton("Analyze", 940, 562, 100, 33)

Local $hGUI, $ahIcons, $StatusBar0
Local $aParts =

$StatusBar0 = _GUICtrlStatusBar_Create($Form1_1)
_GUICtrlStatusBar_SetParts($StatusBar0, $aParts)
_GUICtrlStatusBar_SetText($StatusBar0, 'Tool will auto execute!!!')
_GUICtrlStatusBar_SetText($StatusBar0, 'Foxconn@SIT1', 1)
_GUICtrlStatusBar_SetText($StatusBar0, " Version: v1.0 ", 2)

_GUICtrlListView_SetExtendedListViewStyle($hListView, BitOR($LVS_EX_FULLROWSELECT, $LVS_EX_GRIDLINES, $LVS_EX_SUBITEMIMAGES))



;;;;;;;;; Read testconfig.ini ;;;;;;;;;
;;;;;;;;; Read testconfig.ini ;;;;;;;;;
;;;;;;;;; Read testconfig.ini ;;;;;;;;;

$testerread = _IniReadSection_line(@ScriptDir & '\testconfig.ini', 'TestConfig')
$tester = $testerread

;;;;;;;;; Read testconfig.ini ;;;;;;;;;
;;;;;;;;; Read testconfig.ini ;;;;;;;;;
;;;;;;;;; Read testconfig.ini ;;;;;;;;;



GUISetState()

_GUICtrlListView_RegisterSortCallBack($hListView)

_GUICtrlListView_AddColumn($hListView, "File Name", 0) ;disappear file name
_GUICtrlListView_AddColumn($hListView, "Test Case", 140)
_GUICtrlListView_AddColumn($hListView, "Status", 80)
_GUICtrlListView_AddColumn($hListView, "Process Time", 100)


_GUICtrlListView_SetItemCount($hListView, 5000)


ControlClick("Test Schedule Management", "", "")


While 1

      GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY") ;Double click value get

      Switch GUIGetMsg()



                Case $GUI_EVENT_CLOSE
                        Exit (_GUICtrlListView_UnRegisterSortCallBack($hListView))
                        ExitLoop



                Case $hListView
                        _GUICtrlListView_SortItems($hListView, GUICtrlGetState($hListView))



                Case $Button1
                        $g_hTimer0 = TimerInit()
                                                                $g_hTimer1=TimerInit()
                                                                $g_hTimer2=TimerInit()
                                                                $g_hTimer3=TimerInit()
                                                                $g_hTimer4=TimerInit()
                        ;GUICtrlSetState($Button1,$gui_disable)
                        GUICtrlSetState($cButton_All, $gui_enable)

                        ProgressOn("Test Management Progress Meter", "Progress...", "0%")
                        Sleep(100)

                        ProgressSet(0, "0%")

                        ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 10mins time count
                        Dim $tl = GUICtrlCreateLabel('00 min, 00 sec', 10000, 10000, 150, 15)
                        GUISetState()
                        Dim $timer = TimerInit(), $Day = 0

                        AdlibRegister('Timer0', 500)

                        ;AdlibRegister("Timer2",500)
                        ;AdlibRegister("Timer3",500)
                        ;AdlibRegister("Timer4",500)

                        Local $hGUI, $ahIcons, $StatusBar1
                        Local $aParts =
                        $StatusBar1 = _GUICtrlStatusBar_Create($Form1_1)
                        _GUICtrlStatusBar_SetParts($StatusBar1, $aParts)
                        _GUICtrlStatusBar_SetText($StatusBar1, 'Tool will auto execute!!!')
                        _GUICtrlStatusBar_SetText($StatusBar1, 'Foxconn@SIT1', 1)
                        _GUICtrlStatusBar_SetText($StatusBar1, " Version: v1.0 ", 2)
                        ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 10 mins time count

                        Dim $OldList = "", $aNewList2 = ""


                        AdlibRegister('_add_report_items111', 5000) ;reflash every 10min is 59925
                        ProgressSet(100, "100%")
                        Sleep(500)

                        ProgressOff()

                                                _add_report_items111()


                Case $export
                        MsgBox(0, "Export", "Export list view data to Excel", 1)
                        exporttoexcel()



                Case $export2
                        FileChangeDir(@DesktopDir)
                        FileWrite("TestStatus_Export_" & @YEAR & @MON & @MDAY & @HOUR & @MIN & @SEC & ".html", _ListViewToHTMLTable($hListView))
                        MsgBox(0, "", "Dump html file to: " & @DesktopDir & "\" & "table" & @YEAR & @MON & @MDAY & @HOUR & @MIN & ".html", 1)


      EndSwitch
WEnd





Func _add_report_items111()

      FileChangeDir(@ScriptDir)
      Local $path, $aFileList, $aNewList
      $path = @ScriptDir

      $aFileList = _FileListToArray($path, "*.txt")

      If IsArray($aFileList) Then
                ReDim $aNewList ;;;
                Local $sComboTxt = "All Member"
                GUICtrlSetData($idComboBox, "")
                For $i = 1 To UBound($aFileList) - 1 Step 1
                        $aNewList = $i
                        ;$aNewList[$i] = $aFileList[$i]
                        $aNewList[$i] = FileReadLine($aFileList[$i], 1)
                        $aNewList[$i] = FileReadLine($aFileList[$i], 2)
                        $aNewList[$i] = ""
                        $aNewList[$i] = FileReadLine($aFileList[$i], 4)
                        $aNewList[$i] = FileReadLine($aFileList[$i], 5)
                        $aNewList[$i] = FileReadLine($aFileList[$i], 6)
                        $aNewList[$i] = FileReadLine($aFileList[$i], 7)
                        $aNewList[$i] = FileReadLine($aFileList[$i], 8)

                        If Not StringInStr($sComboTxt, $aNewList[$i]) Then $sComboTxt &= "|" & $aNewList[$i]

                Next

                _GUICtrlListView_DeleteAllItems(GUICtrlGetHandle($hListView))
                _GUICtrlListView_AddArray($hListView, $aNewList)

                For $j = 0 To UBound($aNewList) - 1 Step 1
                        _GUICtrlListView_SetItemText($hListView, $j + 1, " ", 7)
                Next

;~                                                                     $aNewList=$aNewList2
                ;GUICtrlSetData($idComboBox, $sComboTxt, "All Member")
                EndIf

      $j = UBound($aNewList) - 1


      If $aNewList[$j] = 'done' Then
                MsgBox(0, "", "全部是done了,可以注销了")
                AdlibUnRegister('_add_report_items111')
                                AdlibUnRegister('Timer0')
                EndIf



      $aNewList = 'done';先設第一個為Done

      For $j = 1 To UBound($aNewList) - 1
                If $aNewList[$j] = "done" Then
                        AdlibUnRegister("Timer" & $j)
                        _GUICtrlListView_SetItemText($hListView, $j, $g_sTime1, 3)
                                                        ;$g_hTimer0=TimerInit()
                                                       ;        $g_hTimer1=TimerInit()
                                                        ;        $g_hTimer2=TimerInit()
                                                        ;        $g_hTimer3=TimerInit()
                                                        ;        $g_hTimer4=TimerInit()
                                EndIf

                                If $aNewList[$j] = "Preparing" Then
                        AdlibRegister("Timer" & $j)
                        _GUICtrlListView_SetItemText($hListView, $j, $g_sTime&$j, 3)
                                                ;AdlibRegister("Timer" & $j,500)
                                                        ;$g_hTimer0=TimerInit()
                                                       ;        $g_hTimer1=TimerInit()
                                                        ;        $g_hTimer2=TimerInit()
                                                        ;        $g_hTimer3=TimerInit()
                                                        ;        $g_hTimer4=TimerInit()
                                EndIf

                If $aNewList[$j] = "---" And $aNewList[$j - 1] = "done" Then
                        MsgBox(0, "", "程序准备完成,可以运行" & $aNewList[$j],1)
                                                run("_"&$aNewList[$j]&".exe")


                        ExitLoop
                EndIf

      Next
EndFunc   ;==>_add_report_items111







Func Timer1()

      _TicksToTime(Int(TimerDiff($g_hTimer1)), $g_iHour1, $g_iMins1, $g_iSecs1)
      Local $sTime1 = $g_sTime1 ; save current time to be able to test and avoid flicker..
      $g_sTime1 = StringFormat("%02i:%02i:%02i", $g_iHour1, $g_iMins1, $g_iSecs1)
      If $sTime1 <> $g_sTime1 Then _GUICtrlListView_SetItemText($hListView, 1, $g_sTime1, 3)

EndFunc   ;==>Timer1



Func Timer2()

      _TicksToTime(Int(TimerDiff($g_hTimer2)), $g_iHour2, $g_iMins2, $g_iSecs2)
      Local $sTime2 = $g_sTime2 ; save current time to be able to test and avoid flicker..
      $g_sTime2 = StringFormat("%02i:%02i:%02i", $g_iHour2, $g_iMins2, $g_iSecs2)
      If $sTime2 <> $g_sTime2 Then _GUICtrlListView_SetItemText($hListView, 2, $g_sTime2, 3)

EndFunc   ;==>Timer2



Func Timer3()

      _TicksToTime(Int(TimerDiff($g_hTimer3)), $g_iHour3, $g_iMins3, $g_iSecs3)
      Local $sTime3 = $g_sTime3 ; save current time to be able to test and avoid flicker..
      $g_sTime3 = StringFormat("%02i:%02i:%02i", $g_iHour3, $g_iMins3, $g_iSecs3)
      If $sTime3 <> $g_sTime3 Then _GUICtrlListView_SetItemText($hListView, 3, $g_sTime3, 3)

EndFunc   ;==>Timer3



Func Timer4()

      _TicksToTime(Int(TimerDiff($g_hTimer4)), $g_iHour4, $g_iMins4, $g_iSecs4)
      Local $sTime4 = $g_sTime4 ; save current time to be able to test and avoid flicker..
      $g_sTime4 = StringFormat("%02i:%02i:%02i", $g_iHour4, $g_iMins4, $g_iSecs4)
      If $sTime4 <> $g_sTime4 Then _GUICtrlListView_SetItemText($hListView, 4, $g_sTime4, 3)

EndFunc   ;==>Timer4



Func _ListViewToHTMLTable($hList)
      Local $sHTML, $iColCount, $iRowCount
      $iColCount = _GUICtrlListView_GetColumnCount($hList)

      $sHTML = "<P></P><B>Test Management Tool Report</B>" & "<P></P>" & "<table width='1500' border='2' BGColor='#c4d6f6'>" & @LF & "<tr>" & @LF
      ;创建表头
      For $i = 0 To $iColCount - 1
                $aInfo = _GUICtrlListView_GetColumn($hList, $i)
                $sHTML = $sHTML & "<th scope='col' BGColor='#FFFFFF'>" & $aInfo & "</th>" & @LF
      Next
      ;添加数据
      $iRowCount = _GUICtrlListView_GetItemCount($hList)
      For $i = 0 To $iRowCount - 1
                $sHTML = $sHTML & "<tr>" & @LF
                For $j = 0 To $iColCount - 1
                        $aInfo = _GUICtrlListView_GetItem($hList, $i, $j)
                        $sHTML = $sHTML & "<th>" & $aInfo & "</th>" & @LF
                Next
                $sHTML = $sHTML & "</tr>" & @LF
      Next

      $sHTML = $sHTML & "</table>"
      ;MsgBox(0,"",$sHTMl)
      Return $sHTML
EndFunc   ;==>_ListViewToHTMLTable







Func WM_NOTIFY($hWnd, $iMsg, $iwParam, $ilParam)

      #forceref $hWnd, $iMsg, $iwParam
      Local $hWndFrom, $iIDFrom, $iCode, $tNMHDR, $hWndListView, $tInfo

      $hWndListView = GUICtrlGetHandle($hListView)
      $tNMHDR = DllStructCreate($tagNMHDR, $ilParam)
      $hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom"))
      $iIDFrom = DllStructGetData($tNMHDR, "IDFrom")
      $iCode = DllStructGetData($tNMHDR, "Code")
      ;$Index = GUICtrlRead($hListView)


      Switch $hWndFrom
                Case $hWndListView


                        Switch $iCode
                              Case $NM_DBLCLK
                                        $tInfo = DllStructCreate($tagNMITEMACTIVATE, $ilParam)
                                        $Index = DllStructGetData($tInfo, "Index")
                                        $DoubleClick = _GUICtrlListView_GetItemText($hListView, $Index, 1) ;GetItemText IP address
                                        MsgBox(0, "Message", $DoubleClick)


                              Case $NM_RCLICK
                                        $tInfo = DllStructCreate($tagNMITEMACTIVATE, $ilParam)
                                        $Index = DllStructGetData($tInfo, "Index")
                                        $SubItem = DllStructGetData($tInfo, "SubItem")
                                        ;$sItemText =_GUICtrlListView_GetItemText($hWndListView, $Index, $SubItem) ;GetItemText every location
                                        $sItemText = _GUICtrlListView_GetItemText($hListView, $Index, 12)
                                        $sItemTextFilename111 = _GUICtrlListView_GetItemText($hListView, $Index, 3)
                                        $sItemTextFilenameTester = _GUICtrlListView_GetItemText($hListView, $Index, 13)

                                        $sItemTextFilename1 = _GUICtrlListView_GetItemText($hListView, $Index, 1)
                                        $sItemTextFilename4 = _GUICtrlListView_GetItemText($hListView, $Index, 4)
                                        $sItemTextFilename5 = _GUICtrlListView_GetItemText($hListView, $Index, 5)
                                        $sItemTextFilename7 = _GUICtrlListView_GetItemText($hListView, $Index, 7)



                              Case $NM_CUSTOMDRAW
                                        Local $tCustDraw = DllStructCreate($tagNMLVCUSTOMDRAW, $ilParam)
                                        Local $iDrawStage = DllStructGetData($tCustDraw, "dwDrawStage")
                                        If $iDrawStage = $CDDS_PREPAINT Then Return $CDRF_NOTIFYITEMDRAW
                                        If $iDrawStage = $CDDS_ITEMPREPAINT Then Return $CDRF_NOTIFYSUBITEMDRAW
                                        Local $iItem = DllStructGetData($tCustDraw, "dwItemSpec")
                                        Local $iSubItem = DllStructGetData($tCustDraw, "iSubItem")
                                        Local $sComboRead = GUICtrlRead($idComboBox)

                        EndSwitch
      EndSwitch
      Return $GUI_RUNDEFMSG

EndFunc   ;==>WM_NOTIFY



Func _ShowText()
      If $sItemText <> "" Then MsgBox(0, "Test", $sItemText)
EndFunc   ;==>_ShowText



Func exporttoexcel() ;EXPORT TO EXCEL
      $col = 0
      $count = _GUICtrlListView_GetItemCount($hListView)
      GUICtrlSetState($export, $gui_enable)



      $excel = _ExcelBookNew()
      $oWorkbook = _Excel_BookNew($excel)


      _ExcelWriteCell($excel, "1", 2, 2)
      _ExcelWriteCell($excel, "2", 2, 3)
      _ExcelWriteCell($excel, "3", 2, 4)
      _ExcelWriteCell($excel, "4", 2, 5)
      _ExcelWriteCell($excel, "5", 2, 6)
      _ExcelWriteCell($excel, "6", 2, 7)
      _ExcelWriteCell($excel, "7", 2, 8)
      _ExcelWriteCell($excel, "8", 2, 9)
      _ExcelWriteCell($excel, "9", 2, 10)

      For $colexcel = 0 To $col

                $i = 0
                Do
                        _ExcelWriteCell($excel, _GUICtrlListView_GetItemText($hListView, $i, 0), 3 + $i, 1)
                        _ExcelWriteCell($excel, _GUICtrlListView_GetItemText($hListView, $i, 1), 3 + $i, 2)
                        _ExcelWriteCell($excel, _GUICtrlListView_GetItemText($hListView, $i, 2), 3 + $i, 3)
                        _ExcelWriteCell($excel, _GUICtrlListView_GetItemText($hListView, $i, 3), 3 + $i, 4)
                        _ExcelWriteCell($excel, _GUICtrlListView_GetItemText($hListView, $i, 4), 3 + $i, 5)
                        _ExcelWriteCell($excel, _GUICtrlListView_GetItemText($hListView, $i, 5), 3 + $i, 6)
                        _ExcelWriteCell($excel, _GUICtrlListView_GetItemText($hListView, $i, 6), 3 + $i, 7)
                        _ExcelWriteCell($excel, _GUICtrlListView_GetItemText($hListView, $i, 7), 3 + $i, 8)
                        _ExcelWriteCell($excel, _GUICtrlListView_GetItemText($hListView, $i, 8), 3 + $i, 9)

                        $i = $i + 1
                Until $i = $count
      Next


      MsgBox(64, "Message", "Export completed!" & @CRLF _
                         & " " & @CRLF _
                         & "Output excel file on your Desktop!", 2)

      _Excel_BookSaveAs($oWorkbook, @DesktopDir & "\TestStatus_Export_" & @YEAR & @MON & @MDAY & "_" & @HOUR & @MIN & @SEC & ".xlsx", Default, False)
      _Excel_Close($excel, True, True)

EndFunc   ;==>exporttoexcel



Func _ExcelBookNew($fVisible = 0)
      Local $oExcel = ObjCreate("Excel.Application")
      If Not IsObj($oExcel) Then Return SetError(1, 0, 0)
      If Not IsNumber($fVisible) Then Return SetError(2, 0, 0)
      If $fVisible > 1 Then $fVisible = 1
      If $fVisible < 0 Then $fVisible = 0
      With $oExcel
                .Visible = $fVisible
                .WorkBooks.Add
                .ActiveWorkbook.Sheets(1).Select()
      EndWith
      Return $oExcel


EndFunc   ;==>_ExcelBookNew



Func _ExcelWriteCell($oExcel, $sValue, $sRangeOrRow, $iColumn = 1)
      If Not IsObj($oExcel) Then Return SetError(1, 0, 0)
      If Not StringRegExp($sRangeOrRow, "", 0) Then
                If $sRangeOrRow < 1 Then Return SetError(2, 0, 0)
                If $iColumn < 1 Then Return SetError(2, 1, 0)
                $oExcel.Activesheet.Cells($sRangeOrRow, $iColumn).Value = $sValue
                Return 1
      Else
                $oExcel.Activesheet.Range($sRangeOrRow).Value = $sValue
                Return 1
      EndIf
EndFunc   ;==>_ExcelWriteCell



Func Timer0()

      _TicksToTime(Int(TimerDiff($g_hTimer0)), $g_iHour0, $g_iMins0, $g_iSecs0)
      Local $sTime0 = $g_sTime0 ; save current time to be able to test and avoid flicker..
      $g_sTime0 = StringFormat("Total Time: %02s hour, %02s min, %02s sec", $g_iHour0, $g_iMins0, $g_iSecs0)
      If $sTime0 <> $g_sTime0 Then _GUICtrlStatusBar_SetText($StatusBar1, $g_sTime0)

EndFunc   ;==>Timer0

q410225 发表于 2016-12-30 15:41:45

回复 14# ipmitool

你可以用Eval('g_sTime' & $j)
页: [1] 2
查看完整版本: [已解决]list view 讀 txt 檔案數量不對程式產生錯誤