Parse Excel (xlsx) file.. with ease!

xiaix
Tera Guru

So, I'll take the cookies out of the cookie jar that sits on the top shelf, and lay them out on the table ready for consumption.  

First, you'll need four (4) files:

File: xlsx.js

RAW: https://raw.githubusercontent.com/SheetJS/js-xlsx/master/xlsx.js

GIT: https://github.com/SheetJS/js-xlsx/blob/master/xlsx.js

File: shim.js

RAW: https://raw.githubusercontent.com/SheetJS/js-xlsx/master/shim.js

GIT: https://github.com/SheetJS/js-xlsx/blob/master/shim.js

File: ods.js

RAW: https://raw.githubusercontent.com/SheetJS/js-xlsx/master/ods.js

GIT: https://github.com/SheetJS/js-xlsx/blob/master/ods.js

File: jszip.js

RAW: https://raw.githubusercontent.com/SheetJS/js-xlsx/master/jszip.js

GIT: https://github.com/SheetJS/js-xlsx/blob/master/jszip.js

You'll create 4 separate UI Scripts for each file:

find_real_file.png

Great.   Now, create a UI Page that you'll call from where ever you'd like.   The UI Page will look like this:

HTML (xml):

<?xml version="1.0" encoding="utf-8" ?>

<j:jelly trim="false" xmlns:j="jelly:core" xmlns:g="glide" xmlns:j2="null" xmlns:g2="null">

      <g:include_script src="shim.jsdbx" />

      <g:include_script src="jszip.jsdbx" />

      <g:include_script src="xlsx.jsdbx" />

      <g:include_script src="ods.jsdbx" />

      <head>

              <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

      </head>

      <body>

              <p id="TemplateLink"></p>

              <p><input type="file" name="xlfile" id="xlf" /></p>

              <br />

              <pre id="out"></pre>

              <br />

      </body>

</j:jelly>

Client Script:

var X = XLSX;  

 

function fixdata(data)

{

      var o = "";

      var l = 0;

      var w = 10240;

     

      for (; l < data.byteLength / w; ++l)

      {

              o += String.fromCharCode.apply(null, new Uint8Array(data.slice(l * w, l * w + w)));

      }

     

      o += String.fromCharCode.apply(null, new Uint8Array(data.slice(l * w)));

      return o;

}

function to_json(workbook)

{

      var result = {};

      workbook.SheetNames.forEach(function(sheetName) {

              var roa = X.utils.sheet_to_row_object_array(workbook.Sheets[sheetName]);

              if(roa.length > 0)

                      result[sheetName] = roa;

      });

     

      return result;

}

 

function process_wb(wb)  

{  

      var output = "";  

      output = JSON.stringify(to_json(wb), 2, 2);  

         

      var excelData_array = to_json(wb);  

         

      if (excelData_array['Sheet 1'])

      {  

              for (var i = 0; i < excelData_array['Sheet 1'].length; i++)  

              {  

                      var column_1 = excelData_array['Sheet 1'][i]['Name of user'];  

                      var column_2 = excelData_array['Sheet 1'][i]['Role'];  

                      var column_3 = excelData_array['Sheet 1'][i]['Email'];  

              }  

      }  

      else  

              alert("Cannot determine the contents of the file specified.\rEither there were no users listed in the columns,\ror the file is not valid.\r\nError [07072016]");  

 

 

      if (out.innerText === undefined)

              out.textContent = output;  

      else  

              out.innerText = output;  

}

function handleFile(e)

{

      var files = e.target.files;

      var f = files[0];

      {

              var reader = new FileReader();

              var name = f.name;

              reader.onload = function(e) {

                      var data = e.target.result;

                      var arr = fixdata(data);

                      var wb = X.read(btoa(arr), {type: 'base64'});

                      process_wb(wb);

              };

             

              reader.readAsArrayBuffer(f);

      }

}

var xlf = document.getElementById('xlf');

if (xlf && xlf.addEventListener)

      xlf.addEventListener('change', handleFile, false);

Please take note of the process_wb(wb) function above!   This is where you'll need to edit it for your own purposes.

find_real_file.png

You can simply output the stringified JSON data, or you can use the data as I have done as pictured above.

For example, my Excel document looks like this:

find_real_file.png

Now, there's a lot more I'm doing with this data but I'm sure you're not interested.   The point of this post is to get you 99% there to parsing Excel documents in ServiceNow and then using the retrieved data however you see fit.

For what it's worth, here's how I'm using mine:

find_real_file.png

find_real_file.png

... and the result after I choose my file:

find_real_file.png

I certainly hope someone finds this useful.

26 REPLIES 26

julienr
Mega Contributor

Hello David,


Your post pretty much sums up everything I have been trying to do for the past 3 days. Yay to that!


So as you might imagine, I've been thrilled when reading through this.
I tried downloading the 4 scripts to start playing around and the ODS one is missing: the raw gives a 404 and the gh gives a page not found.


Would you still have a copy, or another link, or something I could use?


Thank you very much in advance


I don't think you need the ODS script if you get the latest version of the xlsx one. I've just been using the three scripts that are still available and ODS files work fine, so I guess they merged that script into the main one and removed the separate file.


pawankumar
Tera Contributor

Hi David,



I have pretty much same requirement but I am not able to download the UI scripts from the links in the post. Has it been moved to some other location ?




Thanks,


Pawan


Hello Kumar.


If that's any help, you can access 3 out of 4 (ODS one is unavailable) by clicking on the raw link, then removing the community.servicenow.com in the URL. I know it's not the full set, but it may help.


See attached file