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

Thank you VERY much David!


astha_chaubey
Tera Expert

Hi David,



First of all need to thank you as your post helped me a lot with one of the implementations. My customer was quite curious to know about the 4 scripts that are provided, they have a licence or copyright associated?



Thanks & Regards,


Astha Chaubey


You can check out that info here: SheetJS



They post their code available for use/link/download on GitHub (GitHub - SheetJS/js-xlsx: SheetJS Community Edition -- Spreadsheet Parser and Writer ), which is where I originally obtained it from.



If you really *must* know, send an email to: license@sheetjs.com


uzma1
Kilo Contributor

Hi David,



Your code helped me alot. Can you please suggest me how to call this ui page through UI action?



Thanks,


Uzma


questsal78
Giga Expert

David! Thank you so much for this code