Parse the CSV file upload in ServiceNow Service Portal

Body HTML template

 

<div class=”text-center m”>
<span class=”file-upload-input”>
<input type=”file” style=”display: none” multiple=”true” ng-file-select=”attachmentHandler.onFileSelect($files); filePicked($files);” class=”sp-attachments-input”>
<button title=”Add attachment”
ng-click=”attachmentHandler.openSelector($event)”
class=”btn btn-primary sp-attachment-add”
aria-label=”Add attachment” role=”button”>${Upload the custim file}
</button>
</span>
</div>

Client Controller Code


$scope.filePicked = function (oEvent) {
docArr = [];

// Get The File From The Input
var oFile = oEvent[0];
var sFilename = oFile.name;

// Create A File Reader HTML5
var reader = new FileReader();

// Ready The Event For When A File Gets Selected
reader.onload = function(e) {
try {
setTimeout(function(){
$rootScope.$broadcast(‘file-attached’,null);
},1000);

var data = e.target.result;
var cfb = XLS.CFB.read(data, {type: ‘binary’});

var wb = XLS.parse_xlscfb(cfb);

// Loop Over Each Sheet
wb.SheetNames.forEach(function(sheetName) {
// Obtain The Current Row As CSV
var sCSV = XLS.utils.make_csv(wb.Sheets[sheetName]);
var oJS = XLS.utils.sheet_to_row_object_array(wb.Sheets[sheetName]);
var theanswer = sCSV.split(‘,’);
docArr.push(theanswer);

console.log(theanswer.filter(String)[0]);
$scope.data.output = theanswer.filter(String);
$scope.data.doc_date = theanswer.filter(String)[0];
$scope.data.doc_date_val = theanswer.filter(String)[1];
$scope.data.post_date = theanswer.filter(String)[2];
$scope.data.post_date_val = theanswer.filter(String)[3];
}
}
}
}