Formatting and Styling Tables

Prev Next

Introduction

B4P provides a generic set of functions for giving a final touch to your tables. They include:

  • Setting row widths and column hights
  • Formatting text (font name, font size, color, bold, underscore, line wraps, etc.)
  • Formatting tables (cell colors, etc.)
  • Number and date formats
  • Freezing panes
  • Setting the autofilter
  • and many more.

At present, the formatting and styling functions are supported for following file formats when saving the tables where minor differences in the interpretation of the formatting may apply:

  • HTML (understood by both Excel and web browsers)
  • Excel (XML 2003) format

You can add formatting and style to multiple tables and then save them as multiple sheets in one Excel file.

Note: The whole function library has been written in B4P and is in the file "Style Library.b4p" in your installed library directory. In order to use these functions, you need to do the following function call at first:

include ( Style Library );

Formatting Procedure

Follow the seven basic steps described below in order to do a successful and attractive finish .

1. Finalize the table with all necessary contents. You may want to have some helper columns on the right hand side which you can delete after adding formatting features, but the formatted table contents must not move in any direction because the formatting info will not move along with them.
2. A broad pallette of named colors is already available. Optionally, you can define user-specific colors so you can reference them conveniently by color name and not by red-green-blue codes:
add color(): Add user-defined colors
lighten colors(): Lighten the set of standard colors according to your specifications
darken colors(): Darken the set of standard colors according to your specifications
weaken colors(): Weaken the intensity of standard colors according to your specifications
3. Be creative. Do the artistic work on the table using the following four functions:
table style table(): Formatting instructions applicable for the whole table, e.g. general font name, font size, location of autofilter row, etc.
table style rows(): E.g row height, formatting all entries in the specified rows, etc.
table style columns(): E.g. column width, formatting all entries in the specified columns, etc.
table style cells(): Style individual cells, a set of cells, a matrix of cells, or a rectangular area in the table, and applying Excel mouse-over comments.
3a. Or use more convenient approaches:
table style auto width() to set the column widths automatically so all contents are fully visible and not cut off by the next columns
table style theme() to apply an overall style them on the table, applying formatting on the header and data rows with frames, colors, and much more.
4. If needed, delete redundant contents on the right hand side or below the table but without moving the final table around (e.g. with inserting / deleting columns and/or rows. The formatting you have applied have a fixed association to the rows and columns. If you delete a row, then the next row below will get the formatting. The same applies to sorting and rearranging table rows and columns after the formatting.
General advice: Finalize the table first, then do the creative artwork.
5. Generate the final formatting instructions specific to the file format you intend to use for saving. You need to call one of the two functions listed below
translate style attributes for excel xml 2003() - Excel legacy format
translate style attributes for html() - HTML format for web browsers and Excel
translate style attributes for excel() - Excel only: This function will be called automatically when you save the file, so you can skip calling this function explicitly.
6. Save your (art-)work using one of the following functions:
table save(), and
table save multiple() to save multiple sheets in 1 file,
table save excel file() to save one or multiple sheets in 1 contemporary Excel file. Note: Make sure you choose the format as specified in step 5. You can do the style attribute translation for multiple formats on the same table. If you save the file in a format for which no tranlsation has been made, then the table will be saved as if no formatting has been applied at all. The CSV file format and close relatives (e.g. tab separated files, text files) do not support any formatting.
7. If needed, reset all format attributes using the function
table style reset()
in order to start doing a different formatting artwork on the same table.

Important Info

The "Style Library" works with following global variables which shall not be manipulated directly as this could result in unexpected and erratic outcome

  • table style descriptions[]: This variable contains all formatting attributes for all tables.
  • hex[]: Conversion of hexadecimal values
  • table style rules[]: Defines rules to properly interpret formatting codes
  • table style html comments[]: Template to put comments into HTML file format so opening them with Excel will make the comments show up.

In addition, this library maintains one table called global color table which describes all available colors with their names. See next section for details.


Procedures and Functions Provided:


Color functions:
    add color
    rgb
    darken color
    lighten color
    weaken color
    lighten colors
    darken colors
    weaken colors

Apply style on tables:
    table style table
    table style rows
    table style columns
    table style cells
    table style auto width
    table style theme

Translate applied style to target file format:
    translate style attributes for excel
    translate style attributes for excel xml 2003
    translate style attributes for html

Remove all style attributes from the table:
    table style reset