Export (table, format, output [, search, sort, dir, start, limit])

Result Type:  string

Definition:  Exports formatted data from the given table in the database. If there is no search parameter, you will get all records in the table, otherwise only those matching the search expression.

Using "=" as the search expression will export a single "record" containing just the field names for the file. The table name may include a sort specifier (e.g. "Name.Code-"); you can also sort by passing a comma, delimited string containing fields names to sort by and a sort direction (SortAscending or SortDescending).

To export with a particular format, use the format parameter. Everything in the format string is returned verbatim except for anything inside [...] which is treated as an expression which can reference the fields of the file being exported. Thus if you want tab-delimited, then put tabs between the expressions. You can use metacharacters \t \r \n \xHH (hex) or \\. e.g. "[Code],[Phone]\n".

In addition to the search expression for selecting the records to output, you can limit the range of records in this result that are output, This allows "pages" of results to be obtained (although you will incur the search and sort cost for each additional call even if only the start and limit counts change. No state is retained).

You can also export xml by specifying "xml" as the format. By default, the xml will be terse (also specifiable by "xml-terse" as format) in that any blank or zero fields will be omitted, as will system detail lines for transactions and system (non-importable) fields. To get all fields, specify "xml-verbose" as the format. In verbose format, system detail line records and non-importable fields will include a "system" attribute in their opening tag.

If the output parameter is not supplied, the data is sent to stdout, otherwise a file is created using the full path supplied and the data is written there. The path must be in a "safe" directory location (see File_Open). If you supply an output parameter and no format parameter, you will get the format equivalent to a manual export, otherwise you get all fields. Using a format parameter is strongly recommended, since default export formats are subject to change.

As a special case, an output path of "tmp" will create a temp file for you and return the path of that file.


export("account", "xml", "") exports all accounts as xml

export("product.code-", "[code]\t[description]\n", "prods.txt")

This writes tab-delimited products and descriptions to prods.txt in the MoneyWorks Automation directory.

export("transaction", "xml", "", "enterdate=today()", "sequencenumber", SortAscending, 0, 10)

returns the first 10 transactions entered today as xml.

Availability:  This function is available within MWScript handlers. Note that this function is exactly analogous to the command line and REST export commands. There is also a similar export command available to AppleScript and COM. Some of the apparent weirdness (like two ways of sorting) is down to over 20 years of backwardly-compatible evolution.