What Happens When Excel Decides to Prettify Your Data For You When You Import it to Ruby on Rails (and More Reasons to Use .CSV Files and Not .XLSX Files)

You've fetched your data from an Excel sheet (.xlsx) using a rake task. You've added models, views, and controllers in Ruby on Rails to ensure that you can get and show your data in your React frontend. 

But uh-oh.... your image links aren't working!

Awkward Schitts Creek GIF By CBC


You first take a look at your excel file and the links look just like they should. 


Let's inspect the imported data through rails console:

    
    1 rails c

    2.7.4 :001 > Product.first.image1         Product Load (2.6ms) SELECT "products".* FROM "products" ORDER BY "products".
            "id" ASC LIMIT $1 [["LIMIT", 1]]          => "<html><u>https://i.etsystatic.com/23287327/r/il/2bde7a/3700353637/il_full
                xfull.3700353637_msew.jpg</u></html>"


Taking a closer look at your image links you discover something odd.

What happened here? Why are your links showing up with these strange <html><u> and </u></html> tags appended at the beginning and end? 

Unfortunately you are in a hurry to show your boss some deliverables today, they need your code so they can continue to move this project forward. You're definitely going to have to drop this messy table and re-seed your data. But that can wait until later. In the meantime you decide to make a quick fix to get the images to render so your team can keep working. Then when you have a working product to deliver (today is the due date after all!) you can swim back upstream and fix the issue at the source. 

One quick fix would be to call a JavaScript string method on this image link. 

<img src={image1.slice(9,-11)} alt="mala bead necklace"/>


Now back to the heart of the issue. Why did excel screw us over by adding those <html><u> tags? What is a <u> tag anyway? We refresh our memory with a quick google search (as all good developers do). 

Google It Jimmy Fallon GIF by The Tonight Show Starring Jimmy Fallon

Google reminds us that <u></u> tags are used to underline in HTML. 

What are we peasants using old school HTML now? Well, we're not! But Microsoft Excel is! 

Yes that's right. Under the hood, Excel is adding some <html><u> formatting for us to "pretty print" our links and make them display with a lovely underline for us. 

Excel:   "Oh, I see this cell contains a url. I will make it          look nice for you!" ðŸ¤“

 Excel then proceeds to add oldschool html markup... 

Us:      "My those underlines look distinguished and so                 presentable in this document, thanks Excel!" ðŸ˜ƒ 

Later on...  

Us:      "I need to export this data to use in my SQL                 database. I'm going to use the Roo gem to help me             with that."

Roo gem: "I see you have information in this cell, I'm not             touching that and will import it into SQL exactly             as you have formatted it." 

Us:      "Why is there excess markup baggage on my data?!" ðŸ˜–


The morale of the story... 

Excel is NOT a data medium. It is an analyst tool. Always import a .csv file rather than .xls and you will make your life as a backend developer much easier. 

 

        


    


Unfortunately that inline styling really mangled the string we imported. And Roo, 



Comments

Popular posts from this blog

Using GitHub on a Pair Programming Project

Importing Excel Data into a Ruby on Rails Application

The Journey Begins: Navigating a 15-week Software Engineering Bootcamp