Академический Документы
Профессиональный Документы
Культура Документы
Cell
1.
2.
3.
4.
5.
6.
7.
8.
Instantiation
Properties
Font Formatting
Methods
Ordering
Range Syntax
Active Cell
Unicode
CellRange
1. Instantiation
2. Properties
3. Methods
Worksheets
1.
2.
3.
4.
5.
6.
7.
8.
Active Sheet
Display Sheet
Clear/Delete row/column
Insert row/column
Sheet functions
Merging Cells
Pivot Tables
Saving
VBA
1. Calling VBA from DataNitro
2. Calling DataNitro from VBA
3. Sheets: name and codenameCell
Cell
The "Cell" class represents a single cell of an Excel spreadsheet.
1. Instantiation
There are several ways to instantiate a cell:
1. With a (row, column) pair or tuple.
e.g: Cell(2,1) and Cell((2,1)) return the cell "A2".
2. By name.
e.g: Cell("A2") returns the cell "A2".
3. By entering a range. This will return the first cell in the range.
e.g: Cell("A2:A10^A3:B5,A4,C3") returns the cell "A3".
4. By entering a named range. This will return the first cell in the range. Named
ranges must be on the sheet passed to Cell (or the active sheet).
e.g: Cell("Use_VBA") will return the cell "NOT1993" if "Use_VBA" is the range
"NOT1993:NOT1995".
By default, cells are on the active worksheet. To use a different worksheet,
pass its name as the first argument when calling Cell, or change the active
sheet.
2. Properties
Cells have a number of associated properties.
row, col, position, sheet and name are set when the cell is created, and should not
be changed.
row returns the Cell's row, col and column return its column, and position returns
(row, column) as a tuple.
Cell("A2").row = 2
Cell("A2").col = 1
Cell("A2").column = 1
Cell("A2").position = (2, 1)
value returns and sets the value of a cell. If you want to set a cell to evaluate
an Excel formula, you should use formula. A blank cell will return None.
e.g: Cell("A3").value will return 42 if the cell A3 is set to 42, or contains a formula
which evaluates to 42.
Cell("A3").value will return None if A3 is empty. (This won't print anything if run in
the shell.)
formula returns and sets the formula of a cell. You can set a cell's formula by
passing the formula as a string; you should use value to pass non-formula
values. If you query a cell with no formula, it will return its value instead.
Cell("B5").formula = "=SUM(B1:B4)" will set the value of B5 to the sum of the values
of B1 through B4.
Date
If a cell has a date in it, date will return a Python datetime.date object. Otherwise,
it will return the value in the cell. Setting date is identical to setting a Cell's
value.
datetime.date(2012, 2, 29).
comment will get and set the comment in a cell. Setting a comment to an empty
string removes it.
this is a
comment.
>>> Cell("A1").alignment
'general'
>>> Cell("A1").alignment = 'center'
>>> Cell("A1").alignment
'center'
Color
color returns and sets the color of a cell. Excel stores colors as 24 bit numbers,
with the first two hexadecimal digits describing the amount of red, the next two
the amount of green, and the last two the amount of blue, like html. You can
pass 24 bit numbers to color as hexadecimal numbers, strings, or regular
numbers.
DataNitro also recognizes 16 predefined colors: black, white, red, blue, green,
yellow, purple, gray, silver, maroon, olive, lime, teal, aqua, navy, and fuchsia.
You can see these color names by printing Cell.COLORS(), which prints ['BLACK',
'GRAY', 'SILVER', 'WHITE', 'MAROON', 'RED', 'OLIVE', 'YELLOW', 'GREEN', 'LIME', 'TEAL',
'AQUA', 'NAVY', 'BLUE', 'PURPLE', 'FUCHSIA']
e.g. Cell("C3").color = 'olive' will set C3 to olive.
Cell("C3").color = 'teal' will set C3 to teal.
3. Font Formatting
The font property of a cell is a class that controls a cell's font. font has the
following properties: color, size, bold, italic, underline, strikethrough, subscript, and
superscript. Printing font will print a cell's font color, size, and any active font
styles.
e.g: print Cell("A1").font will print font color: black, font size: 11 if A1 has not had any
changes to its font.
If A2 has a red, size 8 font with bold, italic, and superscript turned on, print
Cell("A2").font will print font color:red, font size: 8, bold, italic, superscript.
Font Size:
Size size gets and sets font sizes. If a cell has text of more than one
size, this will return 0.
e.g: Cell("A1").font.size = 409 will make the text in cell A1 very large.
Font Color:
Color color gets and sets font colors. This accepts colors in the same
format as Cell.color. If a cell has mixed colors, font.color will return 'black'.
e.g: Cell("A1").font.color = 'red' will make the text in A1 red.
Font Styles:
Styles Calling any of bold, italic, underline, strikethrough, subscript, and
superscript will return True if the property is on for the cell, and False if it's off. A
value passed to the Cell will be interpreted as a boolean (i.e. 0, '', [ ], and ()
evaluate to False, nonempty containers and non-zero numbers evaluate to
True). If a cell has mixed formatting (e.g. part bold and part not bold), the
associated style will return True. If a cell has mixed superscript and/or
subscripts, both superscript and subscript will return True.
e.g: Cell("A1").font.bold will return True if the cell has bolded text, and False if it
doesn't.
Notes: Excel supports a maximum of 512 different fonts. Setting more than
this number of fonts will cause Excel to freeze or raise errors. (For example,
coloring a 25 by 25 block of cells with different font colors will cause errors.)
Excel font sizes must be positive integers, and the maximum font size is 409.
Only one of subscript and superscript can be active at a time. Turning on
superscript in a cell set to subscript will turn off superscript, and vice versa.
4. Methods
Cells also have some associated methods.
Clear
clear
is_empty
Offset
offset takes a (row, column) tuple, and returns the cell at the appropriate
position.
x.offset(y-x) == y
returns True.
set_name will create a named range in Excel containing the cell it's called on. If
the named range already exists, it will be overwritten.
5. Ordering
Cells are ordered lexicographically within a sheet by row and then by column.
e.g: "A1" is the first cell. "x1" is less than "y2" for any x and y. "B5" is greater
than "B3".
Cells from different sheets will compare as unequal, and trying to order them
(e.g. Cell("Sheet1", "A1") < Cell("Sheet2", "A2")) raises an exception.
6. Range Syntax
You can enter a range as you would in Excel, i.e. using the ":", ",", and " "
(space) operators (the last two are union and intersection, respectively). You
can use "^" for intersection instead of " " if you prefer. This accepts named
ranges as well.
7. Active Cell
set_active will set the active cell to the cell the method is called on. This cell
Cell("Sheet2", "B3").set_active() will set the active cell to B3 if Sheet2 is active, and
raise an error otherwise.
Active Cell Functions
The active_cell function gets and sets the active cell. This is a general function,
not a method of Cell. Calling active_cell with no argument will return the active
cell, while calling it with a cell or with arguements which can instantiate a cell
(such as two integers, a tuple, or an Excel range) will set the active cell to that
cell.
e.g: active_cell() will return Cell("A1") when you first open a workbook.
8. Unicode
Unicode
If you read a cell with unicode characters into the Python shell, the characters
will be returned as codepoints (e.g: "u'\u671d\u540d').
To display these characters, you have to print the value. The characters will
print properly if you're using a version of Windows that supports the language
the characters are in.
CellRange
The "CellRange" class is a container for cells. Cells in a CellRange must be
on a single sheet. Cells are stored in order and without duplication.
1. Instantiation
You can create a CellRange the same way you create a Cell:
1. With a (row, column) pair or tuple.
e.g: CellRange(2,1) and CellRange((2,1)) return the CellRange containing "A2",
which we'll refer to as ["A2"].
2. By entering a range. This will return all the cells in the range.
e.g: CellRange("A2") returns ["A2"].
e.g: CellRange("A2:A10^A3:B5,A4,C3") returns ["A3","A4","A5","C3"].
3. By entering a named range. This will return the range. Named ranges must be
on the sheet passed to CellRange (or the active sheet).
e.g: CellRange("Use_VBA") will return ["NOT1993","NOT1994","NOT1995"] if
"Use_VBA" is the range "NOT1993:NOT1995".
There are also two ways to create a CellRange that don't have analogues for Cells:
1. With two (row, column) pairs or tuples. This returns the range with top left
corner given by the first pair and bottom right corner given by the second.
e.g: CellRange((2,1),(3,4)) and CellRange(2,1,3,4) return
["A2","B2","C2","D2","A3","B3","C3","D3"].
2. With a list of (row, column) tuples. This returns the cells given by the list.
e.g: CellRange([(1,1),(2,2),(3,3),(4,4),(5,5)]) returns ["A1","B2","C3","D4","E5"].
2. Properties
Like Cell, CellRange has sheet and name properties. These are set when the
CellRange is created and should not be changed. sheet contains the sheet of
all of the cells in the CellRange.
name is set to the string that was used to initialize it. If it was initialized with two
tuples or four integers, the name will be generated using a colon.
e.g: CellRange("A1:B5").name and CellRange((1,1),(5,2)).name will return "A1:B5", while
CellRange("A1,B1,A2:B5").name returns "A1,B1,A2:B5".
position
position returns the positions of all the cells in the range in a list.
e.g: CellRange("B1:C2").position returns [(1,2),(1,3),(2,2),(2,3)].
Value & Formula
Like Cell, CellRange has value and formula properties. CellRange("A1:B5").value is
shorthand for [cell.value for cell in CellRange("A1:B5")]. You can set CellRange.value to
a single value or to a list with the same length as the CellRange. Passing a
single value will set all the cells in the CellRange to that value.
e.g: CellRange("A1:B5").value = 5 will set the values of cells "A1" through "E5" to
5.
10
Getting and setting formulas works in the same way. CellRange.formula will
accept both formulas and values. This enables copying over data from one
range to another, including formulas. Formulas are copied directly, not
relatively.
e.g: CellRange("B1:B5").formula = "=A1" will set cells "B1" through "B5" to the value
of "A1".
If any of the cells contain data (instead of formulas), they'll be copied as well.
e.g: CellRange("A1:E5").font.bold = True will bold the text in cells A1 through E5.
CellRange("A1:A10").font.bold = [True, False] * 5 will bold the text of every other cell
in A1 through A10.
If A1 through A10 have not had any other formatting changes, after this
command print CellRange("A1:A10").font will print
11
3. Methods
Concatenate
You can concatenate two CellRanges from the same sheet by adding them.
Attempting to add two CellRanges from different sheets will raise an
exception. The name of the new CellRange will consist of the names of the
two CellRanges added together with a comma between them.
e.g: CellRange("A1, B2, C3, D4, E5").set_name("diagonal") will create a named range
called 'diagonal' containing the cells A1, B2, C3, D4 and E5.
is empty
is_empty will return True if the range is empty and False otherwise.
e.g: CellRange("A1:A10").is_empty() will return True if A1 through A10 are all
empty, and False otherwise.
Modifying CellRanges
You can append, delete and set cells in CellRanges.
append
12
You can append a Cell to a CellRange if both are on the same sheet. The
name of the Cell will be added to the name of the CellRange. If the Cell is
already in the CellRange, the command will have no effect.
delete
Deleting a cell will remove it from the CellRange. The name of the CellRange
will change to the representation of the new CellRange.
x = CellRange("A1:E5")
del x[2]
x.append(Cell(16,25))
Comparisons
Two CellRanges are considered equal if they contain the same cells on the
same sheet. They may have different names. CellRanges are unordered. e.g:
13
Worksheets
1. Active Sheet
When you create a new Cell or CellRange without specifying a sheet
argument, it will be placed on the active sheet. The default active sheet is the
one that is visible in Excel when you run a script or open a Python shell.
The active_sheet function interacts with the active sheet. Calling active_sheet
without an argument will return the active sheet, and calling it with a sheet
name will set the active sheet.
The display_sheet function interacts with the display sheet. Calling display_sheet
without an argument will return the display sheet, and calling it with a sheet
name will set the display sheet.
The functions clear_row, clear_col, del_row, and del_col will clear or delete a row or
column. Rows are specified by number, and columns can be specified by
number or letter. You can also pass a cell, which will result in the row or
14
You can pass a sheet name as the second argument to specify which a sheet;
otherwise the function will interact with the active sheet.
Note: calling del_row([1, 2]) is not the same as calling del_row(1) and then
del_row(2). del_row([1,2]) will delete the first two rows, while del_row(1) will delete
the first row, which will shift all rows in the spreadsheet. del_row(2) will then
delete the new second row; this was originally the third row, so this is
equivalent to calling del_row([1,3]).
4. Insert row/column
insert_row and insert_col insert rows and columns, respectively. They take the
same input as clear_row and clear_col, and will insert a row above the row
passed (or a column before the column passed).
5. Sheet functions
Autofit
The autofit command resizes sheet cells. It will resize the active sheet unless
passed a sheet name. You can pass a range as the second argument, to
resize the columns in that range.
15
Clear Sheet
clear_sheet clears the named sheet. If called with no argument, clears the
active sheet.
new_sheet creates a new sheet. The sheet can't have the same name as an
existing sheet. If called with no arguments, will create the next numbered
sheet.
e.g: new_sheet("Data") will create a sheet named "Data". new_sheet() will create
remove_sheet removes a sheet. You can't remove the last sheet in a workbook.
Rename Sheet
Note: After installing DataNitro, Excel will be missing Sheet4. If you add
6. Merging Cells
merge_cells(start_cell, end_cell) will merge the cells in the box with top-left
start_cell and bottom-right end_cell. start_cell and end_cell can be cells or cell
16
names. To use a sheet other than the active sheet, pass it as the optional
third argument.
e.g: merge_cells(Cell(1,1), Cell(5,5)) and merge_cells("A1", "E5") will merge the cells
between A1 and E5.
7. Pivot Tables
pivot_refresh(sheet) will refresh the pivot tables on sheet. If sheet isn't passed, it'll
e.g: pivot_refresh() will refresh the pivot tables on the active sheet, and
You can save the workbook by calling save(). You can also save the file under
save_copy("DataNitro").
Both save with a filename and save_copy will save to your Documents
17
1. Overview
To import Python functions, define them in a file named "functions.py" and
import that file into DataNitro. Any function defined in the file will then be
available for use in Excel. It's not necessary to run the script.
Imported functions cannot make calls to other Excel objects (so they cannot
use Cell or CellRange objects, for example). To use values in other cells,
pass them as arguments to the function.
e.g: To compute the sum of cells A1 and A2, write the following in
functions.py:
def my_sum_a1_a2():
return Cell(A1).value + Cell(A2).value
18
Function Output
The udf will print the return value in the cell it is called. If an error occurs, it will
print the error to the cell.
3. Notes
If you have multiple instances of Excel open, udf's will only work in the first
one.
19
VBA
1. Calling VBA from DataNitro
You can call VBA subroutines and functions with the VBA command. These
docs refer to both subroutines and functions as macros.
macro_name
args is
target_sheet
See the bottom of this page for the difference between a sheet's name and codename.
Subroutines
Subroutines can be defined in a sheet or in a module. If a subroutine is
defined in a module, you don't have to specify which module its defined in
unless there's another macro with the same name defined in a different
module.
VBA("test")
VBA("Module1.test")
VBA("Module1.test", [ ], "Sheet1")
20
defined in.
If "test" is defined in "Sheet1",
VBA("Sheet1.test")
VBA("Sheet1.test", [ ], "Sheet2")
VBA("test")
will fail.
Functions
Functions are called in the same way as subroutines. VBA functions must be
Function TimesThree(x)
TimesThree = x * 3
End Function
>>>VBA("Module1.TimesThree", [2])
6
>>>VBA("TimesThree", [4])
12
2. Calling DataNitro from VBA
Calling Scripts
You can call a DataNitro script from VBA with this line:
Sub call_DN()
Application.COMAddIns("DataNitro.DataNitro").Object.RunScript ("test.py")
End Sub
Replace "test.py" with the name of your imported script. If you have multiple scripts
with the same filename, you should use the same name that the script has in the
DataNitro toolbar, e.g. "test.py (1)".
21
def f(x):
return 3*x
ActiveCell.value = "=f(3)"
or this:
ActiveCell.value = "=f(A1)"
If you want to use a DataNitro UDF inside VBA, you need to run it. To store the
value of the UDF in x, we do this:
x = Application.Run("f", 3)
or this:
input.
through VBA; DataNitro never interacts with it unless you're working with VBA.
In the VBA editor, the properties tab lists the name as name, and the
codename as (name).
For example, if you have a sheet with the name "James Bond" and the
codename "Double07", it'll be listed in Excel as "James Bond". You can write
to it from DataNitro by calling
Cell("Double07", "A1").value = 3
will give an error.
In VBA, this will work:
VBA("Double07.DrNo")
but
VBA("James Bond.DrNo")
will fail.
If the subroutine "Goldfinger" is defined in Module1, and you want to run it against
the sheet,
VBA("Goldfinger", [ ], "Double07")
will fail.
23