Skip to content

Instantly share code, notes, and snippets.

@martianboy
Created February 8, 2018 10:35
Show Gist options
  • Save martianboy/e2cc3443022bf760245ed55403c32ac2 to your computer and use it in GitHub Desktop.
Save martianboy/e2cc3443022bf760245ed55403c32ac2 to your computer and use it in GitHub Desktop.
Outlook CSV validation
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Page Title</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
#byte_content {
margin: 5px 0;
max-height: 100px;
overflow-y: auto;
overflow-x: hidden;
}
#byte_range {
margin-top: 5px;
}
</style>
</head>
<body>
<input type="file" id="files" name="file" /> Read bytes:
<span class="readBytesButtons">
<button data-startbyte="0" data-endbyte="4">1-5</button>
<button data-startbyte="5" data-endbyte="14">6-15</button>
<button data-startbyte="6" data-endbyte="7">7-8</button>
<button>entire file</button>
</span>
<button type='button' id='validate'>Validate!</button>
<div id="byte_range"></div>
<div id="byte_content"></div>
<script>
/**
* Validates a file to be in outlook csv format
* @param file {File} input file
* @returns {Promise<boolean>}
*/
function isValidOutlookFile(file) {
const OUTLOOK_HEADER_ROW =
"First Name,Middle Name,Last Name,Title,Suffix,Nickname,Given Yomi,Surname Yomi,E-mail Address,E-mail 2 Address,E-mail 3 Address,Home Phone,Home Phone 2,Business Phone,Business Phone 2,Mobile Phone,Car Phone,Other Phone,Primary Phone,Pager,Business Fax,Home Fax,Other Fax,Company Main Phone,Callback,Radio Phone,Telex,TTY/TDD Phone,IMAddress,Job Title,Department,Company,Office Location,Manager's Name,Assistant's Name,Assistant's Phone,Company Yomi,Business Street,Business City,Business State,Business Postal Code,Business Country/Region,Home Street,Home City,Home State,Home Postal Code,Home Country/Region,Other Street,Other City,Other State,Other Postal Code,Other Country/Region,Personal Web Page,Spouse,Schools,Hobby,Location,Web Page,Birthday,Anniversary,Notes".toLowerCase();
return new Promise(function (res, rej) {
if (window.File && window.FileReader && window.FileList && window.Blob) {
// Take BOM into account
const headerBlob = file.slice(0, OUTLOOK_HEADER_ROW.length + 3)
const reader = new FileReader()
reader.addEventListener('error', rej, false)
reader.addEventListener('abort', rej, false)
reader.addEventListener('load', e => {
/** @type {String} */
const header = e.target.result
const result = header.substring(0, OUTLOOK_HEADER_ROW.length).toLowerCase() === OUTLOOK_HEADER_ROW ||
header.substring(3).toLowerCase() === OUTLOOK_HEADER_ROW
res(result)
}, false)
reader.readAsText(headerBlob)
}
else {
console.warn('Validation impossible!')
res(true)
}
})
}
function readBlob(opt_startByte, opt_stopByte) {
var files = document.getElementById('files').files;
if (!files.length) {
alert('Please select a file!');
return;
}
var file = files[0];
var start = parseInt(opt_startByte) || 0;
var stop = parseInt(opt_stopByte) || file.size - 1;
var reader = new FileReader();
// If we use onloadend, we need to check the readyState.
reader.onloadend = function (evt) {
if (evt.target.readyState == FileReader.DONE) { // DONE == 2
document.getElementById('byte_content').textContent = evt.target.result;
document.getElementById('byte_range').textContent =
['Read bytes: ', start + 1, ' - ', stop + 1,
' of ', file.size, ' byte file'].join('');
}
};
var blob = file.slice(start, stop + 1);
reader.readAsBinaryString(blob);
}
function validateFile() {
var files = document.getElementById('files').files;
if (!files.length) {
alert('Please select a file!');
return;
}
var file = files[0];
isValidOutlookFile(file).then(
valid => console.log(valid),
err => console.error(err)
)
}
document.getElementById('validate').addEventListener('click', function(evt) {
if (evt.target.tagName.toLowerCase() == 'button') {
var startByte = evt.target.getAttribute('data-startbyte');
var endByte = evt.target.getAttribute('data-endbyte');
validateFile()
}
}, false)
document.querySelector('.readBytesButtons').addEventListener('click', function (evt) {
if (evt.target.tagName.toLowerCase() == 'button') {
var startByte = evt.target.getAttribute('data-startbyte');
var endByte = evt.target.getAttribute('data-endbyte');
readBlob(startByte, endByte);
}
}, false);
</script>
</body>
</html>
/**
* Validates a file to be in outlook csv format
* @param file {File} input file
* @returns {Promise<boolean>}
*/
function isValidOutlookFile(file) {
const OUTLOOK_HEADER_ROW =
"First Name,Middle Name,Last Name,Title,Suffix,Nickname,Given Yomi,Surname Yomi,E-mail Address,E-mail 2 Address,E-mail 3 Address,Home Phone,Home Phone 2,Business Phone,Business Phone 2,Mobile Phone,Car Phone,Other Phone,Primary Phone,Pager,Business Fax,Home Fax,Other Fax,Company Main Phone,Callback,Radio Phone,Telex,TTY/TDD Phone,IMAddress,Job Title,Department,Company,Office Location,Manager's Name,Assistant's Name,Assistant's Phone,Company Yomi,Business Street,Business City,Business State,Business Postal Code,Business Country/Region,Home Street,Home City,Home State,Home Postal Code,Home Country/Region,Other Street,Other City,Other State,Other Postal Code,Other Country/Region,Personal Web Page,Spouse,Schools,Hobby,Location,Web Page,Birthday,Anniversary,Notes".toLowerCase();
return new Promise(function(res, rej) {
if (window.File && window.FileReader && window.FileList && window.Blob) {
// Take BOM into account
const headerBlob = file.slice(0, OUTLOOK_HEADER_ROW.length + 3)
const reader = new FileReader()
reader.addEventListener('error', rej, false)
reader.addEventListener('abort', rej, false)
reader.addEventListener('load', e => {
/** @type {String} */
const header = e.target.result
const result = header.substring(0, OUTLOOK_HEADER_ROW.length).toLowerCase() === OUTLOOK_HEADER_ROW ||
header.substring(3).toLowerCase() === OUTLOOK_HEADER_ROW
res(result)
}, false)
reader.readAsText(headerBlob)
}
else {
console.warn('Validation impossible!')
res(true)
}
})
}
@martianboy
Copy link
Author

This script checks the header row to match with Outlook's CSV format.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment