Virtual Columns
Create new columns by applying custom data transformation logic.
Virtual Columns are one of CSVbox’s most powerful data transformation tools.
You can run small snippets of JavaScript that can merge, split, re-format, correct, and enrich incoming data during a data import.
Super useful if you need to process your data, do calculations, merge things together, etc before receiving it at your end. The sky is the limit!
How it works
For any sheet (template), add a Virtual Column via the CSVbox dashboard.
Attach a Javascript snippet to the Virtual Column.
After the users submit the CSV file, the Javascript will run to populate data in the Virtual Columns.
Data from the uploaded CSV as well as the Virtual Column will be pushed to the data destination.
Example
Merge data from the first_name and the last_name columns into a new Virtual Column named full_name.
Here is the attached Javascript for the full_name Virtual Column:
Adding Virtual Columns
Go to the edit sheet page > Columns tab > Click Add Virtual Column button.
Add Virtual Column Name.
Provide Javascript code.
Attach Dependent libraries (optional).
Click Save.
Data Rows
Data rows are the core object that Virtual Columns are built around. For each incoming CSV data row, you can read its data and manipulate it via Javascript to add to the Virtual Column.
After the user submits the CSV, the Javascript will run for each data row in sequential order to populate Virtual Columns.
The csvbox.row
object is available to work with each row. You can use csvbox.row["column_name"]
to read the specific column of the data row. Utilize data from a single column, multiple columns, or no columns to process via Javascript and populate Virtual Columns.
Inserting Data into Virtual Columns
The value returned by the Javascript snippet is added to the Virtual Column.
If Javascript returns a NULL
value, an empty string will be added to the Virtual Column.
Receiving Virtual Data
The Virtual Column data is available at all the data destinations along with the data from the regular columns.
In the API response JSON object, the dynamic data will be displayed inside the _virtual_data object as shown below. The _virtual_data object will be visible only if the Virtual Columns are added to the sheet. In the example below check lines 12 and 31.
The virtual data will also be available on the client-side JSON object.
Dependencies
In the Javascript snippet, you can utilize an external library that is hosted via a CDN. You can also call any external API endpoint to fetch real-time data. Simply add the library script tag and/or any custom scripts to the 'Dependencies' section and start using it in the main Javascript snippet.
For each import, the dependent scripts will be run only once. Whereas the main Javascript snippet will be executed once for each row.
Variables
In the Virtual Column Javascript snippet, you have access to data variables included in the csvbox
object. The following data is available:
csvbox.row
csvbox.row
It contains row data. Each cell in the row can be accessed by providing the column name. Examples:
The column name must exist on the sheet or an error will be thrown.
csvbox.user
csvbox.user
It contains the custom user attributes defined while initializing the importer. Examples:
csvbox.import
csvbox.import
This refers to the current import-specific data. The following data is available:
csvbox.virtual
csvbox.virtual
It contains data from the preceeding virtual columns.
Data from only the virtual columns that are defined before the current virtual column are available for use.
console.log(csvbox);
With this statement, you can print all the available variables in the debugging console,
Last updated