-
-
Notifications
You must be signed in to change notification settings - Fork 36
Data type handling
Spreadsheet cells store their data as one of the following types: STRING
, NUMERIC
, BOOLEAN
, BLANK
and FORMULA
(date values are stored as the number of days since 1 January 1900 and are therefore NUMERIC
).
When adding data to workbooks using the Spreadsheet Library, the cell type is determined in one of two ways by default.
-
If you are adding data as a list or an array the cell type will be auto-detected from the individual value being set. So if the value of row 1 column 1 is
5
, the cell type will be set asNUMERIC
. If row 2 column 1's value is "Frumpo McNugget", the cell type will beSTRING
. -
If you are adding data by passing in a query object, then the library will attempt to set the cell types based on the query's column types, such as
VarChar
,Integer
,Timestamp
etc.
If you are passing in a query, but you do not want the cell types in each column to be determined by the query column types, then you can use the ignoreQueryColumnDataTypes
argument to force all values to be auto-detected.
data = QueryNew( "Number,Date", "VarChar,VarChar", [ [ "1234", "2020-08-24" ] ] );
spreadsheet.addRows( workbook=workbook, data=data, ignoreQueryColumnDataTypes=true );
ignoreQueryColumnDataTypes
is available to any method which accepts data as a query object.
If you want more fine grained control over cell types, you can use the datatypes
argument to specify how types should be set for particular columns, overriding the auto-detected and query column determined types.
For example, say you have an array of data with a column containing numbers, some of which have leading zeros. The auto-detection will set the cell type for the leading zero numbers to be STRING
, so that the zeros are preserved. But if you really want the values treated as numbers with the leading zeros stripped, you could specify this as follows:
data = [ [ "01234" ], [ "05678" ] ];
spreadsheet.addRows( workbook=workbook, data=data, datatypes={ numeric: [ 1 ] } );
Another example: say your data is a query object and you are happy with most of the query column types, but there is a BigInt
column called "LongNumber" that you don't want to fall victim to Excel's 15 digit limit. You can force just that column's type to be overridden and be a string as follows:
data = QueryNew( "Firstname,Lastname,LongNumber", "VarChar,VarChar,BigInt", [ [ "Frumpo", "McNugget", 1234567890123456 ] ];
spreadsheet.addRows( workbook=workbook, data=data, datatypes={ string: [ "LongNumber" ] } );
The datatypes
argument is available to any method which allows data to be added in rows to a workbook.