-
-
Notifications
You must be signed in to change notification settings - Fork 36
read
Julian Halliwell edited this page Nov 19, 2024
·
28 revisions
Reads a spreadsheet file into one of the following:
- a spreadsheet object;
- a CFML query object;
- a HTML string;
- a CSV string.
read( src [, format [, columns [, columnNames [, headerRow [, rows [, sheetName [, includeHeaderRow [, includeBlankRows [, fillMergedCellsWithVisibleValue [, includeHiddenColumns [, includeHiddenRows [, includeRichTextFormatting [, password [, csvDelimiter [, queryColumnTypes, [ makeColumnNamesSafe, [ returnVisibleValues ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] )
or
read( src [, format [, columns [, columnNames [, headerRow [, rows [, sheetNumber [, includeHeaderRow [, includeBlankRows [, fillMergedCellsWithVisibleValue [, includeHiddenColumns[, includeHiddenRows [, includeRichTextFormatting [, password [, csvDelimiter [, queryColumnTypes, [ makeColumnNamesSafe, [ returnVisibleValues ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] )
-
src
string: absolute path to the file to read
-
format
string: "query", "html" or "csv". If omitted, returns a spreadsheet object.- "html" returns the sheet data as a string containing a set of HTML table rows/columns, including the
<thead>
/<tbody>
tags, but excluding the<table>
start/end tags. - "csv" returns the sheet data as a CSV string with values delimited by commas and qualified by double-quotes. Rows are separated by new lines (CRLF).
- "html" returns the sheet data as a string containing a set of HTML table rows/columns, including the
-
columns
string: specify the columns you want to read as a comma-delimited list of ranges. Each value can be either a single number, a range of numbers with a hyphen, e.g. "1,2-5", or an open-ended range, e.g. "2-" (meaning all starting from 2). -
columnNames
string OR array: a comma-delimited list or an array of the names to use for the query columns in the order the columns appear in the spreadsheet. Note that specifyingcolumnNames
overrides the use of aheaderRow
for column names. Alias:queryColumnNames
. -
headerRow
numeric: specify which row is the header to be used for the query column names -
rows
string: specify the rows you want to read as a comma-delimited list of ranges. Each value can be either a single number, a range of numbers with a hyphen, e.g. "1,2-5", or an open-ended range, e.g. "2-" (meaning all starting from 2). -
sheetName
string: name of the sheet to read OR -
sheetNumber
numeric default=1: number of the sheet to read (1 based, not zero-based) -
includeHeaderRow
boolean default=false: whether to include the header row from the spreadsheet (NB: the default is the opposite to Adobe ColdFusion 9, which isexcludeHeaderRow=false
). -
includeBlankRows
boolean default=false: whether to include blank rows from the spreadsheet in the query data set. By default blank rows are suppressed. -
fillMergedCellsWithVisibleValue
boolean default=false: if the source sheet contains merged cells, set the value of each cell in the merged region to the visible value stored in the top/left-most cell -
includeHiddenColumns
boolean default=true: if set to false, columns formatted as "hidden" will not be included when reading into a query -
includeHiddenRows
boolean default=true: if set to false, rows formatted as "hidden" will not be included when reading into a query -
includeRichTextFormatting
boolean default=false: if set to true, basic font formatting of text within cells will be converted to HTML using<span>
tags with inline CSS styles. Only the following formats are supported:- bold
- color
- italic
- strikethrough
- underline
-
password
string: if supplied the file will be treated as encrypted and the password used to try and open it. -
csvDelimiter
string default=",": delimiter to use if reading the file into a CSV string. -
queryColumnTypes
string or struct: when reading a spreadsheet into a query, this allows you to specify the column types (see below for details). -
makeColumnNamesSafe
boolean default=false: ensure that the CSV column names are safe for use in the resulting query (duplicate free and valid CFML variable names). -
returnVisibleValues
boolean default=false: when reading into a query/csv/html, return values as they are visible/formatted in each cell rather than the "raw" values. This mostly affects numbers and dates, where you might for example wish to avoid scientific notation.
Chainable? Yes. If you specify a format
then the data will be returned and the chain will end.
When you specify query
as the format, by default no column types are used because spreadsheets have no such concept (only cells have types). If you wish to specify column types for the query you can use the optional queryColumnTypes
argument in one of four ways:
- Pass the list of types you want in exactly the same way as you would with the CFML function QueryNew()
- Pass a single type to apply to all columns. Normally you would specify
VARCHAR
. - Specify
auto
to have the types auto-detected from the values in each spreadsheet column. If they are all one type, that will be used. If they are mixed,VARCHAR
will be used. Blank cells are ignored. Note that auto-detection only supports the following types:VARCHAR
,DOUBLE
(numeric values) andTIMESTAMP
(date or datetime values). Any other types will be set toVARCHAR
. Be aware also that this option has a performance overhead which may be significant on large sheets. - Pass a struct which maps each column name to the desired type. Use the column name as the key and the type as the value (see example below). The names/values don't have to be in the same order as they appear in the sheet. Note that you must also either specify the
headerRow
in which to find the column names, or supply thecolumnNames
argument (see above). If a header/column is not found in the struct its type will default toVARCHAR
.
spreadsheet = New spreadsheet();
filepath = ExpandPath( "report.xls" );
myQuery = spreadsheet.read( src=filepath, format="query" );
spreadsheet = New spreadsheet();
filepath = ExpandPath( "report.xls" );
mySpreadSheetObject = spreadsheet.read( filepath );
columnTypes = { "text column": "VARCHAR", dates: "DATE", times: "TIME" };
query = spreadsheet.read( src=filepath, format="query", queryColumnTypes=columnTypes, headerRow=1 );