Forum Discussion

_anomDiebolt_'s avatar
_anomDiebolt_
Qrew Elite
11 years ago

Got CSV To Import?

I wanted to show you a simple way to read a CSV file and process it before sending it to QuickBase. Normally when a file input control is on a form you select a file from the local file system and the file gets sent directly to the server when the form posts. However, with the HTML5 File API you can actually read the selected file, manipulate the contents and send the results to the server through an AJAX call so the host page never reloads.

To simplify the procedure even further I wrote a carefully crafted piece of code to make the process dead simple and allow the greatest level of customization for integration with QuickBase. Here is the code:

Pastie Database

https://haversineconsulting.quickbase.com/db/bgcwm2m4g?a=dr&rid=280

In a nutshell all you have to do is place a file control on your page and give it an id such as myFile shown below:

<input type="file" id="myFile" accept=".csv">

After a file is selected by the user, the CSV data will magically show up and can be accessed through the variable csv in the following short snippet of code where the same id is passed to GotCSV():

gotCSV("myFile").then(function(csv){

//your code here

console.log(JSON.stringify(csv, null, " "));

});

So if the CSV file contained the following data:

name,phone,email

John,(800) 555-1212,john@doe.com

Jane,(888) 555-1212,jane@doe.com

Baby,(877) 555-1212,baby@doe.com

You will be able to access the csv variable with properties corresponding to the column names:

[

{

"name": "John",

"phone": "(800) 555-1212",

"email": "john@doe.com"

},

{

"name": "Jane",

"phone": "(888) 555-1212",

"email": "jane@doe.com"

},

{

"name": "Baby",

"phone": "(877) 555-1212",

"email": "baby@doe.com"

}

]

Once you have the CSV represented as a JavaScript object you can go to town manipulating it. It now becomes a trivial matter to transform or filter the incoming CSV data prior to importing it into QuickBase.

Notes:

(1) The function gotCSV() wraps the two asynchronous processes (a) of the user selecting a local file and (b) reading the local file using the HTML5 File API and returns a promise.

(2) When the promise resolves the data in the selected file will be converted to an object and available as the JavaScript variable csv.

(3) This code is very generic and will allow you a perform a wide variety of tasks such as:

  • cleaning up dirty data prior to import
  • normalizing data
  • removing duplicate data
  • prevent importing a redundant data
  • converting quantities prior to import
  • supplying default data prior to import
  • split the raw CSV data into parent/child components prior to import

(4) to use the code the only thing you have to do is place your custom code where the comment "your code here" resides. The promise returned by gotCSV() will insure that your code automatically runs as soon as the file is selected and read by the HTML5 File API.

(5) This code should work in all evergreen browsers including IE11:

http://caniuse.com/fileapi

(6) It may take some time to grok what this code is doing but I assure you this is as extremely powerful technique that can probably greatly streamline your workflow if implemented.

(7) Enjoy!

21 Replies

  • >I do spend lot of time with language syntax cause sometimes things �obvious for the profs, are not so obvious for the newbies.

    Run your code through a linter such as JSLINT or JSHINT to catch errors, typos and deviations from best coding practices:

    JSLINT
    http://www.jslint.com/

    JSHINT
    http://www.jshint.com/

    Or use the linter built into jsfiddle.net (JSHINT) or a linter plugin of your favorite editor editor. The free text editor notepad++ has a JSLINT plugin

    Notepad++
    http://notepad-plus-plus.org/

    JSLINT Plugin for Notepad++
    http://sourceforge.net/projects/jslintnpp/
    If you post code use a pastie or pastebin.com to get syntax highlighting and indexing appropriate for the language.
    Regarding your CSV question, post a new message because it is difficult to separate your issue from the original question and I need the extra forum points to win the all expenses paid trip to Hawaii QuickBase is offering.
  • Hi Dan, Apologies for raising it in the same thread as it is related to the same example which i was trying. it works good, but i have 2 small queries , which i have no clue how to handle.
    1)- How can I encode the columns as in some cases, the data contains special characters. Since my data has text columns with special characters, my data gets split and goes in multiple columns rather than the column i intend to. is there a encode or escape character i can use ?
    2)- How can i import user email address into user fields. As of my know my feed has a user column which has user's email address, which i thought will automatically assign to the user field. I presumed this from my experience on using Quickbase API _AddRecord which automatically assigns to user based on the email address, but somehow this is not happening ?

    Thanks a lot.
    dinesh
  • I am short time so I can't scan the entire thread. If you are importing CSV there aren't many special characters to worry about. All you have to do is surround the entire column value with double quotes and if there is a double quote within the column value just substitute two quotes. So an sentence field might originally look like this

         He said "call me on Monday" so I did just that.

    and you would "escape" it for CSV like this:

         "He said ""call me on Monday"" so I did just that."

    On other matters ask a new question. I am going for the pointage!
  • KellyBianchi's avatar
    KellyBianchi
    Qrew Assistant Captain
    Dan,
    I would like to apply this, but I may need more assistance. Can you provide me with your business contact info? 
  • All sorts of confused here...what code would go in the //your code here?

    gotCSV("myFile").then(function(csv){

    //your code here

    console.log(JSON.stringify(csv, null, " "));
    • AustinK's avatar
      AustinK
      Qrew Commander
      There is nothing there because it depends on what you would like to do with the data. Up top in the first post under the Notes section they give an overview of how you might use this.

      What are you wanting to do with your data?
    • JoseBaez's avatar
      JoseBaez
      Qrew Trainee
      I just want to append the data into my table from an html page. I tested the code and the file does import..except it created a bunch of unnamed fields. My guess is the code needs the field names.