Jump to content
Sign in to follow this  
Bowmore

Fast Array Write To Excel

Recommended Posts

Bowmore

I have some scripts that use Excel to present the result of data validation tests and I was becoming a little frustrated at the length of time it was taking to write the data to excel, using _ExcelWriteSheetFromArray() function in the Excel.au3 UDF. So I thought I'd take a look and see if I could improve on it's performance. This was the result. This new function is 200 to 300 times faster than _ExcelWriteSheetFromArray() for large arrays (20000 plus elements) and in the region of 100 times faster for small arrays.

I have also included the option to write only part of the array to the spreadsheet as sometimes I do not need all of the array.

_ExcelSheetWriteFromArray

; #FUNCTION# ====================================================================================================================
; Name...........: _ExcelSheetWriteFromArray
; Description ...: Writes a 2D array, or part of, to a specified location in the active worksheet
; Syntax.........: _ExcelSheetWriteFromArray($oExcel, ByRef $aArray, $iExcelStartRow = Default, $iExcelStartCol = Default, $iArrayStartRow = Default, $iArrayStartCol = Default, $iArrayEndRow = Default, $iArrayEndCol = Default)
; Parameters ....: $oExcel - Excel object opened by a preceding call to _ExcelBookOpen() or _ExcelBookNew()
;                $aArray - The array ByRef to write data from (array is not modified)
;                $iExcelStartRow - The spreadsheet row to start writing the array to, default is 1
;                $iExcelStartCol - The spreadsheet column to start writing the array to, default is 1
;                $iArrayStartRow - Start row index of array to start writing data from, default is 0
;                $iArrayStartCol - Start column index of array to start writing data from, default is 0
;                $iArrayEndRow - End row index of array to stop writing data from, default is Ubound($aArray)
;                $iArrayEndCol - End column index of array to stop writing data from, default is Ubound($aArray,2)
; Return values .: Success   - Returns 1
;                Failure - Returns 0 and sets @error on errors:
;                |@error=1   - Excel object does not exist
;                |@error=2   - Not a 2D array
;                |@error=3   - End row greater than start row
;                |@error=4   - End col greater than start col
; Author ........: Bowmore
; Modified.......:
; Remarks .......: Only 2D arrays are currently handeled. Maxim cell content length is 255 characters
; Related .......:
; Link ..........:
; Example .......: Yes
; ===============================================================================================================================
Func _ExcelSheetWriteFromArray($oExcel, ByRef $aArray, $iExcelStartRow = Default, $iExcelStartCol = Default, $iArrayStartRow = Default, $iArrayStartCol = Default, $iArrayEndRow = Default, $iArrayEndCol = Default)
Local $iExcelEndRow = 1
Local $iExcelEndCol = 1

If Not IsObj($oExcel) Then Return SetError(1, 0, 0)
If UBound($aArray, 0) <> 2 Then Return SetError(2, 0, 0)
If $iExcelStartRow = Default Then $iExcelStartRow = 1
If $iExcelStartCol = Default Then $iExcelStartCol = 1
If $iArrayStartRow = Default Then $iArrayStartRow = 0
If $iArrayStartCol = Default Then $iArrayStartCol = 0
If $iArrayEndRow = Default Then $iArrayEndRow = UBound($aArray) - 1
If $iArrayEndCol = Default Then $iArrayEndCol = UBound($aArray, 2) - 1
If $iExcelStartRow < 1 Then $iExcelStartRow = 1
If $iExcelStartCol < 1 Then $iExcelStartCol = 1
If $iArrayStartRow < 0 Then $iArrayStartRow = 0
If $iArrayStartCol < 0 Then $iArrayStartCol = 0
If $iArrayEndRow < $iArrayStartRow Then Return SetError(3, 1, 0)
If $iArrayEndCol < $iArrayStartCol Then Return SetError(4, 1, 0)
$iExcelEndRow = $iExcelStartRow + $iArrayEndRow - $iArrayStartRow
$iExcelEndCol = $iExcelStartCol + $iArrayEndCol - $iArrayStartCol

; Check if only part of the array is to written to the speadsheet
If $iArrayStartRow <> 0 Or $iArrayStartCol <> 0 Or $iArrayEndRow <> UBound($aArray) - 1 Or $iArrayEndCol = UBound($aArray, 2) - 1 Then
;Copy specified array range to a temporary array
Local $aTemp[$iArrayEndRow - $iArrayStartRow + 1][$iArrayEndCol - $iArrayStartCol + 1]
Local $iRow = 0
Local $iCol = 0
For $i = $iArrayStartRow To $iArrayEndRow
$iCol = 0
For $j = $iArrayStartCol To $iArrayEndCol
$aTemp[$iRow][$iCol] = $aArray[$i][$j]
$iCol += 1
Next
$iRow += 1
Next
With $oExcel.ActiveSheet
.Range(.Cells($iExcelStartRow, $iExcelStartCol), .Cells($iExcelEndRow, $iExcelEndCol)).Select
.Range(.Cells($iExcelStartRow, $iExcelStartCol), .Cells($iExcelEndRow, $iExcelEndCol)).value = $oExcel.Application.WorksheetFunction.Transpose($aTemp)
EndWith
Else
With $oExcel.ActiveSheet
.Range(.Cells($iExcelStartRow, $iExcelStartCol), .Cells($iExcelEndRow, $iExcelEndCol)).Select
.Range(.Cells($iExcelStartRow, $iExcelStartCol), .Cells($iExcelEndRow, $iExcelEndCol)).value = $oExcel.Application.WorksheetFunction.Transpose($aArray)
EndWith
EndIf

EndFunc ;==>_ExcelSheetWriteFromArray

Simple Speed Test _ExcelSheetWriteFromArray

#include <excel.au3>

Test_Main()

Func Test_Main()
Local $sXlsFile1 = @ScriptDir & "Test1.xls"
Local $sXlsFile2 = @ScriptDir & "Test2.xls"
Local $oExcel = 0
Local $Tim = 0
Local $aData[5000][10]

For $i = 0 To 4999
$aData[$i][0] = "Y"
$aData[$i][1] = "DATA_TEST_ROW_0" & StringFormat("%03i", $i)
$aData[$i][2] = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum ultrices est varius elit consequat sollicitudin mollis libero ultrices. Quisque lobortis lorem quis nunc vestibulum tincidunt. Duis eget urna sapien, nec egestas purus. Fusce massa nunc."
$aData[$i][3] = $i * 10
$aData[$i][4] = $i * 100
$aData[$i][5] = $aData[$i][3] / $aData[$i][4]
$aData[$i][6] = "Pass"
$aData[$i][7] = @YEAR & "-" & @MON & "-" & @MDAY & "T" & @HOUR & ":" & @MIN
$aData[$i][8] = "FAIL"
$aData[$i][9] = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum ultrices est varius elit consequat sollicitudin mollis libero ultrices. Quisque lobortis lorem quis nunc vestibulum tincidunt. Duis eget urna sapien, nec egestas purus. Fusce massa nunc."
Next

; Speed Test of new function
$oExcel = _ExcelBookNew(0)
$Tim = TimerInit()
_ExcelSheetWriteFromArray($oExcel, $aData)
ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : TimerDiff($Tim) = ' & TimerDiff($Tim) & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console
_ExcelBookSaveAs($oExcel, $sXlsFile1)
_ExcelBookClose($oExcel, 0, 0)

; Speed Test of old function
$oExcel = _ExcelBookNew(0)
$Tim = TimerInit()
_ExcelWriteSheetFromArray($oExcel, $aData)
ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : TimerDiff($Tim) = ' & TimerDiff($Tim) & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console
_ExcelBookSaveAs($oExcel, $sXlsFile2)
_ExcelBookClose($oExcel, 0, 0)

EndFunc ;==>Test_Main


; #FUNCTION# ====================================================================================================================
; Name...........: _ExcelSheetWriteFromArray
; Description ...: Writes a 2D array, or part of, to a specified location in the active worksheet
; Syntax.........: _ExcelSheetWriteFromArray($oExcel, ByRef $aArray, $iExcelStartRow = Default, $iExcelStartCol = Default, $iArrayStartRow = Default, $iArrayStartCol = Default, $iArrayEndRow = Default, $iArrayEndCol = Default)
; Parameters ....: $oExcel - Excel object opened by a preceding call to _ExcelBookOpen() or _ExcelBookNew()
;                $aArray - The array ByRef to write data from (array is not modified)
;                $iExcelStartRow - The spreadsheet row to start writing the array to, default is 1
;                $iExcelStartCol - The spreadsheet column to start writing the array to, default is 1
;                $iArrayStartRow - Start row index of array to start writing data from, default is 0
;                $iArrayStartCol - Start column index of array to start writing data from, default is 0
;                $iArrayEndRow - End row index of array to stop writing data from, default is Ubound($aArray)
;                $iArrayEndCol - End column index of array to stop writing data from, default is Ubound($aArray,2)
; Return values .: Success   - Returns 1
;                Failure - Returns 0 and sets @error on errors:
;                |@error=1   - Excel object does not exist
;                |@error=2   - Not a 2D array
;                |@error=3   - End row greater than start row
;                |@error=4   - End col greater than start col
; Author ........: Bowmore
; Modified.......:
; Remarks .......: Only 2D arrays are currently handeled. Maxim cell content length is 255 characters
; Related .......:
; Link ..........:
; Example .......: Yes
; ===============================================================================================================================
Func _ExcelSheetWriteFromArray($oExcel, ByRef $aArray, $iExcelStartRow = Default, $iExcelStartCol = Default, $iArrayStartRow = Default, $iArrayStartCol = Default, $iArrayEndRow = Default, $iArrayEndCol = Default)
Local $iExcelEndRow = 1
Local $iExcelEndCol = 1

If Not IsObj($oExcel) Then Return SetError(1, 0, 0)
If UBound($aArray, 0) <> 2 Then Return SetError(2, 0, 0)
If $iExcelStartRow = Default Then $iExcelStartRow = 1
If $iExcelStartCol = Default Then $iExcelStartCol = 1
If $iArrayStartRow = Default Then $iArrayStartRow = 0
If $iArrayStartCol = Default Then $iArrayStartCol = 0
If $iArrayEndRow = Default Then $iArrayEndRow = UBound($aArray) - 1
If $iArrayEndCol = Default Then $iArrayEndCol = UBound($aArray, 2) - 1
If $iExcelStartRow < 1 Then $iExcelStartRow = 1
If $iExcelStartCol < 1 Then $iExcelStartCol = 1
If $iArrayStartRow < 0 Then $iArrayStartRow = 0
If $iArrayStartCol < 0 Then $iArrayStartCol = 0
If $iArrayEndRow < $iArrayStartRow Then Return SetError(3, 1, 0)
If $iArrayEndCol < $iArrayStartCol Then Return SetError(4, 1, 0)
$iExcelEndRow = $iExcelStartRow + $iArrayEndRow - $iArrayStartRow
$iExcelEndCol = $iExcelStartCol + $iArrayEndCol - $iArrayStartCol

; Check if only part of the array is to written to the speadsheet
If $iArrayStartRow <> 0 Or $iArrayStartCol <> 0 Or $iArrayEndRow <> UBound($aArray) - 1 Or $iArrayEndCol = UBound($aArray, 2) - 1 Then
;Copy specified array range to a temporary array
Local $aTemp[$iArrayEndRow - $iArrayStartRow + 1][$iArrayEndCol - $iArrayStartCol + 1]
Local $iRow = 0
Local $iCol = 0
For $i = $iArrayStartRow To $iArrayEndRow
$iCol = 0
For $j = $iArrayStartCol To $iArrayEndCol
$aTemp[$iRow][$iCol] = $aArray[$i][$j]
$iCol += 1
Next
$iRow += 1
Next
With $oExcel.ActiveSheet
.Range(.Cells($iExcelStartRow, $iExcelStartCol), .Cells($iExcelEndRow, $iExcelEndCol)).Select
.Range(.Cells($iExcelStartRow, $iExcelStartCol), .Cells($iExcelEndRow, $iExcelEndCol)).value = $oExcel.Application.WorksheetFunction.Transpose($aTemp)
EndWith
Else
With $oExcel.ActiveSheet
.Range(.Cells($iExcelStartRow, $iExcelStartCol), .Cells($iExcelEndRow, $iExcelEndCol)).Select
.Range(.Cells($iExcelStartRow, $iExcelStartCol), .Cells($iExcelEndRow, $iExcelEndCol)).value = $oExcel.Application.WorksheetFunction.Transpose($aArray)
EndWith
EndIf

EndFunc ;==>_ExcelSheetWriteFromArray
Edited by Bowmore
  • Like 1

"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to build bigger and better idiots. So far, the universe is winning."- Rick Cook

Share this post


Link to post
Share on other sites
Spiff59

There is a ticket in BugTracker for a new version of _ExcelReadSheetFromArray() that also eliminates the nested loops that individually access Excel cells in favor of using the Range.Cells method. It shows a similar speed increase.

I would think if you reverted back to the production function name, switched both the $iRowBase ($iArrayStartRow) and $iColBase ($iArrayStartCol) parameters back to defaulting to 1, instead of 0, and maintained the existing error return values (with some of your own additions), that you'd have a backward-compatible function (with 2 new parms) that could replace the one in the production UDF?

Share this post


Link to post
Share on other sites
Bowmore

Thanks for the suggestions. Although I wrote it to meet my own specific needs I would make sense to change the version I posted hear to make it compatible with the existing _ExcelWriteSheetFromArray(). I'll change it in the next couple of days when I have half an hour to make the changes and test it properly.


"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to build bigger and better idiots. So far, the universe is winning."- Rick Cook

Share this post


Link to post
Share on other sites
tempman

Hi,

Your Fast Array Write To Excel Script rocks, but only for 65536 rows, if I put one more 65537 I get an error:

: ==> The requested action with this object has failed.:

.Range(.Cells($iExcelStartRow, $iExcelStartCol), .Cells($iExcelEndRow, $iExcelEndCol)).value = $oExcel.Application.WorksheetFunction.Transpose($aTemp)

.Range(.Cells($iExcelStartRow, $iExcelStartCol), .Cells($iExcelEndRow, $iExcelEndCol)).value = $oExcel.Application.WorksheetFunction.Transpose($aTemp)^ ERROR

can that be fixed?

Share this post


Link to post
Share on other sites
water

I'm about to rewrite the Excel UDF that comes with AutoIt. It's an early Alpha version but your problem has been solved by GMX's (it is named _Excel_RangeRead).

If you can test, please post the results in the Excel Rewrite thread.


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2018-06-01 - Version 1.4.9.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-01-27 - Version 1.3.3.1) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites
GMK

Actually, I need to make changes to the _Excel_RangeWrite function as well. Will post it in the Excel Rewrite thread as soon as I've completed it.

Share this post


Link to post
Share on other sites
tempman

I agree with GMK, I have tested _ExcelSheetWriteFromArray with Excel Rewrite.au3 and 65536 issue is still present.

Waiting for changes to the _Excel_RangeWrite function...

Share this post


Link to post
Share on other sites
BrewManNH

There appears to be a limit of 65536 cells when using the .transpose method at least up to Excel 2007, might even go beyond that, but I'm not sure.


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites
tempman

I find this post on mrexcel.com forum: http://www.mrexcel.com/forum/excel-questions/555073-limitation-range-size-transpose-function.html#post2742571

(The Transpose function just doesn't work with >65536, just as SpecialCells or AdvancedFilter etc.methods sometimes don't work with ranges above a certain size. You'd have to ask the Microsoft coders why they haven't updated these to allow for the larger sheet size of Excel 2007 and subsequent versions.)

I really don't know is this comment form mrexcel.com forum true or false, but I find one not so pretty but working solution for example in our topic

#include <excel.au3>

Test_Main()

Func Test_Main()
Local $oExcel = 0
Local $Tim = 0
Local $aData[65536][1]
Local $aData1[65536][1]

For $i = 0 To 65535
$aData[$i][0] = "DATA_TEST_ROW_0" & StringFormat("%03i", $i+1)
Next

For $j = 0 To 65535
$aData1[$j][0] = "DATA_TEST_ROW_0" & StringFormat("%03i", $j+65537)
Next

; Speed Test of new function
$oExcel = _ExcelBookNew()
$Tim = TimerInit()
_ExcelSheetWriteFromArray($oExcel, $aData)
_ExcelSheetWriteFromArray($oExcel, $aData1, 65537, 1)
ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : TimerDiff($Tim) = ' & TimerDiff($Tim) & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console

EndFunc ;==>Test_Main


; #FUNCTION# ====================================================================================================================
; Name...........: _ExcelSheetWriteFromArray
; Description ...: Writes a 2D array, or part of, to a specified location in the active worksheet
; Syntax.........: _ExcelSheetWriteFromArray($oExcel, ByRef $aArray, $iExcelStartRow = Default, $iExcelStartCol = Default, $iArrayStartRow = Default, $iArrayStartCol = Default, $iArrayEndRow = Default, $iArrayEndCol = Default)
; Parameters ....: $oExcel - Excel object opened by a preceding call to _ExcelBookOpen() or _ExcelBookNew()
;            $aArray - The array ByRef to write data from (array is not modified)
;            $iExcelStartRow - The spreadsheet row to start writing the array to, default is 1
;            $iExcelStartCol - The spreadsheet column to start writing the array to, default is 1
;            $iArrayStartRow - Start row index of array to start writing data from, default is 0
;            $iArrayStartCol - Start column index of array to start writing data from, default is 0
;            $iArrayEndRow - End row index of array to stop writing data from, default is Ubound($aArray)
;            $iArrayEndCol - End column index of array to stop writing data from, default is Ubound($aArray,2)
; Return values .: Success - Returns 1
;            Failure - Returns 0 and sets @error on errors:
;            |@error=1 - Excel object does not exist
;            |@error=2 - Not a 2D array
;            |@error=3 - End row greater than start row
;            |@error=4 - End col greater than start col
; Author ........: Bowmore
; Modified.......:
; Remarks .......: Only 2D arrays are currently handeled. Maxim cell content length is 255 characters
; Related .......:
; Link ..........:
; Example .......: Yes
; ===============================================================================================================================
Func _ExcelSheetWriteFromArray($oExcel, ByRef $aArray, $iExcelStartRow = Default, $iExcelStartCol = Default, $iArrayStartRow = Default, $iArrayStartCol = Default, $iArrayEndRow = Default, $iArrayEndCol = Default)
Local $iExcelEndRow = 1
Local $iExcelEndCol = 1

If Not IsObj($oExcel) Then Return SetError(1, 0, 0)
If UBound($aArray, 0) <> 2 Then Return SetError(2, 0, 0)
If $iExcelStartRow = Default Then $iExcelStartRow = 1
If $iExcelStartCol = Default Then $iExcelStartCol = 1
If $iArrayStartRow = Default Then $iArrayStartRow = 0
If $iArrayStartCol = Default Then $iArrayStartCol = 0
If $iArrayEndRow = Default Then $iArrayEndRow = UBound($aArray) - 1
If $iArrayEndCol = Default Then $iArrayEndCol = UBound($aArray, 2) - 1
If $iExcelStartRow < 1 Then $iExcelStartRow = 1
If $iExcelStartCol < 1 Then $iExcelStartCol = 1
If $iArrayStartRow < 0 Then $iArrayStartRow = 0
If $iArrayStartCol < 0 Then $iArrayStartCol = 0
If $iArrayEndRow < $iArrayStartRow Then Return SetError(3, 1, 0)
If $iArrayEndCol < $iArrayStartCol Then Return SetError(4, 1, 0)
$iExcelEndRow = $iExcelStartRow + $iArrayEndRow - $iArrayStartRow
$iExcelEndCol = $iExcelStartCol + $iArrayEndCol - $iArrayStartCol

; Check if only part of the array is to written to the speadsheet
If $iArrayStartRow <> 0 Or $iArrayStartCol <> 0 Or $iArrayEndRow <> UBound($aArray) - 1 Or $iArrayEndCol = UBound($aArray, 2) - 1 Then
;Copy specified array range to a temporary array
Local $aTemp[$iArrayEndRow - $iArrayStartRow + 1][$iArrayEndCol - $iArrayStartCol + 1]
Local $iRow = 0
Local $iCol = 0
For $i = $iArrayStartRow To $iArrayEndRow
$iCol = 0
For $j = $iArrayStartCol To $iArrayEndCol
$aTemp[$iRow][$iCol] = $aArray[$i][$j]
$iCol += 1
Next
$iRow += 1
Next
With $oExcel.ActiveSheet
.Range(.Cells($iExcelStartRow, $iExcelStartCol), .Cells($iExcelEndRow, $iExcelEndCol)).Select
.Range(.Cells($iExcelStartRow, $iExcelStartCol), .Cells($iExcelEndRow, $iExcelEndCol)).value = $oExcel.Application.WorksheetFunction.Transpose($aTemp)
EndWith
Else
With $oExcel.ActiveSheet
.Range(.Cells($iExcelStartRow, $iExcelStartCol), .Cells($iExcelEndRow, $iExcelEndCol)).Select
.Range(.Cells($iExcelStartRow, $iExcelStartCol), .Cells($iExcelEndRow, $iExcelEndCol)).value = $oExcel.Application.WorksheetFunction.Transpose($aArray)
EndWith
EndIf

EndFunc ;==>_ExcelSheetWriteFromArray

Share this post


Link to post
Share on other sites
BrewManNH

A better way would be to do something like this:

#include <excel.au3>

Test_Main()


; #FUNCTION# ====================================================================================================================
; Name...........: _ExcelSheetWriteFromArray
; Description ...: Writes a 2D array, or part of, to a specified location in the active worksheet
; Syntax.........: _ExcelSheetWriteFromArray($oExcel, ByRef $aArray, $iExcelStartRow = Default, $iExcelStartCol = Default, $iArrayStartRow = Default, $iArrayStartCol = Default, $iArrayEndRow = Default, $iArrayEndCol = Default)
; Parameters ....: $oExcel - Excel object opened by a preceding call to _ExcelBookOpen() or _ExcelBookNew()
;            $aArray - The array ByRef to write data from (array is not modified)
;            $iExcelStartRow - The spreadsheet row to start writing the array to, default is 1
;            $iExcelStartCol - The spreadsheet column to start writing the array to, default is 1
;            $iArrayStartRow - Start row index of array to start writing data from, default is 0
;            $iArrayStartCol - Start column index of array to start writing data from, default is 0
;            $iArrayEndRow - End row index of array to stop writing data from, default is Ubound($aArray)
;            $iArrayEndCol - End column index of array to stop writing data from, default is Ubound($aArray,2)
; Return values .: Success - Returns 1
;            Failure - Returns 0 and sets @error on errors:
;            |@error=1 - Excel object does not exist
;            |@error=2 - Not a 2D array
;            |@error=3 - End row greater than start row
;            |@error=4 - End col greater than start col
; Author ........: Bowmore
; Modified.......:
; Remarks .......: Only 2D arrays are currently handeled. Maxim cell content length is 255 characters
; Related .......:
; Link ..........:
; Example .......: Yes
; ===============================================================================================================================
Func _ExcelSheetWriteFromArray($oExcel, ByRef $aArray, $iExcelStartRow = Default, $iExcelStartCol = Default, $iArrayStartRow = Default, $iArrayStartCol = Default, $iArrayEndRow = Default, $iArrayEndCol = Default)
    Local $iExcelEndRow = 1
    Local $iExcelEndCol = 1

    If Not IsObj($oExcel) Then Return SetError(1, 0, 0)
    If UBound($aArray, 0) <> 2 Then Return SetError(2, 0, 0)
    If $iExcelStartRow = Default Then $iExcelStartRow = 1
    If $iExcelStartCol = Default Then $iExcelStartCol = 1
    If $iArrayStartRow = Default Then $iArrayStartRow = 0
    If $iArrayStartCol = Default Then $iArrayStartCol = 0
    If $iArrayEndRow = Default Then $iArrayEndRow = UBound($aArray) - 1
    If $iArrayEndCol = Default Then $iArrayEndCol = UBound($aArray, 2) - 1
    If $iExcelStartRow < 1 Then $iExcelStartRow = 1
    If $iExcelStartCol < 1 Then $iExcelStartCol = 1
    If $iArrayStartRow < 0 Then $iArrayStartRow = 0
    If $iArrayStartCol < 0 Then $iArrayStartCol = 0
    If $iArrayEndRow < $iArrayStartRow Then Return SetError(3, 1, 0)
    If $iArrayEndCol < $iArrayStartCol Then Return SetError(4, 1, 0)
    If $iArrayEndRow > UBound($aArray) - 1 Then $iArrayEndRow = UBound($aArray) - 1 ; <<<<<<<<<<<<<<< added this line, because there's no check to see if the $iArrayEndRow is larger than the end of the array
    $iExcelEndRow = $iExcelStartRow + $iArrayEndRow - $iArrayStartRow
    $iExcelEndCol = $iExcelStartCol + $iArrayEndCol - $iArrayStartCol

    ; Check if only part of the array is to written to the speadsheet
    If $iArrayStartRow <> 0 Or $iArrayStartCol <> 0 Or $iArrayEndRow <> UBound($aArray) - 1 Or $iArrayEndCol = UBound($aArray, 2) - 1 Then
        ;Copy specified array range to a temporary array
        Local $aTemp[$iArrayEndRow - $iArrayStartRow + 1][$iArrayEndCol - $iArrayStartCol + 1]
        Local $iRow = 0
        Local $iCol = 0
        For $i = $iArrayStartRow To $iArrayEndRow
            $iCol = 0
            For $j = $iArrayStartCol To $iArrayEndCol
                $aTemp[$iRow][$iCol] = $aArray[$i][$j]
                $iCol += 1
            Next
            $iRow += 1
        Next
        With $oExcel.ActiveSheet
            .Range(.Cells($iExcelStartRow, $iExcelStartCol), .Cells($iExcelEndRow, $iExcelEndCol)).Select
            .Range(.Cells($iExcelStartRow, $iExcelStartCol), .Cells($iExcelEndRow, $iExcelEndCol)).value = $oExcel.Application.WorksheetFunction.Transpose($aTemp)
        EndWith
    Else
        With $oExcel.ActiveSheet
            .Range(.Cells($iExcelStartRow, $iExcelStartCol), .Cells($iExcelEndRow, $iExcelEndCol)).Select
            .Range(.Cells($iExcelStartRow, $iExcelStartCol), .Cells($iExcelEndRow, $iExcelEndCol)).value = $oExcel.Application.WorksheetFunction.Transpose($aArray)
        EndWith
    EndIf

EndFunc   ;==>_ExcelSheetWriteFromArray

Func Test_Main()
    Local $oExcel = 0
    Local $Tim = 0
    Local $aData[131072][1]

    For $i = 0 To 131071
        $aData[$i][0] = "DATA_TEST_ROW_0" & StringFormat("%03i", $i + 1)
    Next
    ; Speed Test of new function
    $oExcel = _ExcelBookNew()
    $Tim = TimerInit()
    ; This only writes 65535 elements of the array at a time, if your array is larger than that, then it will split it up
    ; for you.
    For $loop = 0 To UBound($aData) - 1 Step 65536 
        _ExcelSheetWriteFromArray($oExcel, $aData, $loop + 1, 0, $loop, 0, $Loop + 65535)
    Next
    ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : TimerDiff($Tim) = ' & TimerDiff($Tim) & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console

EndFunc   ;==>Test_Main

This rewrites the test script that tempman posted above and also corrects a bug in the _ExcelSheetWriteFromArray function if you send it an end row of the array that is beyond the UBound dimension of the array.

This will write 65535 rows to the spreadsheet until you've hit the end of the array, and the array can be of any size either larger or smaller than 65535 rows.


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites
GMK
updated.

Share this post


Link to post
Share on other sites
water

Thanks GMK!

I hope to release a new alpha version with your changes quite soon.

Edited by water

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2018-06-01 - Version 1.4.9.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-01-27 - Version 1.3.3.1) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites
water

I posted a revised version of _Excel_RangeWrite for you to test.

Would like to hear from Excel 2003 testers as I only can test with Excel 2010.


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2018-06-01 - Version 1.4.9.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-01-27 - Version 1.3.3.1) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites
DeDee

Hi

Is there a script for Fast Array Write from Excel? I see that it takes a lot of time for this as well - when using big Excel files.

Thank you!

Share this post


Link to post
Share on other sites
water

Welcome to AutoIt and the forum,

the rewrite of the Excel UDF I'm currently working on has a _Excel_RangeRead function too.

Please check the for the latest download. But be aware that it is still an early Alpha version!

If you search the forum you will find some other fast _ExcelReadSheetToArray functions too.


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2018-06-01 - Version 1.4.9.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-01-27 - Version 1.3.3.1) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

  • Similar Content

    • TrashBoat
      By TrashBoat
      So Im trying to make a simple 2d game and make some sort of collision detection so why not to make a 2 dimensional array but i have no clue how  to write it in multiple lines
      Global $map[5,5] = [0,0,0,0,0 _ [0,0,0,0,0 _ [0,0,0,0,0 _ [0,0,0,0,0 _ [0,0,0,0,0] something like this but it doesn't work
    • Zein
      By Zein
      #include "..\Include\Array.au3" #include "..\Include\File.au3" #include "..\Include\AutoItConstants.au3" Local $aRetArray Local $sFilePath = "n.csv" _FileReadToArray($sFilePath, $aRetArray, ",") ; _FileReadToArray($sFilePath, $aRetArray, $FRTA_COUNT, ",") _ArrayDisplay($aRetArray, "Original", Default, 8) The above code shows two versions of _FileReadToArray and both don't work as expected.
      The first one doesn't use the comma as a delimiter. (so I get a single column array)  I tried adding "Default" between $aRetArray and "," then it told me it had an incorrect number of parameters. 

      I looked again at the documentation:
       
      #include <File.au3> _FileReadToArray ( $sFilePath, ByRef $vReturn [, $iFlags = $FRTA_COUNT [, $sDelimiter = ""]] )
      And I with or without the flags params I should be getting a 2D array due to my file being a csv. 
      I then tried a regular flag, $FRTA_COUNT, and it tells me that I'm using a variable $FRTA_COUNT while it's not declared. Tried putting in 1 instead and it told me again, incorrect number of params. 

       
    • nooneclose
      By nooneclose
      I need to perform a subtotal in excel and I would like to automate this process using Autoit if possible like always any and all help will be greatly appreciated. 
      I can not find a good example but the two from Microsoft. Here is one of the two from msdn.microsoft.com/en-us/vba/excel-vba/articles/range-subtotal-method-excel
      I do not really understand how to translate this into AutoIt, but I gave it a try and here is what I have.
      $OpenRange      = "A1:E200" $xlSum          = -4157 $Added_Array[2] = [2, 3] $OpenRange.Subtotal("B1", $xlSum, $Added_Array, True, False, True) I just need to perform a subtotal on a range based on a header called department, and then perform a sum on the results.
    • nooneclose
      By nooneclose
      How to use _Excel_RangeSort to sort my excel file by three different headers Column A1, B1, and C1 have headers on which I want to sort by. The headers on which I want to sort are department, employee type, and name.
      I still really new to AutoIt so I do not actually know how to properly start this line or lines of code, to be honest. The example code is the best I can do.
      _Excel_RangeSort($OpenWorkbook, Default, "A1:C1", "1:1", $xlDescending, Default, $xlYes, Default, $xlSortRows) I just need to sort by those three headers in that order of department, employee type, and name, plus in descending order.
       
      any and all help would be greatly appreciated.  Thank you!
    • ternal
      By ternal
      Hi,
      Recently I have had the need to do a sort and then do a second sort while the item of the first sort stays the same ( double sorting , first on column x then while column x is the same sort column y).
      I did not put much efffort into error checking but so far I did not need it.
      For my applications so far it works perfectly however if someone is willing I want to test this extensivly.
      If anyone has big lists of random stuff to sort could you try this out please?
      #include <Array.au3> ; #FUNCTION# ==================================================================================================================== ; Name ..........: _ArraySort_Double ; Description ...: ; Syntax ........: _ArraySort_Double (Byref $array[, $first_index = Default[, $second_index = Default[, $ascending = Default]]]) ; Parameters ....: $array - 2d array to sort. ; $first_index - [optional] first column to sort. Default is 0. ; $second_index - [optional] second column to sort. Default is 1. ; $ascending - [optional] ascending/descending. Default is 1. ; Return values .: 1 if no errors occured , -1 if errors occured ; Author ........: Ternal ; Remarks .......: Needs excessive testing. ; Related .......: _arraysort() ; =============================================================================================================================== Func _ArraySort_Double (byref $array, $first_index = Default, $second_index = Default, $ascending = Default) Local $temp_value Local $counter = 1 If UBound($array, $UBOUND_DIMENSIONS) <> 2 Then MsgBox(0, "error", "error") return -1 EndIf If $first_index = Default Then $first_index = 0 If $second_index = Default Then $second_index = 1 If $ascending = Default Then $ascending = 1 _ArraySort($array, $ascending, 0, 0, $first_index); you can alter settings of primary sort here If @error Then MsgBox(0, "error", @error) return -1 EndIf $temp_value = $array[0][$first_index] For $x = 1 to UBound($array, 1) - 1 If Mod( $x, 10000) = 0 Then ConsoleWrite("at " & $x & " of a total : " & UBound($array, 1) & @CRLF) If $array[$x][$first_index] = $temp_value Then $counter+= 1 If $x = UBound($array, 1) - 1 Then; do last line here(if last line is not a new item) _ArraySort($array, $ascending, $x - $counter, $x, $second_index);you can alter settings of secondary sort here(don't forget to place line 34 the exact same) If @error Then MsgBox(0, "error", @error) return -1 EndIf EndIf Else If $counter > 0 Then ;at least 2 of the same _ArraySort($array, $ascending, $x - $counter, $x - 1, $second_index);you can alter settings of secondary sort here(don't forget to place line 29 the exact same) If @error Then MsgBox(0, "error", @error) return -1 EndIf $counter = 1 EndIf EndIf $temp_value = $array[$x][$first_index] Next Return 1 EndFunc Kind regards, Ternal
×