Command Reference : Working with Tables and Spreadsheets : Customizing Tables
  
Customizing Tables
 
Column Width and Row Height
Cell Formatting
Content Formatting
Justification and Indentation
Fonts
Background and Borders
Cell Annotation and Merging
Conditional Table Cells
Target Range Specification
Boolean Expression
Basic Comparisons
Comparisons using Expressions
Find Expression
EViews provides considerable control over the appearance of table objects, providing a variety of table procedures allowing you specify row heights and column widths, content formatting, justification, font face, size, and color, cell background color and borders. Cell merging and annotation are also supported.
Column Width and Row Height
We begin by noting that if the contents of a cell are wider or taller than the display width or height of the cell, part of the cell contents may not be visible. You may use the Table::setwidth and Table::setheight table procedures to change the dimensions of a column or row of table cells.
To change the column widths for a set of columns in a table, use the setwidth keyword followed by a column range specification in parentheses, and a desired width.
The column range should be either a single column number or letter (e.g., “5”, “E”), a colon delimited range of columns (from low to high, e.g., “3:5”, “C:E”), or the keyword “@ALL”. The width unit is computed from representative characters in the default font for the current table (the EViews table default font at the time the table was created), and corresponds roughly to a single character. Width values may be non-integer values with resolution up to 1/10 of a unit. The default width value for columns in an unmodified table is 10.
For example, both commands
tab1.setwidth(2) 12
tab1.setwidth(B) 12
set the width of column 2 to 12 width units, while the command
tab1.setwidth(2:10) 20
sets the widths for columns 2 through 10 to 20 width units. To set all of the column widths, use the “@ALL” keyword.
tab1.setwidth(@all) 20
Similarly, you may specify row heights using the setheight keyword, followed by a row specification in parentheses, and a desired row height.
Rows are specified either as a single row number (e.g., “5”), as a colon delimited range of rows (from low to high, e.g., “3:5”), or using the keyword “@ALL”. Row heights are given in height unit values, where height units are in character heights. The character height is given by the font-specific sum of the units above and below the baseline and the leading in the default font for the current table. Height values may be non-integer values with resolution up to 1/10 of a height unit. The default row height value is 1.
For example,
tab1.setheight(2) 1
sets the height of row 2 to match the table default font character height, while
tab1.setheight(2) 3.5
increases the row height to 3-1/2 character heights.
Similarly, the command:
tab1.setheight(2:7) 1.5
sets the heights for rows 2 through 7 to 1-1/2 character heights.
We may set the row height conditionally. The command
tab1.setheight(2:7 if [a2:a7]>0.5) 1.5
will set the heights of row 2 through 7 to 1-1/2 character heights if the additional condition is true. In this case, if the value of cell A2 is greater than 0.5, the height of row will be set to 1.5 character. If the value of cell A3 is greater than 0.5, the height of row 3 will be set to 1.5 character and so forth through row 7. For more information on using table ranges and expressions see “Conditional Table Cells”.
Lastly the command,
tab1.setheight(@all) 2
sets all row heights to twice the default height.
Earlier versions of EViews supported the setting of column widths using the setcolwidth command. This command, which is provided for backward compatibility, offers only a subset of the capabilities of Table::setwidth.
Cell Formatting
A host of cell characteristics may be set using table procedures. Each procedure is designed to work on individual cells, ranges of cells, or the entire table.
Content Formatting
Cell content formatting allows you to alter the appearance of the data in a table cell without changing the contents of the cell. Using the table proc Table::setformat, you may, for example, instruct EViews to change the format of a number to scientific or fixed decimal, or to display a date number in a different date format. These changes in display format do not alter the cell values.
To format the contents of table cells, simply follow the table name with a period and the setformat proc keyword, followed by a cell range specification in parentheses, and then a valid numeric or date format string. The cell range may be specified in a number of ways, including individual cells, cell rectangles, row or column ranges or the entire table. See Table::setformat for a description of cell range specification and numeric and date format string syntax.
For example, to set the format for the fifth column of a matrix to fixed 5-digit precision, you may provide the format specification:
tab1.setformat(e) f.5
To set a format for the cell in the third row of the fifth column to scientific notation with 5 digits of precision, specify the individual cell, as in:
tab1.setformat(3,e) e.5
tab1.setformat(e3) e.5
To specify the format for a rectangle of cells, specify the upper left and lower right cells in the rectangle. The following commands set cells in the same region to show 3-significant digits, with negative numbers in parentheses:
tab1.setformat(2,B,10,D) g(.3)
tab1.setformat(r2c2:r10c4) g(.3)
tab1.setformat(b2:d10) g(.3)
The rectangle of cells is delimited by row 2, column 2, and row 10, column 4.
To conditionally set the format of cells, you can provide a conditional cell range argument. For example, using the following 2 commands in sequence
tab1.setformat(r2c2:r10c4 if [r2c2:r10c4]<10000) g(.2)
tab1.setformat(r2c2:r10c4 if [r2c2:r10c4]<1000) g(.3)
will set the format of cells row 2, column 2, to row 10, column 4 based on their cell values. If the cell values are less than 10000 only 2 significant digits will be displayed but if the cell values are less than 1000, 3 significant digits will be displayed. For more information on using table ranges and expressions see “Conditional Table Cells”.
Alternately you may provide a date format for the table cells. The command:
tab1.setformat(@all) "dd/MM/YY HH:MI:SS.SSS"
will display numeric values in the entire table using formatted date strings containing days followed by months, years, hours, minutes and seconds, to a resolution of thousandths of a second.
Note that changing the display format of a cell that contains a string will have no effect unless the cell is later changed to contain a numerical value.
Justification and Indentation
The cell justification and indentation control the position of the table cell contents within the table cell itself.
You may use the Table::setjust proc to position the cell contents in the cell. Simply use the setjust keyword, followed by a cell range specification in parentheses, and one or more keywords describing a vertical or horizontal position for the cell contents. You may use the keywords auto, left, right, and center to control horizontal positioning, and top, middle, and bottom to control vertical positioning. You may use the auto keyword to specify left justification for string cells and right justification for numeric cells.
For example,
tab1.setjust(@all) top left
sets the justification for all cells in the table to top left, while
tab1.setjust(2,B,10,D) center
horizontally centers the cell contents in the rectangle from B2 to D10, while leaving the vertical justification unchanged.
In addition, you may use Table::setindent to specify a left or right indentation from the edge of the cell for cells that are left or right justified, respectively. You should use the setindent keyword followed by a cell range in parentheses, and an indentation unit, specified in 1/5 of a width unit. Indentation is only relevant for non-center justified cells.
For example:
tab1.setjust(2,B,10,D) left
tab1.indent(2,B,10,D) 2
left-justifies, then indents the specified cells by 2/5 of a width unit from the left-hand side of the cell.
It may be useful to adjust the justification based on the contents of the cell. To change vertical justification based on the length of the string cell in the cell you could use a conditional range. For example,
tab1.setjust(b2:d10 if @length([b2:d10])>15) top
will individually set the vertical justification to be top justified for cells b2 through d10 if the cells contain a string longer than 15. For more information on using table ranges and expressions see “Conditional Table Cells”.
Alternatively,
tab2.setjust(@all) center
tab2.indent(@all) 3
will set the indentation for all cells in the table to 3/5 of a width unit, but this will have no effect on the center justified cells. If the cells are later modified to be left or right justified, the indentation will be used. If you subsequently issue the command
tab2.indent(@all) right
the cells will be indented 3/5 of a width unit from the right-hand edges.
Fonts
You may specify font face and characteristics, and the font color for table cells using the Table::setfont and Table::settextcolor table procs.
The setfont proc should be used to set the font face, size, boldface, italic, strikethrough and underline characteristics for table cells. You should provide a cell range specification, and one or more font arguments corresponding to font characteristics that you wish to modify. For example:
tab1.setfont(3,B,10,D) "Times New Roman" +u 8pt
changes the text in the specified cells to Times New Roman, 8 point, underline. Similarly,
tab1.setfont(4,B) -b +i -s
adds the italic to and removes boldface and strikethrough from the B4 cell.
To set the color of your text, use settextcolor with a cell range specification and a color specification. Color specifications may be provided using the @RGB or @HEX settings, or using one of the EViews predefined colors keywords:
tab1.settextcolor(f2:g10) @rgb(255, 128, 0)
tab1.settextcolor(f2:g10) orange
sets the text color for the specified cells to orange.
See Table::setfillcolor for a complete description of color specifications.
One practical text coloring application is to highlight negative cell values in a table by changing the text color. Using settextcolor, you may use a colormap (see “Value-Based Text and Fill Coloring”) or you can manually set the text color with a Boolean expression.
To set the text color of negative values in your table for the cell range A1:G10, you can either use a positive-negative colormap,
tab1.settextcolor(t=posneg, A1:G10) posclr(@RGB(0,0,0)) negclr(@RGB(255,0,0))naclr(@RGB(0,0,0))
or manually set the text color via
tab1.settextcolor(A1:G10 if [A1:G10]<0) red
For more information on using table ranges and expressions see “Conditional Table Cells”.
(Note that both commands set negative values to red. The difference between the two methods is colormaps dynamically determines the text color during rendering. Therefore, if the cell value is changed so will the color. Whereas when manually setting the text color, the color becomes fixed and will not change regardless of the cell value. While the use of colormaps is more flexible and dynamic, defining a colormap is more difficult. Manually setting the color is easier and faster and recommended when the cell values in the table are not expected to change.)
Background and Borders
You may set the background color for cells using the Table::setfillcolor table procedure. Specify the cell range and provide a color specification using @RGB or @HEX settings or one of the predefined color keywords. The commands:
tab1.setfillcolor(R2C3:R3C6) ltgray
tab1.setfillcolor(2,C,3,F) @rgb(192, 192, 192)
both set the background color of the specified cells to light gray.
The Table::setlines table proc may be used to draw borders or lines around specified table cells. If a single cell is specified, you may draw borders around the cell or a double line through the center of the cell. If multiple columns or rows is selected, you may, in addition, add borders between cells.
Follow the name of the table object with a period, the setlines keyword, a cell range specification, and one or more line arguments describing the lines and borders you wish to draw. For example:
tab1.setlines(b2:d6) +a -h -v
first adds all borders (“a”) to the cells in the rectangle defined by B2 and D6, then removes the inner horizontal (“h”), and inner vertical (“v”) borders. The command
tab1.setlines(2,b) +o
adds borders to the outside (“o”), all four borders, of the B2 cell.
You may also use the setlines command to place double horizontal separator lines in the table. Enter the setlines keyword, followed by the name of the table, and a row number, both in parentheses. For example,
bestres.setlines(8) +d
places a separator line in the eighth row of the table BESTRES. The command:
bestres.setlines(8) -d
removes the double separator lines from all of the cells in the eighth row of the table.
You may also apply a conditional expression when activating borders. Let’s assume we have a table that contains quarterly data by row, whereby the first column has the date (similar to a series spreadsheet) and we would like to visually indicate the starts and ends of years. We will accomplish this effect by applying a conditional expression to add a bottom border to the rows which contain the fourth quarter.
By default, quarterly workfile dates in EViews are denoted by year followed by quarter. The fourth quarter of the 2020 would appear as ‘2020Q4’. With this information, we can use the command:
tab1.setlines(a1:d50 if @right([a1:a50], 2)=="Q4")
This command will add, for each row from 1 to 50, a bottom border from column A to D if the first cell in their respective row ends in “Q4”. For more information on using table ranges and expressions see “Conditional Table Cells”.
Cell Annotation and Merging
Each cell in a table object is capable of containing a comment. Cell comments contain text that is hidden until the mouse cursor is placed over the cell containing the comment. Comments are useful for adding notes to a table without changing the appearance of the table.
To add a comment with the Table::comment table proc, follow the name of the table object with a period, a single cell identifier (in parentheses), and the comment text enclosed in double quotes. If no comment text is provided, a previously defined comment will be removed.
To add a comment “hello world” to the cell in the second row, fourth column, you may use the command:
tab1.comment(d2) "hello world"
To remove the comment simply repeat the command, omitting the text:
tab1.comment(d2)
In addition, EViews permits you to merge cells horizontally in a table object. To merge multiple cells in a row or to un-merge previously merged cells, you should use the Table::setmerge table proc. Enter the name of the table object, a period, followed by a cell range describing the cells in a single row that are to be merged.
If the first specified column is less than the last specified column (left specified before right), the cells in the row will be merged left to right, otherwise, the cells will be merged from right to left. The contents of the merged cell will be taken from the first cell in the merged region. If merging from left to right, the leftmost cell contents will be used; if merging from right to left, the rightmost cell contents will be displayed.
For example,
tab1.setmerge(a2:d2)
merges the cells in row 2, columns 1 to 4, from left to right, while
tab2.setmerge(d2:a2)
merges the cells in row 2, columns 2 to 5, from right to left. The cell display will use the leftmost cell in the first example, and the rightmost in the second.
If you specify a merge involving previously merged cells, EViews will unmerge all cells within the specified range. We may then unmerge cells by issuing the Table::setmerge command using any of the previously merged cells. The command:
tab2.setmerge(r2c4)
unmerges the previously merged cells.
When modifying a characteristic of a table via command, the section of the table to be modified must be specified. This section or range can be a set of cells, rows, or columns. You could for example the set fill color of row or column or rectangular area within the table. It may also be desirable to set remove a row or delete a column. EViews has numerous commands for customizing a table.
Conditional Table Cells
Many EViews table customization procedures allow you to specify ranges of cells. For example, Table::setfillcolor allows you to specify the background color for the specified cells, while Table::setformat allows you to apply custom formatting to the contents of the specified cells.
There are cases when you may wish to perform operations on cells in a table, but only under certain conditions. Suppose, for example, that you have a table with annual data on each row and you want to identify each row where there is a decrease in value in column 3 when compared to the value for the previous row (year). The goal is to apply custom text or cell formatting to these rows.
Fortunately, EViews allows you to identify the desired cells using a conditional target range specification comprised of a target_range and a boolean_expression. You may specify the conditional target cells using the syntax:
target_range if boolean_expression
where target_range gives the range of potential cells of interest, “if” is a keyword, and boolean_expression provides indicators for which of the target_range cells to use.
Similarly, the table @find data member ( “Table Data Members”) allows you to obtain a string list containing the cells in a table range that satisfy a condition.
The syntax for @find requires a find_expression:
table_name.@find(find_expression)
where find_expression simultaneously specifies the range of cells to consider and the boolean expression for whether each cell should be used.
Note that in contrast to the conditional target syntax above, @find does not require an explicit target_specification since the find_expression implicitly defines the range of cells to consider.
Target Range Specification
The target_range of potential cells to consider consists of the rectangle of cells defined by first_cell and last_cell:
first_cell:last_cell
where first_cell and last_cell are cell identifiers (separated by “:”) specified using either a column letter and row number (e.g., “A2”), or using “R” (for row) followed by the row number followed by “C” (for column) and the column number (e.g., “R1C2”).
You may also use the special keyword “@all” to refer to every cell in the table.
It is useful to consider simple examples of an unconditional target_range in action. Suppose that we wish to set the fill color for a collection of cells. The command
tab1.setfillcolor(A2:A7) red
will set the fill color of all the cells in the target region from A2 (top left cell) to A7 (bottom right cell) in table TAB1 to red. In this case, the target cell range is “A2:A7” and EViews will set the fill color for every cell from A2 to A7 to red.
Similarly,
tab1.setfillcolor(@all) red
sets the background color in all of cells of the table to red.
Boolean Expression
The boolean_expression defines a comparison producing indicators corresponding to the elements of the target_range.
Basic Comparisons
The boolean_expression syntax consists of
left_spec “operator” right_spec
where “operator” is a standard EViews comparison operator such as “>”, “<>” “<=”, “=”, etc., and where the left_spec and right_spec consist of one of:
a literal value, e.g., the number 37, or the string “Kansas” or “New Mexico”.
a cell range, enclosed in “[]”, e.g. “[A1:C9]”, “[R1C1:R10C30]”.
where for purposes of the boolean comparison, the cell range is refers to the contents of the corresponding cells in the table.
For example, for a target_range of size we may specify the boolean_expressions:
[A1:C5] < 3
[A1:C5] = [B1:B5]
[C2] > [A1:C1]
[E1:E5] <> "Out of Stock"
where we see that the specs can be rectangles, columns, and rows of cells, literal numbers, and literal strings.
Conceptually, EViews takes the left_spec and creates an implicit table of values that is the same size as target_range, then compares it to a similarly constructed table of right_spec values. The pairwise comparison of the cells of the two implicit tables produces the boolean indicators for the conditional target range.
The rules for forming the implicit tables are straightforward:
if the spec is a numeric or string literal, or a single cell, the value will be repeated for each element of the implicit table
if spec is a row of cells, it must have the same number of columns as target_range; the values in spec will be repeated for each row of the implicit table
if the spec is a column of cells, it must have the same number of rows as target_range; the values in spec will be repeated for each column of the implicit table
if the spec is a rectangle of cells, it must be of the same dimension as target_range; the implicit table uses the values in spec
These concepts are most easily illustrated via examples, in which we assume a target_range.
The boolean comparison,
[A1:C5] < 3
compares every element of
the rectangle of the values in the table cells from A1 to C5
(an implicit table of) the number 3
producing a TRUE if an element of the former is less than the latter, and a FALSE otherwise.
[A1:C5] = [B1:B5]
compares every element of
the rectangle of the values in the table cells from A1 to C5
an implicit table constructed by repeating horizontally the column of values in B1 to B5
producing a TRUE if an element of the former is equal to the latter, and a FALSE otherwise.
[C2] > [A1:C1]
compares every element of
(a implicit table filled with the value in C2
an implicit table constructed by repeating vertically the row of values in A1 to C1
producing a TRUE if an element of the former is greater than the latter, and a FALSE otherwise.
[E1:E5] <> "Out of Stock"
compares every element of
the implicit table filled with the column of valuesin E1 to E5 repeated horizontally
(an implicit table of) the string value “Out of Stock”
producing a TRUE if the element of the former is not equal to the latter, and a FALSE otherwise.
We can illustrate the use boolean expressions by modifying our earlier command that setfillcolor using an unconditional target_range. Suppose that we add an “if” condition, for whether the cells are decreasing in value, to the earlier command. The command
tab1.setfillcolor(A2:A7 if [A2:A7]<[A1:A6]) red
will only set the fill color to red for the target cell range, if the decreasing value condition holds.
Comparisons using Expressions
Earlier, we saw how to use the boolean_expression to define simple comparisons of cell values and literals ( “Basic Comparisons”).
EViews expression and function language can be used with the numeric and string literals in and cell expressions in left_spec and right_spec. We may have quite complicated boolean expressions as in:
[A1:C5]^2+log(@pi*[A1:C5]) < 3
[A1:C5]/3 = [B1:B5]
@tdist([C2], [C4]) > [A1:C1]/100
@length([E1:E5]) <> "Out of Stock"
where a function or expression involving a cell range is interpreted as applying the function or expression to each element of the range after expansion into implicit tables.
For the most part, interpreting the expressions is straightforward, as we can simply evaluate the expressions for each element of the implicit tables. The power of this type of evaluation can, however, lead to potentially useful but at first glance odd specifications, as in
tab1.setfillcolor(A1:B2 if @log([A1:A2]^2+[A1:B1])/10 < @floor(3+[C1:D2])) red
While interpreting this expression without invoking the concept of implicit tables is quite difficult, in practice, evaluation is reasonably straightforward:
1. the target_range is “A1:B2” which defines a target and size for the implicit tables
2. “[A1:A2]” is a row range which is repeated vertically to yield a implicit table; each element of the table is squared
3. “[A1:B1]” is a column range which is repeated horizontally to yield a implicit table; each element is divided by 10
4. the results in the previous two steps are added together cell-by-cell to yield a implicit table; the natural logarithm is taken of each cell
5. “[C1:D2] is a range; the number 3 is added to the values in the table for this range, forming a implicit table; the floor function is used to find the nearest integer less than, for each cell.
6. the elements of the two implicit tables are compared element-by-element, with TRUE given if the element of the first is strictly less than than the element of the second.
Find Expression
The find_expression is a conditional table cell specification in which the set of cells to consider is part of the boolean expression. The syntax for this expression is:
left_spec “operator” right_spec
where “operator” is a standard EViews comparison operator such as “>”, “<>” “<=”, “=”, etc., and where the left_spec and right_spec consist of one of:
a literal value, e.g., the number 37, or the string “Kansas” or “New Mexico”.
a cell range, enclosed in “[]”, e.g. “[A1:C9]”, “[R1C1:R10C30]”.
Most of the discussion in “Boolean Expression” applies here, with the only difference being that the target range will be the larger of the left_spec and right_spec ranges.
For example,
string s = tab1.@find("[b1:c15]>0.3")
returns a list of cells between B1 and C15 greater than 0.3,
string s = tab1.@find("[@all]=0.5")
returns a list of cells in the table equal to 0.5,
string s = t.@find("[b3:c5]<[d5]")
returns a list of cells between B3 and C5 less than cell D5.
Note that since the find_expression must be enclosed in double-quotes inside the @find function, the use of string literals will require “""” escape sequences to include the actual quotes in the comparison.
string s = tabl1.@find("[a1:e67]= ""1949q4""")
returns a list of cells between A1 and E67 matching the string “1949q4” (string comparisons in find ignore case);
string s = t.@find("@instr([@all],""in"")")
returns a list of cells in the table containing the substring “in”;