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

Yep, I attached the 4 files to this reply "4_files_mentioned_above.zip"

The 4 files I attached are XML exports of the 4 UI Scripts.  If you only want the js code, let me know and I'll give just those.

sgodinho
Kilo Contributor
Thanks a million! I will try this out in my PDI today((: