Excel UDF
This page is still a work in progress.
The Excel UDF offers functions to control and manipulate Microsoft Excel workbooks.
This page describes the Excel UDF that comes with AutoIt 3.3.12.0 or later.
Features
New versions of Microsoft Office have been released since the last changes were made to the Excel UDF. The new extensions (e.g. xlsx) were not (fully) supported, new functions were missing etc. The current version of the Excel UDF lifts this limitations.
- Works with as many instances of Excel as you like - not just one
- Works with any Workbook - not just the active one
- Works with any Worksheet - not just the active one
- Only does what you tell it to do - no implicit "actions"
- Only one function to read from a cell or a range
- Only one function to write a string, a 1D or 2D array to a cell or a range
- Support for every file format Excel supports
- Speed enhancements when transferring data from/to an Excel sheet (20 - 100 times faster)
Concepts
Range
A Range is a rectangular block made of one or more cells that Excel treats as a unit. The functions of the UDF mainly work with ranges. A range - unlike a selection - is not visible on the screen.
Examples how to define special ranges can be found here.
Cell reference
The UDF only supports the A1 form (example: "B7") to reference cells. The R1C1 form (example "R7C2") is not supported. But the UDF provides functions to translate a cell reference between this two forms.
You can reference cells by name as well.
Examples
- Single cell: "B7"
- Multiple cells: "A1:B7"
- Row(s): "2" or "3:5"
- Column(s): "B" or "D:F"
- Name: "TestRange"
Features not covered by the UDF
The UDF only covers basic user needs. Single line functions (like switching to another sheet) or functions with too many parameters (like formatting a cell or range) are not covered by this UDF. You need to use the Excel COM yourself.
I will give a few examples here. The rest can be found on MSDN.
Format a range
Alignment
; Horizontal alignment
; Enumeration for Excel 2010: http://msdn.microsoft.com/en-us/library/ff840772%28v=office.14%29.aspx
$oRange.HorizontalAlignment = $XlHAlign ; Can be xlCenter, xlDistributed, xlJustify, xlLeft or xlRight of the XlHAlign enumeration.
; Vertical alignment
; Enumeration for Excel 2010: http://msdn.microsoft.com/en-us/library/ff835305%28v=office.14%29.aspx
$oRange.VerticalAlignment = $XlVAlign ; Can be xlBottom, xlCenter, xlDistributed, xlJustify or xlTop of the XlVAlign enumeration.
Background
See
Border
...
Color
...
Font
Bold etc.
True if the font is bold. Read/write.
$oRange.Font.Bold = True
This works similar for Italic, Strikethrough, Subscript, Superscript and Underline.
Color, ColorIndex
Color: Returns or sets the primary color of the object. Use the RGB function to create a color value.
ColorIndex: Returns or sets a variant value that represents the color of the font.
The color is specified as an index value into the current color palette.
Object | Color |
---|---|
Border | The color of the border. |
Borders | The color of all four borders of a range. If they're not all the same color, Color returns 0 (zero). |
Font | The color of the font. |
Interior | The cell shading color or the drawing object fill color. |
Tab | The color of the tab. |
$oRange.Font.Color = 5 ; Set the color of the font
$oRange.Borders.ColorIndex = 5 ; Set the color of all four borders
Name
Returns or sets a variant value that represents the name of the font.
$oRange.Font.Name = "Arial"
Size
Returns or sets the size of the font specified in units of points.
$oRange.Font.Size = 12
Underline
Returns or sets the type of underline applied to the font. Can be one of the XlUnderlineStyle constants xlUnderlineStyleNone, xlUnderlineStyleSingle, xlUnderlineStyleDouble, xlUnderlineStyleSingleAccounting or xlUnderlineStyleDoubleAccounting. Read/write.
$oRange.Font.Underline = $xlUnderlineStyleSingle
Height/Width
...
Number Format
Returns or sets a variant value that represents the format code for the object.
This property returns Null if all cells in the specified range don't have the same number format.
The format code is the same string as the Format Codes option in the Format Cells dialog box. The Format function uses different format code strings than do the NumberFormat and NumberFormatLocal properties.
A number format consists of up to 4 items, separated by semicolons. Each of the items is an individual number format. The first by default applies to positive numbers, the second to negative numbers, the third to zeros, and the fourth to text.
A very good description of format codes can be found here.
Format code | Description |
---|---|
@ | Format as string |
General | Default format |
0000 | Format number with 4 digits, add leading zeroes where needed |
[Blue]$#,##0;[Red]$#,##0;$#,##0 | Format positive numbers in blue, negative in red and 0 in default color (black). The numbers are prefixed with the dollar sign and a thousands separator is inserted. The numbers are displayed as integers. Available colors are [Black], [Blue], [Cyan], [Green], [Magenta], [Red], [White], and [Yellow]. |
[Blue][>=3000]$#,##0;[Red][<0]$#,##0;$#,##0 | Format values >= 3000 in blue, values < 0 in read and all other numbers in default color (black). |
0" feet" | append a label to the number |
$oRange.NumberFormat = "General"
Script breaking changes after AutoIt version 3.3.10.2.
New versions of Microsoft Office have been released since the last changes were made to the Excel UDF. New file types and new functions needed to be supported, hence the UDF was complete rewritten.
Some functions/parameters have been removed or renamed, new functions/parameters have been added. A detailed list of changes can be found here.
General
All function names have been changed from _Excel* to _Excel_*.
@extended no longer contains the number of the invalid parameter. The code returned in @error tells exactly what went wrong.
The following list shows the old/new function/parameter name (a "-" is shown if the function/parameter has been removed) and some example scripts how to mimic the behaviour of the "old" UDF. If there is no entry for a removed function/parameter then there is no need for this functionality.
Function -/_Excel_Open
It's mandatory now to call function _Excel_Open before any other function. This function didn't exist in the old UDF. @extended is set if Excel was already running.
Function _ExcelFontSetProperties/-
There are so many formatting functions in Excel that they can't be covered by a few functions. The function only contained a single line of code. So it was removed. Use the code examples above to format a range.
Function _ExcelHorizontalAlignSet/-
There are so many formatting functions in Excel that they can't be covered by a few functions. The function only contained a single line of code. So it was removed. Use the code examples above to format a range.
Function _ExcelSheetActivate/-
The function only contained a single line of code. So it was removed. Replace the function with the following code:
$oWorkbook.Sheets(x).Activate ; x can be the number or name of the sheet to be activated
Function _ExcelSheetNameGet/-
The function only contained a single line of code. So it was removed. Replace the function with the following code:
$sSheetName = $oSheet.Name
Function _ExcelSheetNameSet/-
The function only contained a single line of code. So it was removed. Replace the function with the following code:
$oSheet.Name = "Name of the sheet"
Compare example scripts
In this section I will show how some selected example scripts taken from AutoIt 3.3.8.1 should look like with the new Excel UDF.
To enhance readability error checking statements have been omitted.
But I highly recommend to check for errors after each call of a _Excel_* function.
AutoIt 3.3.8.1 | AutoIt 3.3.12.0 and later |
---|---|
_ExcelBookAttach | _Excel_BookAttach |
#include <Excel.au3>
Local $sFilePath = @TempDir & "\Temp.xls"
_ExcelBookOpen($sFilePath)
Local $oExcel = _ExcelBookAttach($sFilePath)
|
#include <Excel.au3>
Local $sWorkbook = @TempDir & "\Temp.xls"
Local $oExcel = _Excel_Open()
Local $oWorkbook = _Excel_BookOpen($oExcel, $sWorkbook)
$oWorkbook = _Excel_BookAttach($sWorkbook)
|
_ExcelBookClose | _Excel_BookClose |
#include <Excel.au3>
Local $oExcel = _ExcelBookNew()
_ExcelBookClose($oExcel)
|
#include <Excel.au3>
Local $oExcel = _Excel_Open()
Local $oWorkbook = _Excel_BookNew($oExcel)
_Excel_BookClose($oWorkbook, False)
|
_ExcelBookNew | _Excel_BookNew |
#include <Excel.au3>
Local $oExcel = _ExcelBookNew()
|
#include <Excel.au3>
Local $oExcel = _Excel_Open()
Local $oWorkbook = _Excel_BookNew($oExcel, 2)
|
_ExcelBookOpen | _Excel_BookOpen |
#include <Excel.au3>
Local $sFilePath1 = @ScriptDir & "\Test.xls"
Local $oExcel = _ExcelBookOpen($sFilePath1)
|
#include <Excel.au3>
Local $oExcel = _Excel_Open()
Local $sWorkbook = @ScriptDir & "\Test.xlsx"
Local $oWorkbook = _Excel_BookOpen($oExcel, $sWorkbook, Default, Default, True)
|