table save ...

Prev Next

Function Names

table save, table save with local decimal separator, table save with decimal comma, table save multiple

Description

This function saves 1 or more tables to a file. Following file formats are supported and can be specified in the 2nd function parameter:

CSV Comma separated files Uses list separation symbols according to locale settings.
Text files Plain text Text files can be saved by using 'new line' as list separator.
EXCEL MS Excel XML 2003 Format Excel compatible XML format which supports multiple tables in one file as well as styles. This format is obsolete.
Attention: Use table save excel file() instead to save your work in modern Excel fromat with fie type '.xlsx'
HTML HTML format Saves the table in a HTML file and supports styles. The HTML file can be opened with Excel and with web browsers.
JSON JavaScript Object Notation format Saves the table in a JSON file. Various options exist.
XML XML (extended markup) file (Not yet supported)

For CSV files, the following function names save the files differently:
table save with local decimal separator will use the decimal symbol for numbers as specified in the locale setting. It's a comma for settings related to Germany.
table save with decimal comma will always use the comma as decimal separator symbol.
table save will always use the point as decimal separator symbol.
Note: No thousand separators will be used.


table save multiple is supported with following formats: HTML, EXCEL, JSON arrays and JSON objects, but not with CSV and TEXT.

Call as: procedure or function

Restrictions

Indirect parameter passing is disabled

Parameter count

2-6

Parameters

No.TypeDescription
1.
input
set or string Name(s) of table(s)

Provide 1 table as string unless the function table save multiple is used.

- / 2.
input
set or string Sheet name(s)

Only applicable for table save multiple: For each table provided in the 1st function parameter, a corresponding sheet name needs to be provided. When saving in Excel format, these sheet names will appar as tab names at the bottom of the displayed worksheets.

2. / 3.
input
string File name

Specify the file name. Add the path in front if the file is not located in the current working directory.

Opt. 3. / 4.
input
string Format descriptor

Value Description
, CSV using comma as separator. (Put it into quotation marks ',' when specifying it in the function parameter)
; CSV using semicolon as separator. (Put it into quotation marks ';' when specifying it in the function parameter)
tab TSV using tab as separator (Do not use quotation marks here because tab is a reserved B4P keyword representing a single tab character)
CSV CSV using comma as separator
LOCAL CSV using decimal symbol according to locale settings, semicolon is several European countries, commas in N. America
1 character CSV format using any other single character, e.g. "/" as separator symbol
TEXT Text file. Same as if 'new line' specified as separator. Every table entry takes one line of text in the file.
HTML Saves the file as HTML file. The file can be opened with web browsers and Excel.
EXCEL Excel XML 2003 Format (obsolete). For the modern .xlsx format, use the function table save excel file() instead.
JSON arrays Save table(s) as 2-dimensional JSON arrays
JSON objects Save table(s) as 1-dimensional JSON arrays containing JSON object(s) where object names correspond to header names and object values to the data.
JSON lean Similar to jSON objects, but blank table contents are skipped.

Default value: LOCAL (CSV using decimal symbol according to locale settings)
Opt. 4. / 5.
input
string Character set

Value Character set
ansi Text format in ANSI- Non-ASCII symbols are converted to question mark, including foreign symbols and the Euro symbol (€)
iso8859-1 8-bit West European text format, but without symbols in 0x80 – 0x9F range which also excludes the Euro (€) symbol
win1252 Windows West European text format, incl. Euro (€) symbol
utf-8 UNICODE 8-bit format (supported by Excel)
utf-16 UNICODE 16-bit format (supported by Excel)
utf-16 big endian UNICODE 16-bit format (rarely used and not supported by Excel) Use only for data export to "Big Endian" based systems.

Attention: Put the values into quotation marks if they contain a hyphen, otherwise the hyphen is interpreted as a minus sign. Advice: If the file has an exotic character set, consider using "win1252" for full 8-bit character space, and interprete the character codes on your own.

Default value: utf-8 (UNICODE)
Opt. 5. / 6.
input
string Option

At present, one option is defined in addition to the default option:

default No specific option applies.
end without new line Applicable to simple formats (CSV, tab, TEXT, ...): Last row contains no concluding new line symbol.

Default value: default

Return value

TypeDescription
boolean Success

If not successul, then an exception has happened

Exceptions

Path not found
File not accessible
File locked
Syntax errors in table formats, e.g in HTML, JSON, XML
File locked. Close the file in the other application and retry

Examples

       table initialize( t, { { Object, Height in meters }, { dog, 0.5 }, { horse, 1.8 }, { elephant, 2.5 }} );

       table list ( t ); echo;
       table save( t, animals.csv, CSV );

       table load( t, animals.csv, ";" );
       table list ( t ); echo;

       table load( t, animals.csv, "," );
       echo("Table loaded with wrong separator symbol.  Outcome looks like this:");
       table list ( t ); echo;

       file delete silently( animals.csv );

Output

    0 : Object   | Height in meters
    1 : dog      | 0.5             
    2 : horse    | 1.8             
    3 : elephant | 2.5             


    0 : Object   | Height in meters
    1 : dog      | 0.5             
    2 : horse    | 1.8             
    3 : elephant | 2.5             


Table loaded with wrong separator symbol.  Outcome looks like this:
    0 : Object;Height in meters
    1 : dog;0.5                
    2 : horse;1.8              
    3 : elephant;2.5           


Try it yourself: Open LIB_Function_table_save.b4p in B4P_Examples.zip. Decompress before use.

See also

table save excel file
table load
Saving JSON files