Parse Excel (xlsx) file.. with ease!

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-08-2016 04:55 AM
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:
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.
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:
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:
... and the result after I choose my file:
I certainly hope someone finds this useful.
- Labels:
-
Scripting and Coding
- 19,680 Views
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-02-2018 06:53 AM
Hey there!
Bit confused where to write this client script. I have an excel which holds multiple sheets, need to get data from those sheets and update multiple tables at one go. Instead of doing it for every sheet every time thru load data.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-07-2018 05:47 AM
I had the same requirement (excel sheet with multiple tabs) at one time and wrote code for that too. I even wrote handling for possible large chunks of data where the script would puke if each tab had like 10k rows. Let me get that cleaned up and then I'll post that too.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-27-2018 04:05 AM
Hi @davidd.
Did you achieve that requirement (excel which holds multiple sheets, need to get data from those sheets and update multiple tables at one go), if so can you please share your some light on that.
Thanks 🙂
Shanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-23-2019 01:13 AM
Hi Aswath,
did you get solution for this thread, i have requirement like I want to parse excel data in the table format to the current record, does it possible?
Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-06-2020 07:09 AM
Hello,
I am receiving an error when clicking on the file links provided above...I am particularly in need of an Excel sheet setup. Does anyone still have these files?
Thank you in advance!