12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232 |
- #include-once
- #include "Array.au3"
- #include "ExcelConstants.au3"
- ; #INDEX# =======================================================================================================================
- ; Title .........: Microsoft Excel Function Library
- ; AutoIt Version : 3.3.14.5
- ; Language ......: English
- ; Description ...: A collection of functions for accessing and manipulating Microsoft Excel files
- ; Author(s) .....: SEO (Locodarwin), DaLiMan, Stanley Lim, MikeOsdx, MRDev, big_daddy, PsaltyDS, litlmike, water, spiff59, golfinhu, bowmore, GMX, Andreu, danwilli
- ; Resources .....:
- ; ===============================================================================================================================
- ; #CURRENT# =====================================================================================================================
- ; _Excel_Open
- ; _Excel_Close
- ; _Excel_BookAttach
- ; _Excel_BookClose
- ; _Excel_BookList
- ; _Excel_BookNew
- ; _Excel_BookOpen
- ; _Excel_BookOpenText
- ; _Excel_BookSave
- ; _Excel_BookSaveAs
- ; _Excel_ColumnToLetter
- ; _Excel_ColumnToNumber
- ; _Excel_ConvertFormula
- ; _Excel_Export
- ; _Excel_FilterGet
- ; _Excel_FilterSet
- ; _Excel_PictureAdd
- ; _Excel_Print
- ; _Excel_RangeCopyPaste
- ; _Excel_RangeDelete
- ; _Excel_RangeFind
- ; _Excel_RangeInsert
- ; _Excel_RangeLinkAddRemove
- ; _Excel_RangeRead
- ; _Excel_RangeReplace
- ; _Excel_RangeSort
- ; _Excel_RangeValidate
- ; _Excel_RangeWrite
- ; _Excel_SheetAdd
- ; _Excel_SheetCopyMove
- ; _Excel_SheetDelete
- ; _Excel_SheetList
- ; ===============================================================================================================================
- ; #INTERNAL_USE_ONLY#============================================================================================================
- ; __Excel_CloseOnQuit
- ; __Excel_COMErrFunc
- ; ===============================================================================================================================
- ; #FUNCTION# ====================================================================================================================
- ; Author ........: water
- ; Modified ......:
- ; ===============================================================================================================================
- Func _Excel_Open($bVisible = Default, $bDisplayAlerts = Default, $bScreenUpdating = Default, $bInteractive = Default, $bForceNew = Default)
- ; Error handler, automatic cleanup at end of function
- Local $oError = ObjEvent("AutoIt.Error", "__Excel_COMErrFunc")
- #forceref $oError
- Local $oExcel, $bApplCloseOnQuit = False
- If $bVisible = Default Then $bVisible = True
- If $bDisplayAlerts = Default Then $bDisplayAlerts = False
- If $bScreenUpdating = Default Then $bScreenUpdating = True
- If $bInteractive = Default Then $bInteractive = True
- If $bForceNew = Default Then $bForceNew = False
- If Not $bForceNew Then $oExcel = ObjGet("", "Excel.Application")
- If $bForceNew Or @error Then
- $oExcel = ObjCreate("Excel.Application")
- If @error Or Not IsObj($oExcel) Then Return SetError(1, @error, 0)
- $bApplCloseOnQuit = True
- EndIf
- __Excel_CloseOnQuit($oExcel, $bApplCloseOnQuit)
- $oExcel.Visible = $bVisible
- $oExcel.DisplayAlerts = $bDisplayAlerts
- $oExcel.ScreenUpdating = $bScreenUpdating
- $oExcel.Interactive = $bInteractive
- Return SetError(0, $bApplCloseOnQuit, $oExcel)
- EndFunc ;==>_Excel_Open
- ; #FUNCTION# ====================================================================================================================
- ; Author ........: water
- ; Modified ......:
- ; ===============================================================================================================================
- Func _Excel_Close(ByRef $oExcel, $bSaveChanges = Default, $bForceClose = Default)
- ; Error handler, automatic cleanup at end of function
- Local $oError = ObjEvent("AutoIt.Error", "__Excel_COMErrFunc")
- #forceref $oError
- If $bSaveChanges = Default Then $bSaveChanges = True
- If $bForceClose = Default Then $bForceClose = False
- If Not IsObj($oExcel) Or ObjName($oExcel, 1) <> "_Application" Then Return SetError(1, 0, 0)
- If $bSaveChanges Then
- For $oWorkbook In $oExcel.Workbooks
- If Not $oWorkbook.Saved Then
- $oWorkbook.Save()
- If @error Then Return SetError(3, @error, 0)
- EndIf
- Next
- EndIf
- If __Excel_CloseOnQuit($oExcel) Or $bForceClose Then
- $oExcel.Quit()
- If @error Then Return SetError(2, @error, 0)
- __Excel_CloseOnQuit($oExcel, False)
- $oExcel = 0
- EndIf
- Return 1
- EndFunc ;==>_Excel_Close
- ; #FUNCTION# ====================================================================================================================
- ; Author ........: Bob Anthony (big_daddy)
- ; Modified.......: water
- ; ===============================================================================================================================
- Func _Excel_BookAttach($sString, $sMode = Default, $oInstance = Default)
- ; Error handler, automatic cleanup at end of function
- Local $oError = ObjEvent("AutoIt.Error", "__Excel_COMErrFunc")
- #forceref $oError
- Local $oWorkbook, $iCount = 0, $sCLSID_Workbook = "{00020819-0000-0000-C000-000000000046}" ; Microsoft.Office.Interop.Excel.WorkbookClass
- If $sMode = Default Then $sMode = "FilePath"
- While True
- $oWorkbook = ObjGet("", $sCLSID_Workbook, $iCount + 1)
- If @error Then Return SetError(1, @error, 0)
- $iCount += 1
- If $oInstance <> Default And $oInstance <> $oWorkbook.Parent Then ContinueLoop
- Switch $sMode
- Case "filename"
- If $oWorkbook.Name = $sString Then Return $oWorkbook
- Case "filepath"
- If $oWorkbook.FullName = $sString Then Return $oWorkbook
- Case "title"
- If $oWorkbook.Application.Caption = $sString Then Return $oWorkbook
- Case Else
- Return SetError(2, 0, 0)
- EndSwitch
- WEnd
- EndFunc ;==>_Excel_BookAttach
- ; #FUNCTION# ====================================================================================================================
- ; Author ........: SEO <locodarwin at yahoo dot com>
- ; Modified.......: big_daddy, litlmike, water
- ; ===============================================================================================================================
- Func _Excel_BookClose(ByRef $oWorkbook, $bSave = Default)
- ; Error handler, automatic cleanup at end of function
- Local $oError = ObjEvent("AutoIt.Error", "__Excel_COMErrFunc")
- #forceref $oError
- If Not IsObj($oWorkbook) Or ObjName($oWorkbook, 1) <> "_Workbook" Then Return SetError(1, 0, 0)
- If $bSave = Default Then $bSave = True
- If $bSave And Not $oWorkbook.Saved Then
- $oWorkbook.Save()
- If @error Then Return SetError(2, @error, 0)
- EndIf
- $oWorkbook.Close()
- If @error Then Return SetError(3, @error, 0)
- $oWorkbook = 0
- Return 1
- EndFunc ;==>_Excel_BookClose
- ; #FUNCTION# ====================================================================================================================
- ; Author ........: water
- ; Modified.......:
- ; ===============================================================================================================================
- Func _Excel_BookList($oExcel = Default)
- ; Error handler, automatic cleanup at end of function
- Local $oError = ObjEvent("AutoIt.Error", "__Excel_COMErrFunc")
- #forceref $oError
- Local $aBooks[1][3], $iIndex = 0
- If IsObj($oExcel) Then
- If ObjName($oExcel, 1) <> "_Application" Then Return SetError(1, 0, 0)
- Local $iTemp = $oExcel.Workbooks.Count
- ReDim $aBooks[$iTemp][3]
- For $iIndex = 0 To $iTemp - 1
- $aBooks[$iIndex][0] = $oExcel.Workbooks($iIndex + 1)
- $aBooks[$iIndex][1] = $oExcel.Workbooks($iIndex + 1).Name
- $aBooks[$iIndex][2] = $oExcel.Workbooks($iIndex + 1).Path
- Next
- Else
- If $oExcel <> Default Then Return SetError(1, 0, 0)
- Local $oWorkbook, $sCLSID_Workbook = "{00020819-0000-0000-C000-000000000046}"
- While True
- $oWorkbook = ObjGet("", $sCLSID_Workbook, $iIndex + 1)
- If @error Then ExitLoop
- ReDim $aBooks[$iIndex + 1][3]
- $aBooks[$iIndex][0] = $oWorkbook
- $aBooks[$iIndex][1] = $oWorkbook.Name
- $aBooks[$iIndex][2] = $oWorkbook.Path
- $iIndex += 1
- WEnd
- EndIf
- Return $aBooks
- EndFunc ;==>_Excel_BookList
- ; #FUNCTION# ====================================================================================================================
- ; Author ........: SEO <locodarwin at yahoo dot com>
- ; Modified.......: litlmike, water
- ; ===============================================================================================================================
- Func _Excel_BookNew($oExcel, $iSheets = Default)
- ; Error handler, automatic cleanup at end of function
- Local $oError = ObjEvent("AutoIt.Error", "__Excel_COMErrFunc")
- #forceref $oError
- If Not IsObj($oExcel) Or ObjName($oExcel, 1) <> "_Application" Then Return SetError(1, 0, 0)
- With $oExcel
- If $iSheets <> Default Then
- If $iSheets < 1 Or $iSheets > 255 Then Return SetError(4, 0, 0)
- Local $iSheetsBackup = .SheetsInNewWorkbook
- .SheetsInNewWorkbook = $iSheets
- If @error Then Return SetError(2, @error, 0)
- EndIf
- Local $oWorkbook = .Workbooks.Add()
- If @error Then
- Local $iError = @error
- If $iSheets <> Default Then .SheetsInNewWorkbook = $iSheetsBackup
- Return SetError(3, $iError, 0)
- EndIf
- If $iSheets <> Default Then .SheetsInNewWorkbook = $iSheetsBackup
- EndWith
- Return $oWorkbook
- EndFunc ;==>_Excel_BookNew
- ; #FUNCTION# ====================================================================================================================
- ; Author ........: SEO <locodarwin at yahoo dot com>
- ; Modified.......: litlmike, water, GMK, willichan
- ; ===============================================================================================================================
- Func _Excel_BookOpen($oExcel, $sFilePath, $bReadOnly = Default, $bVisible = Default, $sPassword = Default, $sWritePassword = Default, $bUpdateLinks = Default)
- ; Error handler, automatic cleanup at end of function
- Local $oError = ObjEvent("AutoIt.Error", "__Excel_COMErrFunc")
- #forceref $oError
- If Not IsObj($oExcel) Or ObjName($oExcel, 1) <> "_Application" Then Return SetError(1, @error, 0)
- If StringLeft($sFilePath, "HTTP") = 0 And Not FileExists($sFilePath) Then Return SetError(2, 0, 0)
- If $bReadOnly = Default Then $bReadOnly = False
- If $bVisible = Default Then $bVisible = True
- Local $oWorkbook = $oExcel.Workbooks.Open($sFilePath, $bUpdateLinks, $bReadOnly, Default, $sPassword, $sWritePassword)
- If @error Then Return SetError(3, @error, 0)
- Local $oWindow = $oExcel.Windows($oWorkbook.Name)
- If IsObj($oWindow) Then $oWindow.Visible = $bVisible
- ; If a read-write workbook was opened read-only then set @extended = 1
- If $bReadOnly = False And $oWorkbook.Readonly = True Then Return SetError(0, 1, $oWorkbook)
- Return $oWorkbook
- EndFunc ;==>_Excel_BookOpen
- ; #FUNCTION# ====================================================================================================================
- ; Author ........: water
- ; Modified.......:
- ; ===============================================================================================================================
- Func _Excel_BookOpenText($oExcel, $sFilePath, $iStartRow = Default, $iDataType = Default, $sTextQualifier = Default, $bConsecutiveDelimiter = Default, $sDelimiter = Default, $aFieldInfo = Default, $sDecimalSeparator = Default, $sThousandsSeparator = Default, $bTrailingMinusNumbers = Default, $iOrigin = Default)
- ; Error handler, automatic cleanup at end of function
- Local $oError = ObjEvent("AutoIt.Error", "__Excel_COMErrFunc")
- #forceref $oError
- Local $bTab = False, $bSemicolon = False, $bComma = False, $bSpace = False, $aDelimiter[1], $bOther = False, $sOtherChar
- If Not IsObj($oExcel) Or ObjName($oExcel, 1) <> "_Application" Then Return SetError(1, @error, 0)
- If StringLeft($sFilePath, "HTTP") = 0 And Not FileExists($sFilePath) Then Return SetError(2, 0, 0)
- If $iStartRow = Default Then $iStartRow = 1
- If $sTextQualifier = Default Then $sTextQualifier = $xlTextQualifierDoubleQuote
- If $bConsecutiveDelimiter = Default Then $bConsecutiveDelimiter = False
- If $sDelimiter = Default Then $sDelimiter = ","
- If $bTrailingMinusNumbers = Default Then $bTrailingMinusNumbers = True
- If StringInStr($sDelimiter, @TAB) > 0 Then $bTab = True
- If StringInStr($sDelimiter, ";") > 0 Then $bSemicolon = True
- If StringInStr($sDelimiter, ",") > 0 Then $bComma = True
- If StringInStr($sDelimiter, " ") > 0 Then $bSpace = True
- $aDelimiter = StringRegExp($sDelimiter, "[^;, " & @TAB & "]", $STR_REGEXPARRAYMATCH)
- If Not @error Then
- $sOtherChar = $aDelimiter[0]
- $bOther = True
- EndIf
- $oExcel.Workbooks.OpenText($sFilePath, $iOrigin, $iStartRow, $iDataType, $sTextQualifier, $bConsecutiveDelimiter, _
- $bTab, $bSemicolon, $bComma, $bSpace, $bOther, $sOtherChar, $aFieldInfo, Default, $sDecimalSeparator, $sThousandsSeparator, _
- $bTrailingMinusNumbers, False)
- If @error Then Return SetError(3, @error, 0)
- Return $oExcel.ActiveWorkbook ; Method OpenText doesn't return the Workbook object
- EndFunc ;==>_Excel_BookOpenText
- ; #FUNCTION# ====================================================================================================================
- ; Author ........: SEO <locodarwin at yahoo dot com>
- ; Modified.......: litlmike, water
- ; ===============================================================================================================================
- Func _Excel_BookSave($oWorkbook)
- ; Error handler, automatic cleanup at end of function
- Local $oError = ObjEvent("AutoIt.Error", "__Excel_COMErrFunc")
- #forceref $oError
- If Not IsObj($oWorkbook) Or ObjName($oWorkbook, 1) <> "_Workbook" Then Return SetError(1, 0, 0)
- If Not $oWorkbook.Saved Then
- $oWorkbook.Save()
- If @error Then Return SetError(2, @error, 0)
- Return SetError(0, 1, 1)
- EndIf
- Return 1
- EndFunc ;==>_Excel_BookSave
- ; #FUNCTION# ====================================================================================================================
- ; Author ........: SEO <locodarwin at yahoo dot com>
- ; Modified.......: litlmike, water
- ; ===============================================================================================================================
- Func _Excel_BookSaveAs($oWorkbook, $sFilePath, $iFormat = Default, $bOverWrite = Default, $sPassword = Default, $sWritePassword = Default, $bReadOnlyRecommended = Default)
- ; Error handler, automatic cleanup at end of function
- Local $oError = ObjEvent("AutoIt.Error", "__Excel_COMErrFunc")
- #forceref $oError
- If Not IsObj($oWorkbook) Or ObjName($oWorkbook, 1) <> "_Workbook" Then Return SetError(1, 0, 0)
- If $iFormat = Default Then
- $iFormat = $xlWorkbookDefault
- Else
- If Not IsNumber($iFormat) Then Return SetError(2, 0, 0)
- EndIf
- If $bOverWrite = Default Then $bOverWrite = False
- If $bReadOnlyRecommended = Default Then $bReadOnlyRecommended = False
- If FileExists($sFilePath) Then
- If Not $bOverWrite Then Return SetError(3, 0, 0)
- Local $iResult = FileDelete($sFilePath)
- If $iResult = 0 Then Return SetError(4, 0, 0)
- EndIf
- $oWorkbook.SaveAs($sFilePath, $iFormat, $sPassword, $sWritePassword, $bReadOnlyRecommended)
- If @error Then Return SetError(5, @error, 0)
- Return 1
- EndFunc ;==>_Excel_BookSaveAs
- ; #FUNCTION# ====================================================================================================================
- ; Name ..........: _Excel_ColumnToLetter
- ; Description ...: Converts the column number to letter(s).
- ; Syntax ........: _ExcelColumnToLetter($iColumn)
- ; Parameters ....: $iColumn - The column number which you want to turn into letter(s)
- ; Return values .: Success - Returns the column letter(s)
- ; Failure - Returns "" and sets @Error:
- ; Author(s): Spiff59
- ; Modified ......:
- ; ===============================================================================================================================
- Func _Excel_ColumnToLetter($iColumn)
- If Not StringRegExp($iColumn, "^[0-9]+$") Then Return SetError(1, 0, "")
- Local $sLetters, $iTemp
- While $iColumn
- $iTemp = Mod($iColumn, 26)
- If $iTemp = 0 Then $iTemp = 26
- $sLetters = Chr($iTemp + 64) & $sLetters
- $iColumn = ($iColumn - $iTemp) / 26
- WEnd
- Return $sLetters
- EndFunc ;==>_Excel_ColumnToLetter
- ; #FUNCTION# ====================================================================================================================
- ; Author ........: Golfinhu
- ; Modified ......:
- ; ===============================================================================================================================
- Func _Excel_ColumnToNumber($sColumn)
- $sColumn = StringUpper($sColumn)
- If Not StringRegExp($sColumn, "^[A-Z]+$") Then Return SetError(1, 0, 0)
- Local $sLetters = StringSplit($sColumn, "")
- Local $iNumber = 0
- Local $iLen = StringLen($sColumn)
- For $i = 1 To $sLetters[0]
- $iNumber += 26 ^ ($iLen - $i) * (Asc($sLetters[$i]) - 64)
- Next
- Return $iNumber
- EndFunc ;==>_Excel_ColumnToNumber
- ; #FUNCTION# ====================================================================================================================
- ; Author ........: water
- ; Modified ......:
- ; ===============================================================================================================================
- Func _Excel_ConvertFormula($oExcel, $sFormula, $iFromStyle, $iToStyle = Default, $iToAbsolute = Default, $vRelativeTo = Default)
- ; Error handler, automatic cleanup at end of function
- Local $oError = ObjEvent("AutoIt.Error", "__Excel_COMErrFunc")
- #forceref $oError
- If Not IsObj($oExcel) Or ObjName($oExcel, 1) <> "_Application" Then Return SetError(1, 0, "")
- If $vRelativeTo <> Default Then
- If Not IsObj($vRelativeTo) Then $vRelativeTo = $oExcel.Range($vRelativeTo)
- If @error Or Not IsObj($vRelativeTo) Then Return SetError(2, 0, "")
- EndIf
- Local $sConverted = $oExcel.ConvertFormula($sFormula, $iFromStyle, $iToStyle, $iToAbsolute, $vRelativeTo)
- Return $sConverted
- EndFunc ;==>_Excel_ConvertFormula
- ; #FUNCTION# ====================================================================================================
- ; Author ........: water
- ; Modified ......:
- ; ===============================================================================================================
- Func _Excel_Export($oExcel, $vObject, $sFileName, $iType = Default, $iQuality = Default, $bIncludeProperties = Default, $iFrom = Default, $iTo = Default, $bOpenAfterPublish = Default)
- ; Error handler, automatic cleanup at end of function
- Local $oError = ObjEvent("AutoIt.Error", "__Excel_COMErrFunc")
- #forceref $oError
- If Not IsObj($oExcel) Or ObjName($oExcel, 1) <> "_Application" Then Return SetError(1, 0, 0)
- If Not IsObj($vObject) Then $vObject = $oExcel.Range($vObject)
- If @error Or Not IsObj($vObject) Then Return SetError(2, @error, 0)
- If $sFileName = "" Then Return SetError(3, 0, 0)
- If $iType = Default Then $iType = $xlTypePDF
- If $iQuality = Default Then $iQuality = $xlQualityStandard
- If $bIncludeProperties = Default Then $bIncludeProperties = True
- If $bOpenAfterPublish = Default Then $bOpenAfterPublish = False
- $vObject.ExportAsFixedFormat($iType, $sFileName, $iQuality, $bIncludeProperties, Default, $iFrom, $iTo, $bOpenAfterPublish)
- If @error Then Return SetError(4, @error, 0)
- Return $vObject
- EndFunc ;==>_Excel_Export
- ; #FUNCTION# ====================================================================================================================
- ; Author ........: water
- ; Modified.......:
- ; ===============================================================================================================================
- Func _Excel_FilterGet($oWorkbook, $vWorksheet = Default)
- ; Error handler, automatic cleanup at end of function
- Local $oError = ObjEvent("AutoIt.Error", "__Excel_COMErrFunc")
- #forceref $oError
- If Not IsObj($oWorkbook) Or ObjName($oWorkbook, 1) <> "_Workbook" Then Return SetError(1, 0, 0)
- If Not IsObj($vWorksheet) Then
- If $vWorksheet = Default Then
- $vWorksheet = $oWorkbook.ActiveSheet
- Else
- $vWorksheet = $oWorkbook.WorkSheets.Item($vWorksheet)
- EndIf
- If @error Or Not IsObj($vWorksheet) Then Return SetError(2, @error, 0)
- ElseIf ObjName($vWorksheet, 1) <> "_Worksheet" Then
- Return SetError(2, @error, 0)
- EndIf
- Local $iIndex = 0, $iRecords, $iItems = $vWorksheet.AutoFilter.Filters.Count
- If $iItems > 0 Then
- Local $aFilters[$iItems][7]
- For $oFilter In $vWorksheet.AutoFilter.Filters
- $aFilters[$iIndex][0] = $oFilter.On
- $aFilters[$iIndex][1] = $oFilter.Count
- $aFilters[$iIndex][2] = $oFilter.Criteria1
- If IsArray($oFilter.Criteria1) Then $aFilters[$iIndex][2] = _ArrayToString($aFilters[$iIndex][2])
- $aFilters[$iIndex][3] = $oFilter.Criteria2
- If IsArray($oFilter.Criteria2) Then $aFilters[$iIndex][3] = _ArrayToString($aFilters[$iIndex][3])
- $aFilters[$iIndex][4] = $oFilter.Operator
- $aFilters[$iIndex][5] = $oFilter.Parent.Range
- $iRecords = 0
- For $oArea In $oFilter.Parent.Range.SpecialCells($xlCellTypeVisible).Areas
- $iRecords = $iRecords + $oArea.Rows.Count
- Next
- $aFilters[$iIndex][6] = $iRecords
- $iIndex = $iIndex + 1
- Next
- Return $aFilters
- Else
- Return SetError(3, 0, "")
- EndIf
- EndFunc ;==>_Excel_FilterGet
- ; #FUNCTION# ====================================================================================================================
- ; Author ........: water
- ; Modified.......:
- ; ===============================================================================================================================
- Func _Excel_FilterSet($oWorkbook, $vWorksheet, $vRange, $iField, $sCriteria1 = Default, $iOperator = Default, $sCriteria2 = Default)
- ; Error handler, automatic cleanup at end of function
- Local $oError = ObjEvent("AutoIt.Error", "__Excel_COMErrFunc")
- #forceref $oError
- If Not IsObj($oWorkbook) Or ObjName($oWorkbook, 1) <> "_Workbook" Then Return SetError(1, 0, 0)
- If Not IsObj($vWorksheet) Then
- If $vWorksheet = Default Then
- $vWorksheet = $oWorkbook.ActiveSheet
- Else
- $vWorksheet = $oWorkbook.WorkSheets.Item($vWorksheet)
- EndIf
- If @error Or Not IsObj($vWorksheet) Then Return SetError(2, @error, 0)
- ElseIf ObjName($vWorksheet, 1) <> "_Worksheet" Then
- Return SetError(2, @error, 0)
- EndIf
- If $vRange = Default Then
- $vRange = $vWorksheet.Usedrange
- ElseIf Not IsObj($vRange) Then
- $vRange = $vWorksheet.Range($vRange)
- If @error Or Not IsObj($vRange) Then Return SetError(3, @error, 0)
- EndIf
- If $iField <> 0 Then ; Set a new filter
- $vRange.AutoFilter($iField, $sCriteria1, $iOperator, $sCriteria2)
- If @error Then Return SetError(4, @error, 0)
- ; If no filters remain then AutoFiltermode is set off
- If $vWorksheet.Filtermode = False Then $vWorksheet.AutoFilterMode = False
- Else ; remove all filters
- $vWorksheet.AutoFilterMode = False
- EndIf
- Return 1
- EndFunc ;==>_Excel_FilterSet
- ; #FUNCTION# ====================================================================================================================
- ; Author ........: DanWilli
- ; Modified.......: water
- ; ===============================================================================================================================
- Func _Excel_PictureAdd($oWorkbook, $vWorksheet, $sFile, $vRangeOrLeft, $iTop = Default, $iWidth = Default, $iHeight = Default, $bKeepRatio = True)
- ; Error handler, automatic cleanup at end of function
- Local $oError = ObjEvent("AutoIt.Error", "__Excel_COMErrFunc")
- #forceref $oError
- Local $oReturn, $iPosLeft, $iPosTop
- If Not IsObj($oWorkbook) Or ObjName($oWorkbook, 1) <> "_Workbook" Then Return SetError(1, 0, 0)
- If Not FileExists($sFile) Then Return SetError(5, 0, 0)
- If Not IsObj($vWorksheet) Then
- If $vWorksheet = Default Then
- $vWorksheet = $oWorkbook.ActiveSheet
- Else
- $vWorksheet = $oWorkbook.WorkSheets.Item($vWorksheet)
- EndIf
- If @error Or Not IsObj($vWorksheet) Then Return SetError(2, @error, 0)
- ElseIf ObjName($vWorksheet, 1) <> "_Worksheet" Then
- Return SetError(2, @error, 0)
- EndIf
- If IsNumber($vRangeOrLeft) Then
- $iPosLeft = $vRangeOrLeft
- $iPosTop = $iTop
- Else
- If Not IsObj($vRangeOrLeft) Then
- $vRangeOrLeft = $vWorksheet.Range($vRangeOrLeft)
- If @error Or Not IsObj($vRangeOrLeft) Then Return SetError(3, @error, 0)
- EndIf
- $iPosLeft = $vRangeOrLeft.Left
- $iPosTop = $vRangeOrLeft.Top
- EndIf
- If IsNumber($vRangeOrLeft) Or ($vRangeOrLeft.Columns.Count = 1 And $vRangeOrLeft.Rows.Count = 1) Then
- If $iWidth = Default And $iHeight = Default Then
- $oReturn = $vWorksheet.Shapes.AddPicture($sFile, -1, -1, $iPosLeft, $iPosTop, 0, 0)
- If @error Then Return SetError(4, @error, 0)
- $oReturn.Scalewidth(1, -1, 0)
- $oReturn.Scaleheight(1, -1, 0)
- ElseIf $iWidth = Default Then
- $oReturn = $vWorksheet.Shapes.AddPicture($sFile, -1, -1, $iPosLeft, $iPosTop, 0, 0)
- If @error Then Return SetError(4, @error, 0)
- $oReturn.Visible = 0
- $oReturn.Scalewidth(1, -1, 0)
- $oReturn.Scaleheight(1, -1, 0)
- $oReturn.Scalewidth($iHeight / $oReturn.Height, -1, 0)
- $oReturn.Scaleheight($iHeight / $oReturn.Height, -1, 0)
- $oReturn.Visible = 1
- ElseIf $iHeight = Default Then
- $oReturn = $vWorksheet.Shapes.AddPicture($sFile, -1, -1, $iPosLeft, $iPosTop, 0, 0)
- If @error Then Return SetError(4, @error, 0)
- $oReturn.Visible = 0
- $oReturn.Scalewidth(1, -1, 0)
- $oReturn.Scaleheight(1, -1, 0)
- $oReturn.Scaleheight($iWidth / $oReturn.Width, -1, 0)
- $oReturn.Scalewidth($iWidth / $oReturn.Width, -1, 0)
- $oReturn.Visible = 1
- Else
- $oReturn = $vWorksheet.Shapes.AddPicture($sFile, -1, -1, $iPosLeft, $iPosTop, $iWidth, $iHeight)
- If @error Then Return SetError(4, @error, 0)
- EndIf
- Else
- If $bKeepRatio = True Then
- $oReturn = $vWorksheet.Shapes.AddPicture($sFile, -1, -1, $iPosLeft, $iPosTop, 0, 0)
- If @error Then Return SetError(4, @error, 0)
- $oReturn.Visible = 0
- $oReturn.Scalewidth(1, -1, 0)
- $oReturn.Scaleheight(1, -1, 0)
- Local $iRw = $vRangeOrLeft.Width / $oReturn.Width
- Local $iRh = $vRangeOrLeft.Height / $oReturn.Height
- If $iRw < $iRh Then
- $oReturn.Scaleheight($iRw, -1, 0)
- $oReturn.Scalewidth($iRw, -1, 0)
- Else
- $oReturn.Scaleheight($iRh, -1, 0)
- $oReturn.Scalewidth($iRh, -1, 0)
- EndIf
- $oReturn.Visible = 1
- Else
- $oReturn = $vWorksheet.Shapes.AddPicture($sFile, -1, -1, $iPosLeft, $iPosTop, $vRangeOrLeft.Width, $vRangeOrLeft.Height)
- If @error Then Return SetError(4, @error, 0)
- EndIf
- EndIf
- Return $oReturn
- EndFunc ;==>_Excel_PictureAdd
- ; #FUNCTION# ====================================================================================================
- ; Author ........: water
- ; Modified ......:
- ; ===============================================================================================================
- Func _Excel_Print($oExcel, $vObject, $iCopies = Default, $sPrinter = Default, $bPreview = Default, $iFrom = Default, $iTo = Default, $bPrintToFile = Default, $bCollate = Default, $sPrToFileName = "")
- ; Error handler, automatic cleanup at end of function
- Local $oError = ObjEvent("AutoIt.Error", "__Excel_COMErrFunc")
- #forceref $oError
- If Not IsObj($oExcel) Or ObjName($oExcel, 1) <> "_Application" Then Return SetError(1, 0, 0)
- If IsString($vObject) Then $vObject = $oExcel.Range($vObject)
- If @error Or Not IsObj($vObject) Then Return SetError(2, @error, 0)
- $vObject.PrintOut($iFrom, $iTo, $iCopies, $bPreview, $sPrinter, $bPrintToFile, $bCollate, $sPrToFileName)
- If @error Then Return SetError(3, @error, 0)
- Return $vObject
- EndFunc ;==>_Excel_Print
- ; #FUNCTION# ====================================================================================================================
- ; Author ........: water
- ; Modified.......:
- ; ===============================================================================================================================
- Func _Excel_RangeCopyPaste($oWorksheet, $vSourceRange, $vTargetRange = Default, $bCut = Default, $iPaste = Default, $iOperation = Default, $bSkipBlanks = Default, $bTranspose = Default)
- ; Error handler, automatic cleanup at end of function
- Local $oError = ObjEvent("AutoIt.Error", "__Excel_COMErrFunc")
- #forceref $oError
- If Not IsObj($oWorksheet) Or ObjName($oWorksheet, 1) <> "_Worksheet" Then Return SetError(1, 0, 0)
- If $bCut = Default Then $bCut = False
- If $vSourceRange = Default And $vTargetRange = Default Then Return SetError(7, 0, 0)
- If Not IsObj($vSourceRange) And $vSourceRange <> Default Then
- $vSourceRange = $oWorksheet.Range($vSourceRange)
- If @error Then Return SetError(2, @error, 0)
- EndIf
- If Not IsObj($vTargetRange) And $vTargetRange <> Default Then
- $vTargetRange = $oWorksheet.Range($vTargetRange)
- If @error Then Return SetError(3, @error, 0)
- EndIf
- If $vSourceRange = Default Then ; Paste from the clipboard
- If $bSkipBlanks = Default Then $bSkipBlanks = False
- If $bTranspose = Default Then $bTranspose = False
- $vTargetRange.PasteSpecial($iPaste, $iOperation, $bSkipBlanks, $bTranspose)
- If @error Then Return SetError(4, @error, 0)
- Else
- If $bCut Then
- $vSourceRange.Cut($vTargetRange)
- If @error Then Return SetError(5, @error, 0)
- Else
- $vSourceRange.Copy($vTargetRange)
- If @error Then Return SetError(6, @error, 0)
- EndIf
- EndIf
- If $vTargetRange <> Default Then
- Return $vTargetRange
- Else
- Return 1
- EndIf
- EndFunc ;==>_Excel_RangeCopyPaste
- ; #FUNCTION# ====================================================================================================================
- ; Author ........: water
- ; Modified.......:
- ; ===============================================================================================================================
- Func _Excel_RangeDelete($oWorksheet, $vRange, $iShift = Default, $iEntireRowCol = Default)
- ; Error handler, automatic cleanup at end of function
- Local $oError = ObjEvent("AutoIt.Error", "__Excel_COMErrFunc")
- #forceref $oError
- If Not IsObj($oWorksheet) Or ObjName($oWorksheet, 1) <> "_Worksheet" Then Return SetError(1, 0, 0)
- If Not IsObj($vRange) Then
- $vRange = $oWorksheet.Range($vRange)
- If @error Then Return SetError(2, @error, 0)
- EndIf
- If $iEntireRowCol = 1 Then
- $vRange.EntireRow.Delete($iShift)
- ElseIf $iEntireRowCol = 2 Then
- $vRange.EntireColumn.Delete($iShift)
- Else
- $vRange.Delete($iShift)
- EndIf
- If @error Then Return SetError(3, @error, 0)
- Return 1
- EndFunc ;==>_Excel_RangeDelete
- ; #FUNCTION# ====================================================================================================================
- ; Author ........: water
- ; Modified.......:
- ; ===============================================================================================================================
- Func _Excel_RangeFind($oWorkbook, $sSearch, $vRange = Default, $iLookIn = Default, $iLookAt = Default, $bMatchcase = Default)
- ; Error handler, automatic cleanup at end of function
- Local $oError = ObjEvent("AutoIt.Error", "__Excel_COMErrFunc")
- #forceref $oError
- If Not IsObj($oWorkbook) Or ObjName($oWorkbook, 1) <> "_Workbook" Then Return SetError(1, 0, 0)
- If StringStripWS($sSearch, BitOR($STR_STRIPLEADING, $STR_STRIPTRAILING)) = "" Then Return SetError(2, 0, 0)
- If $iLookIn = Default Then $iLookIn = $xlValues
- If $iLookAt = Default Then $iLookAt = $xlPart
- If $bMatchcase = Default Then $bMatchcase = False
- Local $oMatch, $sFirst = "", $bSearchWorkbook = False, $oSheet
- If $vRange = Default Then
- $bSearchWorkbook = True
- $oSheet = $oWorkbook.Sheets(1)
- $vRange = $oSheet.UsedRange
- ElseIf IsString($vRange) Then
- $vRange = $oWorkbook.Activesheet.Range($vRange)
- If @error Then Return SetError(3, @error, 0)
- EndIf
- Local $aResult[100][6], $iIndex = 0, $iIndexSheets = 1, $oTemp
- While 1
- $oMatch = $vRange.Find($sSearch, Default, $iLookIn, $iLookAt, Default, Default, $bMatchcase)
- If @error Then Return SetError(4, @error, 0)
- If IsObj($oMatch) Then
- $sFirst = $oMatch.Address
- While 1
- $aResult[$iIndex][0] = $oMatch.Worksheet.Name
- $oTemp = $oMatch.Name
- If Not @error Then $aResult[$iIndex][1] = $oTemp.Name
- $aResult[$iIndex][2] = $oMatch.Address
- $aResult[$iIndex][3] = $oMatch.Value
- $aResult[$iIndex][4] = $oMatch.Formula
- $oTemp = $oMatch.Comment
- If IsObj($oTemp) Then $aResult[$iIndex][5] = $oTemp.Text
- $iIndex = $iIndex + 1
- If Mod($iIndex, 100) = 0 Then ReDim $aResult[UBound($aResult, 1) + 100][6]
- $oMatch = $vRange.Findnext($oMatch)
- If Not IsObj($oMatch) Or $sFirst = $oMatch.Address Then ExitLoop
- WEnd
- EndIf
- If Not $bSearchWorkbook Then ExitLoop
- $iIndexSheets = $iIndexSheets + 1
- $sFirst = ""
- $oSheet = $oWorkbook.Sheets($iIndexSheets)
- If @error Then ExitLoop
- $vRange = $oSheet.UsedRange
- WEnd
- ReDim $aResult[$iIndex][6]
- Return $aResult
- EndFunc ;==>_Excel_RangeFind
- ; #FUNCTION# ====================================================================================================================
- ; Author ........: water
- ; Modified.......:
- ; ===============================================================================================================================
- Func _Excel_RangeInsert($oWorksheet, $vRange, $iShift = Default, $iCopyOrigin = Default)
- ; Error handler, automatic cleanup at end of function
- Local $oError = ObjEvent("AutoIt.Error", "__Excel_COMErrFunc")
- #forceref $oError
- If Not IsObj($oWorksheet) Or ObjName($oWorksheet, 1) <> "_Worksheet" Then Return SetError(1, 0, 0)
- If Not IsObj($vRange) Then
- $vRange = $oWorksheet.Range($vRange)
- If @error Then Return SetError(2, @error, 0)
- EndIf
- $vRange.Insert($iShift, $iCopyOrigin)
- If @error Then Return SetError(3, @error, 0)
- Return $vRange
- EndFunc ;==>_Excel_RangeInsert
- ; #FUNCTION# ====================================================================================================================
- ; Author ........: water
- ; Modified ......: Added parameter $sTextToDisplay
- ; ===============================================================================================================================
- Func _Excel_RangeLinkAddRemove($oWorkbook, $vWorksheet, $vRange, $sAddress, $sSubAddress = Default, $sScreenTip = Default, $sTextToDisplay = Default)
- ; Error handler, automatic cleanup at end of function
- Local $oError = ObjEvent("AutoIt.Error", "__Excel_COMErrFunc")
- #forceref $oError
- Local $oLink
- If Not IsObj($oWorkbook) Or ObjName($oWorkbook, 1) <> "_Workbook" Then Return SetError(1, 0, 0)
- If Not IsObj($vWorksheet) Then
- If $vWorksheet = Default Then
- $vWorksheet = $oWorkbook.ActiveSheet
- Else
- $vWorksheet = $oWorkbook.WorkSheets.Item($vWorksheet)
- EndIf
- If @error Or Not IsObj($vWorksheet) Then Return SetError(2, @error, 0)
- ElseIf ObjName($vWorksheet, 1) <> "_Worksheet" Then
- Return SetError(2, @error, 0)
- EndIf
- If Not IsObj($vRange) Then
- $vRange = $vWorksheet.Range($vRange)
- If @error Or Not IsObj($vRange) Then Return SetError(3, @error, 0)
- EndIf
- If $sAddress = "" Then
- $vRange.Hyperlinks.Delete()
- If @error Then Return SetError(4, @error, 0)
- Return 1
- Else
- $oLink = $vWorksheet.Hyperlinks.Add($vRange, $sAddress, $sSubAddress, $sScreenTip, $sTextToDisplay)
- If @error Then Return SetError(4, @error, 0)
- Return $oLink
- EndIf
- EndFunc ;==>_Excel_RangeLinkAddRemove
- ; #FUNCTION# ====================================================================================================================
- ; Author ........: SEO <locodarwin at yahoo dot com>
- ; Modified.......: litlmike, water, GMK
- ; ===============================================================================================================================
- Func _Excel_RangeRead($oWorkbook, $vWorksheet = Default, $vRange = Default, $iReturn = Default, $bForceFunc = Default)
- ; Error handler, automatic cleanup at end of function
- Local $oError = ObjEvent("AutoIt.Error", "__Excel_COMErrFunc")
- #forceref $oError
- If Not IsObj($oWorkbook) Or ObjName($oWorkbook, 1) <> "_Workbook" Then Return SetError(1, 0, 0)
- If Not IsObj($vWorksheet) Then
- If $vWorksheet = Default Then
- $vWorksheet = $oWorkbook.ActiveSheet
- Else
- $vWorksheet = $oWorkbook.WorkSheets.Item($vWorksheet)
- EndIf
- If @error Or Not IsObj($vWorksheet) Then Return SetError(2, @error, 0)
- ElseIf ObjName($vWorksheet, 1) <> "_Worksheet" Then
- Return SetError(2, @error, 0)
- EndIf
- If $vRange = Default Then
- $vRange = $vWorksheet.Usedrange
- ElseIf Not IsObj($vRange) Then
- $vRange = $vWorksheet.Range($vRange)
- If @error Or Not IsObj($vRange) Then Return SetError(3, @error, 0)
- EndIf
- If $iReturn = Default Then
- $iReturn = 1
- ElseIf $iReturn < 1 Or $iReturn > 4 Then
- Return SetError(4, 0, 0)
- EndIf
- If $bForceFunc = Default Then $bForceFunc = False
- Local $vResult, $iCellCount = $vRange.Columns.Count * $vRange.Rows.Count
- If $iReturn = 3 And $iCellCount > 1 Then Return SetError(8, @error, 0)
- ; The max number of elements in an AutoIt array is limited to 2^24 = 16,777,216
- If $iCellCount > 16777216 Then Return SetError(6, 0, 0)
- ; Transpose has an undocumented limit on the number of cells or rows it can transpose. This limit increases with the Excel version
- ; Limits:
- ; Excel 97 - 5461 cells
- ; Excel 2000 - 5461 cells
- ; Excel 2003 - ?
- ; Excel 2007 - 65535 cells
- ; Excel 2010 - ?
- ; Excel 2013 - ?
- If $iCellCount > 65535 Then $bForceFunc = True
- If $bForceFunc Then
- Switch $iReturn
- Case 1
- $vResult = $vRange.Value
- Case 2
- $vResult = $vRange.Formula
- Case 3
- $vResult = $vRange.Text
- Case Else
- $vResult = $vRange.Value2
- EndSwitch
- If @error Then Return SetError(7, @error, 0)
- If $iCellCount > 1 Then _ArrayTranspose($vResult)
- Else
- Local $oExcel = $oWorkbook.Parent
- Switch $iReturn
- Case 1
- $vResult = $oExcel.Transpose($vRange.Value)
- Case 2
- $vResult = $oExcel.Transpose($vRange.Formula)
- Case 3
- $vResult = $oExcel.Transpose($vRange.Text)
- Case Else
- $vResult = $oExcel.Transpose($vRange.Value2)
- EndSwitch
- If @error Then Return SetError(5, @error, 0)
- EndIf
- Return $vResult
- EndFunc ;==>_Excel_RangeRead
- ; #FUNCTION# ====================================================================================================================
- ; Author ........: water
- ; Modified.......:
- ; ===============================================================================================================================
- Func _Excel_RangeReplace($oWorkbook, $vWorksheet, $vRange, $sSearch, $sReplace, $iLookAt = Default, $bMatchcase = Default)
- ; Error handler, automatic cleanup at end of function
- Local $oError = ObjEvent("AutoIt.Error", "__Excel_COMErrFunc")
- #forceref $oError
- If Not IsObj($oWorkbook) Or ObjName($oWorkbook, 1) <> "_Workbook" Then Return SetError(1, 0, 0)
- If Not IsObj($vWorksheet) Then
- If $vWorksheet = Default Then
- $vWorksheet = $oWorkbook.ActiveSheet
- Else
- $vWorksheet = $oWorkbook.WorkSheets.Item($vWorksheet)
- EndIf
- If @error Or Not IsObj($vWorksheet) Then Return SetError(2, @error, 0)
- ElseIf ObjName($vWorksheet, 1) <> "_Worksheet" Then
- Return SetError(2, @error, 0)
- EndIf
- If StringStripWS($sSearch, BitOR($STR_STRIPLEADING, $STR_STRIPTRAILING)) = "" Then Return SetError(3, 0, 0)
- If $vRange = Default Then
- $vRange = $vWorksheet.Usedrange
- ElseIf Not IsObj($vRange) Then
- $vRange = $vWorksheet.Range($vRange)
- If @error Or Not IsObj($vRange) Then Return SetError(4, @error, 0)
- EndIf
- If $iLookAt = Default Then $iLookAt = $xlPart
- If $bMatchcase = Default Then $bMatchcase = False
- Local $bReplace
- $bReplace = $vRange.Replace($sSearch, $sReplace, $iLookAt, Default, $bMatchcase)
- If @error Then Return SetError(5, @error, 0)
- Return SetError(0, $bReplace, $vRange)
- EndFunc ;==>_Excel_RangeReplace
- ; #FUNCTION# ====================================================================================================================
- ; Author ........: water
- ; Modified.......:
- ; ===============================================================================================================================
- Func _Excel_RangeSort($oWorkbook, $vWorksheet, $vRange, $vKey1, $iOrder1 = Default, $iSortText = Default, $iHeader = Default, _
- $bMatchcase = Default, $iOrientation = Default, $vKey2 = Default, $iOrder2 = Default, $vKey3 = Default, $iOrder3 = Default)
- ; Error handler, automatic cleanup at end of function
- Local $oError = ObjEvent("AutoIt.Error", "__Excel_COMErrFunc")
- #forceref $oError
- If Not IsObj($oWorkbook) Or ObjName($oWorkbook, 1) <> "_Workbook" Then Return SetError(1, 0, 0)
- If Not IsObj($vWorksheet) Then
- If $vWorksheet = Default Then
- $vWorksheet = $oWorkbook.ActiveSheet
- Else
- $vWorksheet = $oWorkbook.WorkSheets.Item($vWorksheet)
- EndIf
- If @error Or Not IsObj($vWorksheet) Then Return SetError(2, @error, 0)
- ElseIf ObjName($vWorksheet, 1) <> "_Worksheet" Then
- Return SetError(2, @error, 0)
- EndIf
- If $vRange = Default Then
- $vRange = $vWorksheet.Usedrange
- ElseIf Not IsObj($vRange) Then
- $vRange = $vWorksheet.Range($vRange)
- If @error Or Not IsObj($vRange) Then Return SetError(3, @error, 0)
- EndIf
- $vKey1 = $vWorksheet.Range($vKey1)
- If @error Or Not IsObj($vKey1) Then Return SetError(4, @error, 0)
- If $vKey2 <> Default Then
- $vKey2 = $vWorksheet.Range($vKey2)
- If @error Or Not IsObj($vKey2) Then Return SetError(5, @error, 0)
- EndIf
- If $vKey3 <> Default Then
- $vKey3 = $vWorksheet.Range($vKey3)
- If @error Or Not IsObj($vKey3) Then Return SetError(6, @error, 0)
- EndIf
- If $iHeader = Default Then $iHeader = $xlNo
- If $bMatchcase = Default Then $bMatchcase = False
- If $iOrientation = Default Then $iOrientation = $xlSortColumns
- If $iOrder1 = Default Then $iOrder1 = $xlAscending
- If $iSortText = Default Then $iSortText = $xlSortNormal
- If $iOrder2 = Default Then $iOrder2 = $xlAscending
- If $iOrder3 = Default Then $iOrder3 = $xlAscending
- If Int($oWorkbook.Parent.Version) < 112 Then ; Use Sort method for Excel 2003 and older
- $vRange.Sort($vKey1, $iOrder1, $vKey2, Default, $iOrder2, $vKey3, $iOrder3, $iHeader, Default, $bMatchcase, $iOrientation, Default, $iSortText, $iSortText, $iSortText)
- Else
- ; http://www.autoitscript.com/forum/topic/136672-excel-multiple-column-sort/?hl=%2Bexcel+%2Bsort+%2Bcolumns#entry956163
- ; http://msdn.microsoft.com/en-us/library/ff839572(v=office.14).aspx
- $vWorksheet.Sort.SortFields.Clear
- $vWorksheet.Sort.SortFields.Add($vKey1, $xlSortOnValues, $iOrder1)
- If $vKey2 <> Default Then $vWorksheet.Sort.SortFields.Add($vKey2, $xlSortOnValues, $iOrder2)
- If $vKey3 <> Default Then $vWorksheet.Sort.SortFields.Add($vKey3, $xlSortOnValues, $iOrder3)
- $vWorksheet.Sort.SetRange($vRange)
- $vWorksheet.Sort.Header = $iHeader
- $vWorksheet.Sort.MatchCase = $bMatchcase
- $vWorksheet.Sort.Orientation = $iOrientation
- $vWorksheet.Sort.Apply
- EndIf
- If @error Then Return SetError(7, @error, 0)
- Return $vRange
- EndFunc ;==>_Excel_RangeSort
- ; #FUNCTION# ====================================================================================================================
- ; Author ........: water
- ; Modified.......:
- ; ===============================================================================================================================
- Func _Excel_RangeValidate($oWorkbook, $vWorksheet, $vRange, $iType, $sFormula1, $iOperator = Default, $sFormula2 = Default, $bIgnoreBlank = Default, $iAlertStyle = Default, $sErrorMessage = Default, $sInputMessage = Default)
- ; Error handler, automatic cleanup at end of function
- Local $oError = ObjEvent("AutoIt.Error", "__Excel_COMErrFunc")
- #forceref $oError
- If Not IsObj($oWorkbook) Or ObjName($oWorkbook, 1) <> "_Workbook" Then Return SetError(1, 0, 0)
- If Not IsObj($vWorksheet) Then
- If $vWorksheet = Default Then
- $vWorksheet = $oWorkbook.ActiveSheet
- Else
- $vWorksheet = $oWorkbook.WorkSheets.Item($vWorksheet)
- EndIf
- If @error Or Not IsObj($vWorksheet) Then Return SetError(2, @error, 0)
- ElseIf ObjName($vWorksheet, 1) <> "_Worksheet" Then
- Return SetError(2, @error, 0)
- EndIf
- If $vRange = Default Then
- $vRange = $vWorksheet.Usedrange
- ElseIf Not IsObj($vRange) Then
- $vRange = $vWorksheet.Range($vRange)
- If @error Or Not IsObj($vRange) Then Return SetError(3, @error, 0)
- EndIf
- If $bIgnoreBlank = Default Then $bIgnoreBlank = True
- If $iAlertStyle = Default Then $iAlertStyle = $xlValidAlertStop
- $vRange.Validation.Delete() ; delete existing validation before adding a new one
- $vRange.Validation.Add($iType, $iAlertStyle, $iOperator, $sFormula1, $sFormula2)
- If @error Then Return SetError(4, @error, 0)
- $vRange.Validation.IgnoreBlank = $bIgnoreBlank
- If $sInputMessage <> Default Then
- $vRange.Validation.InputMessage = $sInputMessage
- $vRange.Validation.ShowInput = True
- EndIf
- If $sErrorMessage <> Default Then
- $vRange.Validation.ErrorMessage = $sErrorMessage
- $vRange.Validation.ShowError = True
- EndIf
- Return $vRange
- EndFunc ;==>_Excel_RangeValidate
- ; #FUNCTION# ====================================================================================================================
- ; Author ........: SEO <locodarwin at yahoo dot com>
- ; Modified.......: litlmike, PsaltyDS, Golfinhu, water
- ; ===============================================================================================================================
- Func _Excel_RangeWrite($oWorkbook, $vWorksheet, $vValue, $vRange = Default, $bValue = Default, $bForceFunc = Default)
- ; Error handler, automatic cleanup at end of function
- Local $oError = ObjEvent("AutoIt.Error", "__Excel_COMErrFunc")
- #forceref $oError
- If Not IsObj($oWorkbook) Or ObjName($oWorkbook, 1) <> "_Workbook" Then Return SetError(1, 0, 0)
- If Not IsObj($vWorksheet) Then
- If $vWorksheet = Default Then
- $vWorksheet = $oWorkbook.ActiveSheet
- Else
- $vWorksheet = $oWorkbook.WorkSheets.Item($vWorksheet)
- EndIf
- If @error Or Not IsObj($vWorksheet) Then Return SetError(2, @error, 0)
- ElseIf ObjName($vWorksheet, 1) <> "_Worksheet" Then
- Return SetError(2, @error, 0)
- EndIf
- If $vRange = Default Then $vRange = "A1"
- If $bValue = Default Then $bValue = True
- If $bForceFunc = Default Then $bForceFunc = False
- If Not IsObj($vRange) Then
- $vRange = $vWorksheet.Range($vRange)
- If @error Or Not IsObj($vRange) Then Return SetError(3, @error, 0)
- EndIf
- If Not IsArray($vValue) Then
- If $bValue Then
- $vRange.Value = $vValue
- Else
- $vRange.Formula = $vValue
- EndIf
- If @error Then Return SetError(4, @error, 0)
- Else
- If $vRange.Columns.Count = 1 And $vRange.Rows.Count = 1 Then
- If UBound($vValue, 0) = 1 Then
- $vRange = $vRange.Resize(UBound($vValue, 1), 1)
- Else
- $vRange = $vRange.Resize(UBound($vValue, 1), UBound($vValue, 2))
- EndIf
- EndIf
- ; ==========================
- ; Transpose has an undocument limit on the number of cells or rows it can transpose. This limit increases with the Excel version
- ; Limits:
- ; Excel 97 - 5461 cells
- ; Excel 2000 - 5461 cells
- ; Excel 2003 - ?
- ; Excel 2007 - 65536 rows ?
- ; Excel 2010 - ?
- ; Example: If $oExcel.Version = 14 And $vRange.Columns.Count * $vRange.Rows.Count > 1000000 Then $bForceFunc = True
- If $bForceFunc Then
- _ArrayTranspose($vValue)
- If $bValue Then
- $vRange.Value = $vValue
- Else
- $vRange.Formula = $vValue
- EndIf
- If @error Then Return SetError(5, @error, 0)
- Else
- Local $oExcel = $oWorkbook.Parent
- If $bValue Then
- $vRange.Value = $oExcel.Transpose($vValue)
- Else
- $vRange.Formula = $oExcel.Transpose($vValue)
- EndIf
- If @error Then Return SetError(6, @error, 0)
- EndIf
- EndIf
- Return $vRange
- EndFunc ;==>_Excel_RangeWrite
- ; #FUNCTION# ====================================================================================================================
- ; Author ........: water
- ; Modified.......:
- ; ===============================================================================================================================
- Func _Excel_SheetAdd($oWorkbook, $vSheet = Default, $bBefore = Default, $iCount = Default, $sName = Default)
- ; Error handler, automatic cleanup at end of function
- Local $oError = ObjEvent("AutoIt.Error", "__Excel_COMErrFunc")
- #forceref $oError
- If Not IsObj($oWorkbook) Or ObjName($oWorkbook, 1) <> "_Workbook" Then Return SetError(1, 0, 0)
- Local $bInsertAtEnd = False, $iStartSheet, $oBefore = Default, $oAfter = Default
- If $iCount = Default Then $iCount = 1
- If $iCount > 255 Then Return SetError(6, 0, 0)
- If $bBefore = Default Then $bBefore = True
- If $vSheet = Default Then
- $vSheet = $oWorkbook.ActiveSheet
- ElseIf Not IsObj($vSheet) Then
- If $vSheet = -1 Then
- $vSheet = $oWorkbook.WorkSheets.Item($oWorkbook.WorkSheets.Count)
- Else
- $vSheet = $oWorkbook.WorkSheets.Item($vSheet)
- EndIf
- If @error Then Return SetError(2, @error, 0)
- If $vSheet.Index = $oWorkbook.WorkSheets.Count And $bBefore = False Then $bInsertAtEnd = True
- EndIf
- If $sName <> Default Then
- Local $aName = StringSplit($sName, "|")
- SetError(0) ; Reset @error if the separator was not found
- If $aName[1] <> "" Then ; Name provided
- For $iIndex1 = 1 To $aName[0]
- For $iIndex2 = 1 To $oWorkbook.WorkSheets.Count
- If $oWorkbook.WorkSheets($iIndex2).Name = $aName[$iIndex1] Then Return SetError(3, $iIndex1, 0)
- Next
- Next
- Else
- $sName = Default ; No name provided
- EndIf
- EndIf
- If $bBefore Then
- $oBefore = $vSheet
- Else
- $oAfter = $vSheet
- EndIf
- Local $oSheet = $oWorkbook.WorkSheets.Add($oBefore, $oAfter, $iCount)
- If @error Then Return SetError(4, @error, 0)
- If $sName <> Default Then
- ; If sheets are added after the last sheet then the returned sheet is the rightmost, else it is the leftmost
- If $bInsertAtEnd = True Then
- $iStartSheet = $oSheet.Index - $iCount + 1
- Else
- $iStartSheet = $oSheet.Index
- EndIf
- $iIndex2 = 1
- For $iSheet = $iStartSheet To $iStartSheet + $iCount - 1
- If $aName[$iIndex2] <> "" Then $oWorkbook.WorkSheets($iSheet).Name = $aName[$iIndex2]
- If @error Then Return SetError(5, @error, 0)
- $iIndex2 += 1
- If $iIndex2 > $aName[0] Then ExitLoop
- Next
- EndIf
- Return $oSheet
- EndFunc ;==>_Excel_SheetAdd
- ; #FUNCTION# ====================================================================================================================
- ; Author ........: SEO <locodarwin at yahoo dot com>
- ; Modified.......: litlmike, water
- ; ===============================================================================================================================
- Func _Excel_SheetCopyMove($oSourceBook, $vSourceSheet = Default, $oTargetBook = Default, $vTargetSheet = Default, $bBefore = Default, $bCopy = Default)
- ; Error handler, automatic cleanup at end of function
- Local $oError = ObjEvent("AutoIt.Error", "__Excel_COMErrFunc")
- #forceref $oError
- Local $vBefore = Default, $vAfter = Default
- If Not IsObj($oSourceBook) Or ObjName($oSourceBook, 1) <> "_Workbook" Then Return SetError(1, 0, 0)
- If $vSourceSheet = Default Then $vSourceSheet = $oSourceBook.ActiveSheet
- If $oTargetBook = Default Then $oTargetBook = $oSourceBook
- If Not IsObj($oTargetBook) Or ObjName($oTargetBook, 1) <> "_Workbook" Then Return SetError(2, 0, 0)
- If $vTargetSheet = Default Then $vTargetSheet = 1
- If $bBefore = Default Then $bBefore = True
- If $bCopy = Default Then $bCopy = True
- If Not IsObj($vSourceSheet) Then
- $vSourceSheet = $oSourceBook.Sheets($vSourceSheet)
- If @error Or Not IsObj($vSourceSheet) Then SetError(3, @error, 0)
- EndIf
- If Not IsObj($vTargetSheet) Then
- $vTargetSheet = $oTargetBook.Sheets($vTargetSheet)
- If @error Or Not IsObj($vTargetSheet) Then SetError(4, @error, 0)
- EndIf
- If $bBefore Then
- $vBefore = $vTargetSheet
- Else
- $vAfter = $vTargetSheet
- EndIf
- If $bCopy Then
- $vSourceSheet.Copy($vBefore, $vAfter)
- Else
- $vSourceSheet.Move($vBefore, $vAfter)
- EndIf
- If @error Then Return SetError(5, @error, 0)
- If $bBefore Then
- Return $oTargetBook.Sheets($vTargetSheet.Index - 1)
- Else
- Return $oTargetBook.Sheets($vTargetSheet.Index + 1)
- EndIf
- EndFunc ;==>_Excel_SheetCopyMove
- ; #FUNCTION# ====================================================================================================================
- ; Author ........: water
- ; Modified.......:
- ; ===============================================================================================================================
- Func _Excel_SheetDelete($oWorkbook, $vSheet = Default)
- ; Error handler, automatic cleanup at end of function
- Local $oError = ObjEvent("AutoIt.Error", "__Excel_COMErrFunc")
- #forceref $oError
- If Not IsObj($oWorkbook) Or ObjName($oWorkbook, 1) <> "_Workbook" Then Return SetError(1, 0, 0)
- Local $oSheet
- If $vSheet = Default Then
- $oSheet = $oWorkbook.ActiveSheet
- ElseIf Not IsObj($vSheet) Then
- $oSheet = $oWorkbook.WorkSheets.Item($vSheet)
- Else
- $oSheet = $vSheet
- EndIf
- If @error Then Return SetError(2, @error, 0)
- $oSheet.Delete()
- If @error Then Return SetError(3, @error, 0)
- Return 1
- EndFunc ;==>_Excel_SheetDelete
- ; #FUNCTION# ====================================================================================================================
- ; Author ........: SEO <locodarwin at yahoo dot com>
- ; Modified.......: litlmike, water
- ; ===============================================================================================================================
- Func _Excel_SheetList($oWorkbook)
- ; Error handler, automatic cleanup at end of function
- Local $oError = ObjEvent("AutoIt.Error", "__Excel_COMErrFunc")
- #forceref $oError
- If Not IsObj($oWorkbook) Or ObjName($oWorkbook, 1) <> "_Workbook" Then Return SetError(1, 0, 0)
- Local $iSheetCount = $oWorkbook.Sheets.Count
- Local $aSheets[$iSheetCount][2]
- For $iIndex = 0 To $iSheetCount - 1
- $aSheets[$iIndex][0] = $oWorkbook.Sheets($iIndex + 1).Name
- $aSheets[$iIndex][1] = $oWorkbook.Sheets($iIndex + 1)
- Next
- Return $aSheets
- EndFunc ;==>_Excel_SheetList
- ; #INTERNAL_USE_ONLY#============================================================================================================
- ; Name...........: __Excel_CloseOnQuit
- ; Description ...: Sets or returns the state used to determine if the Excel instance can be closed by _Excel_Close.
- ; Syntax.........: __Excel_CloseOnQuit($oExcel[, $bNewState = Default])
- ; Parameters ....: $oExcel - Object of the Excel instance to be processed
- ; $bNewState - Optional: The following values can be passed:
- ; |True if the Excel instance was started by function _Excel_Open and can be closed by _Excel_Close
- ; |False if the Excel instance was just closed by _Excel_Close and needs to be removed from the table
- ; |Default returns the current state (True, False) for the specified Excel instance
- ; Return values .: Success - Current state. Can be either True (Instance will be closed by _Excel_Close) or False (Instance will not be closed by _Excel_Close)
- ; Author ........: Valik
- ; Modified ......: water
- ; Remarks .......:
- ; Related .......:
- ; Link ..........:
- ; Example .......: No
- ; ===============================================================================================================================
- Func __Excel_CloseOnQuit($oExcel, $bNewState = Default)
- Static $bState[101] = [0]
- If $bNewState = True Then ; Add new Excel instance to the table. Will be closed on _Excel_Close
- For $i = 1 To $bState[0]
- If Not IsObj($bState[$i]) Or $bState[$i] = $oExcel Then ; Empty cell found or instance already stored
- $bState[$i] = $oExcel
- Return True
- EndIf
- Next
- $bState[0] = $bState[0] + 1 ; No empty cell found and instance not already in table. Create a new entry at the end of the table
- $bState[$bState[0]] = $oExcel
- Return True
- Else
- For $i = 1 To $bState[0]
- If $bState[$i] = $oExcel Then ; Excel instance found
- If $bNewState = False Then ; Remove Excel instance from table (set value to zero)
- $bState[$i] = 0
- Return False
- Else
- Return True ; Excel instance found. Will be closed on _Excel_Close
- EndIf
- EndIf
- Next
- EndIf
- Return False ; Excel instance not found. Will not be closed by _Excel_Close
- EndFunc ;==>__Excel_CloseOnQuit
- ; #INTERNAL_USE_ONLY#============================================================================================================
- ; Name...........: __Excel_COMErrFunc
- ; Description ...: Dummy function for silently handling COM errors.
- ; Syntax.........:
- ; Parameters ....:
- ; Return values .:
- ;
- ; Author ........:
- ; Modified ......:
- ; Remarks .......:
- ; Related .......:
- ; Link ..........:
- ; Example .......:
- ; ===============================================================================================================================
- Func __Excel_COMErrFunc()
- ; Do nothing special, just check @error after suspect functions.
- EndFunc ;==>__Excel_COMErrFunc
|