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.
Add Virtual Column
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.
Virtual Columns will be available in the column mapping modal. You can push Virtual Columns to any receiving data field of your choice.
Mapping Virtual Columns
In the AWS S3 file store, the Virtual Columns will be added as new columns in the uploaded file.
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.
VIrtual Column Processing
Example
Consider you want to use the data from the incoming USD_amountcolumn to create a new GBP_amount Virtual Column. The currency amount should be converted from USD to GBP using real-time exchange rates.
We can utilize the Money.js library for currency conversions and Open Exchange Rates for fetching real-time conversion rates. Here is the sample dependency code:
Corresponding Javascript snippet for GBP_amount Virtual Column:
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
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
It contains the custom user attributes defined while initializing the importer. Examples:
csvbox.import
This refers to the current import-specific data. The following data is available:
csvbox.virtual
It contains data from the preceeding virtual columns.
csvbox.environment
It contains the environment variables that are passed during importer initialization.
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,
//Load jQuery via CDN
<script src="https://code.jquery.com/jquery-3.6.1.min.js" integrity="sha256-o88AwQnZB+VDvE9tvIXrMQaPlFFSUTR+nldQm1LuPXQ=" crossorigin="anonymous"> </script>
//Load Money.js via CDN
<script src="https://cdnjs.cloudflare.com/ajax/libs/money.js/0.0.1/money.min.js" integrity="sha512-dfZQaBBlTXvL+AUQKi7dd/9kb/KhreymnZYVdinjigqTdZUrAnLUNJRV34DUPFCdyek9mMBns3rzTlipnBKhTg==" crossorigin="anonymous" referrerpolicy="no-referrer"> </script>
//Fetch real-time conversion rates via ajax call and initialize fx object
<script type="text/javascript" >
// Load exchange rates data via AJAX:
$.getJSON(
// NB: using Open Exchange Rates here, but you can use any source!
'https://openexchangerates.org/api/latest.json?app_id=fa4744d77e844adb9cc3533b7ae162f3',
function(data) {
// Check money.js has finished loading:
if (typeof fx !== "undefined" && fx.rates) {
fx.rates = data.rates;
fx.base = data.base;
} else {
// If not, apply to fxSetup global:
var fxSetup = {
rates: data.rates,
base: data.base
}
}
}
);
</script>