2. Install Code

Add a snippet of code to your app to start accepting spreadsheets from your users.

Go to the 'Code' tab of the sheet and find the integration code. Place the code in your application at the location you want to display the import button.

Sample code with basic usage:

<button class="btn btn-primary" data-csvbox disabled onclick="importer.openModal();">Import</button>
<script type="text/javascript" src="https://js.csvbox.io/script.js"></script>
<script type="text/javascript">
    function callback(result, data) {
        if(result){
            console.log("success");
            console.log(data.row_success + " rows uploaded");
            //custom code
        }else{
            console.log("fail");
            //custom code
        }
    }
    let importer = new CSVBoxImporter("YOUR_LICENSE_KEY_HERE",{        
    }, callback);
    
    importer.setUser({
        user_id: "default123"
    })
</script>

Each sheet has a unique Licence Key. Find the Licence Key of the sheet on the 'Code' tab of the sheet page and pass it to the CSVBoxImporter function.

If your app/database is restricted to IP addresses on an allowlist, you will need to manually add CSVbox's addresses in order to use the importer.

You will have to whitelist the following IP addresses:

  • 18.233.84.183

  • 18.213.249.53

  • 3.73.26.144

Note that at any time, you will only see one of these addresses in use. However, the active IP address can change at any time, so you should add them all to your IP whitelist to ensure no interruptions in service.

Referencing the user

You can configure custom user attributes in the installation code to identify the users in your platform and match them with their respective imports.

Pass custom user attributes as input parameters to the setUsermethod. The custom user attributes will be pushed to your destination along with the uploaded data.

user_id is the only custom attribute that is mandatory. Apart from user_id, you can add up to 4 custom attributes in the<key>: <value>format. Example:

 importer.setUser({
        user_id: "1a2b3c4d5e6f",
        team_id: "sales2",
        isAuthenticated: "true",
        permissionLevel: "admin",
        email: "abc@example.com"
    })

Callback function

Once the user uploads a file the importer will return the status of the import along with metadata describing the completed import. Data is returned via two variables: result and data.

  1. result - It is of type boolean with the value true if the import is successful and false if the import fails.

  2. data - It returns JSON data as shown below:

  {
    "import_id": 79418895,
    "sheet_id": 575,
    "sheet_name": "Products Import",
    "destination_type": "webhook"
    "row_count": 100,
    "row_success": 98,
    "row_fail": 2,
    "import_status": "Partial",
    "import_starttime": 87987897897,
    "import_endtime": 90890890809,
    "original_filename": "example-1.csv",
    "raw_file": "https://file-download-link",
    "custom_fields": {
      "user_id": "Z1001"
    },
    "column_mappings": [
      { "Name": "Name" },
      { "SKU": "Product SKU" },
      { "Price": "Sale Price" },
      { "Quantity": "Inventory"},
      { "Image URL": "Img"}    
    ]
  }

The data object will be null if the import fails

You can also configure the importer to receive the entire file data in JSON format in the data object above. More information here.

You can write custom code to handle the success or failure conditions client side.

The result and datavariables will be available in the callbackfunction that is triggered when the import is completed.

function callback(result, data) {
        if(result){
            console.log("success");
            console.log(data.row_success + " rows uploaded");
            //custom code
        }else{
            console.log("fail");
            //custom code
        }
    }

Options

Here is the list of additional configuration options available with the CSVbox importer.

max_rows

  • Type: number

  • Default: null

  • Description:

    Specify the maximum number of rows that a single file can import. This value excludes the headers of the file. So if the number of rows in a sheet is 101, but the first row is the header, then this file would be considered to have 100 data rows in it.

max_rows_allow_submit

  • Type: boolean

  • Default: true

  • Description:

    Allow or disallow submission of the permissible number of rows when the overall row count exceeds the limit. Consider row limit (max_rows) is set to 5. If max_rows_allow_submit is set to true then the user can upload the top 5 rows of his file. If max_rows_allow_submit is set to false then the user will not be able to submit even 1 row if the overall row count exceeds max_rows.

max_rows_custom_message

  • Type: string

  • Default: null

  • Description:

    Display a custom message to the user when the file row count is greater than max_rows. This message will show only when max_rows_allow_submit is set to false.

min_rows

  • Type: number

  • Default: null

  • Description:

    The minimum number of rows that is required for a single upload.

min_rows_custom_message

  • Type: string

  • Default: null

  • Description:

    Display a custom message when the file row count is less than min_rows.

language

  • Type: string

  • Default: null

  • Description:

    Specify the importer frontend language. This value will override the default language option configured via the csvbox dashboard. Acceptable values are:

allow_invalid

  • Type: boolean

  • Default: 0

  • Description:

    It specifies if the users can continue to submit the file even if there are validation errors.

request_headers

  • Type: { key: value, key: value, ... }

  • Default: null

  • Description:

    This is where you define additional headers that get passed with each HTTP request.

dynamic_list_request_headers

  • Type: { key: value, key: value, ... }

  • Default: null

  • Description:

    This is where you define additional headers that get passed with each HTTP request for the dynamic list API.

sample_template_url

  • Type: String

  • Default: null

  • Description:

    It is the URL to download the sample CSV file for the end users.

sample_template_button_text

  • Type: String

  • Default: null

  • Description:

    It specifies the text on the button for downloading the sample CSV file.

The sample_template_url and the sample_template_button_text options can be used to provide the end users with a customized dynamic sample CSV file.

target_file_name

  • Type: String

  • Default: null

  • Description:

    It specifies the name of the file that gets pushed to your destination. This is applicable for the following destinations only:

    • s3

    • FTP Server

    • Google Sheets

Examples:

Pass additional options as input parameters to the setOptionsmethod.

importer.setOptions({
    max_rows: 50,
    language: 'de',
    request_headers: {
            "Content-Type": "application/json",
            "X-Access-Token": "71ab1d73a4d1319b260e9a0sdbdbc1c"
    },
    sample_template_url: 'https://files.myapp.com/user-18768',
    sample_template_button_text: 'Starter Template' 
})

Events

Here is the list of additional importer events/properties:

onReady

Triggers when the importer is initialized and ready for use by the users. The users can then click the Import button to open the Importer modal dialog.

onLoadStart

Triggers when the importer iFrame starts loading.

In vanilla Javascript, onLoadStart will trigger only when the Lazy Load is activated. Without Lazy Load, the iframe loading starts as soon as the importer is initialized.

onClose

Triggers when the importer is closed.

onSubmit

Triggers when the user hits the 'Submit' button to upload the validated file. data object is available in this event. It contains metadata related to the import.

onSubmit Data object sample
{
  "import_id": 79418895,
  "sheet_id": 575,
  "sheet_name": "Products Import",
  "destination_type": "webhook",
  "row_count": 100,
  "import_starttime": 87987897897,
  "original_filename": "example-01.csv",
  "custom_fields": {
    "user_id": "Z1001"
  },
  "column_mappings": [
    {
      "Item": "Product Name"
    },
    {
      "SKU": "Product SKU"
    },
    {
      "Price": "Sale Price"
    },
    {
      "Quantity": "Inventory"
    },
    {
      "Image URL": "Img"
    }
  ]
}

onImport

Triggers when all the data is pushed to the destination. Two objects are available in this event:

  1. result (boolean): It is true when the import is successful and false when the import fails.

  2. data (object): Contains metadata related to the import.

Examples:

<button class="btn btn-primary" data-csvbox disabled onclick="importer.openModal();">Import</button>
<script type="text/javascript" src="https://js.csvbox.io/script.js"></script>
<script type="text/javascript">
       function callback(result, data) {
           if(result){
               console.log("Sheet uploaded successfully");
               console.log(data.row_success + " rows uploaded");
           }else{
               console.log("There was some problem uploading the sheet");
           }
       }
       let importer = new CSVBoxImporter("YOUR_LICENSE_KEY_HERE",{}, callback);
       importer.setUser({
           user_id: 'default123'
       });
       
       importer.listen("onReady", function(){
        console.log("onReady");
       });
       
       importer.listen("onClose", function(){
        console.log("onClose");
       });
       
        importer.listen("onSubmit", function(data){
        console.log("onSubmit");
        console.log(data.import_id);
       });
       
</script>    

Other Settings

Lazy Load

The importer assets are loaded on the webpage load. This can sometimes slow the app if you initialize multiple importers on the same page. As a workaround, you can defer the loading of the importer assets to the Import button click. This can be done by adding the lazy: true parameter to the initialization code.

let importer = new CSVBoxImporter("YOUR_LICENSE_KEY_HERE",{        
    }, callback, { lazy: true });

Last updated