Created
January 31, 2018 13:54
-
-
Save davisokoth/5b973a561766573f6a845be8b060fb6a to your computer and use it in GitHub Desktop.
Sample Code to strip data from an excel file and upload to server
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
NON_EDITABLE_STATES = ['DR', 'IN', 'IP', 'RA']; | |
SHEET_1_NAME = 'Facility ARV CDRR_MoH730Bb'; | |
SHEET_2_NAME = 'Facility_ARV_MAPS_MoH729B'; | |
FILE_VERSION = 'VER 0.1'; | |
CDRR_UPPER_BOUNDS = 13; | |
CDRR_LOWER_BOUNDS = 72; | |
SKIP_ROWS = [15, 19, 21, 30, 36, 39, 52, 63, 69,]; | |
site_id = 1023303; | |
tool_id = 1000000; | |
codeX = 9; | |
codeY = 3; | |
versionX = 12; | |
versionY = 3; | |
sites: any[]; | |
parsedSites = any[]; | |
currentSite: any; | |
ngOnInit() { | |
this.initialize(); | |
} | |
initialize() { | |
this.getDrugs(this.tool_id); | |
this.currentSite = this.getSiteDetails(this.site_id, this.tool_id); | |
} | |
getSiteDetails(c_bpartner_id: number, lmis_tool_id: number) { | |
this.centralsiteService.getCurrentSite(c_bpartner_id, lmis_tool_id).subscribe( | |
data => { | |
this.sites = data; | |
this.editable = this.checkSiteDetails(this.sites, this.NON_EDITABLE_STATES); | |
}, | |
err => { this.error = true; } | |
); | |
} | |
getDrugs(lmis_tool_id: number) { | |
this.toolService.getToolDrugs(lmis_tool_id).subscribe( | |
(data: any[]) => this.drugs = data, | |
err => this.error = true | |
); | |
} | |
checkSiteStates(sites: any, states: any[]) { | |
let editable = true; | |
sites.forEach(function(site) { | |
if (states.indexOf(site.docstatus) === -1) { | |
editable = false; | |
} | |
}); | |
return editable; | |
} | |
upload(files: any) { | |
for (const file of files) { | |
this.parseFiles(file); | |
} | |
} | |
parseFiles(file: any) { | |
const scope = this; | |
const target: DataTransfer = (<DataTransfer>(file)); | |
const reader = new FileReader(); | |
reader.onload = function (e: any) { | |
/* read workbook */ | |
const bstr = e.target.result; | |
const wb = XLSX.read(bstr, { type: 'binary' }); | |
scope.validateWorkbook(wb, this.codeX, this.codeY, this.versionX, this.versionY, this.sites, this.fileVersion); | |
}; | |
reader.readAsBinaryString(file); | |
} | |
validateWorkbook(workbook: any, codeX: number, codeY: number, versionX: number, versionY: number, sites: any[], fileVersion: string) { | |
if ((workbook.SheetNames.length !== 0) && (workbook.SheetNames[0] === SHEET_1_NAME) && (workbook.SheetNames[1] === SHEET_2_NAME) | |
) { | |
const sheet = workbook.Sheets[workbook.SheetNames[0]]; | |
const sheetData = (<AOA>(XLSX.utils.sheet_to_json(sheet, { header: 1 }))); | |
const mflcode = sheetData[codeX][codeY]; | |
const version = sheetData[versionX][versionY]; | |
if (mflcode !== undefined && version !== undefined) { | |
for (const site of sites) { | |
if ( | |
(site.facilitycode === mflcode) && | |
(site.iscomplete === 'N') && | |
(version.trim() === fileVersion) | |
) { | |
this.parseCDRR(workbook, site.lmis_facilityreport_id, this.CDRR_UPPER_BOUNDS, this.CDRR_LOWER_BOUNDS, this.SKIP_ROWS, this.drugs); | |
this.parsedSites.push(site); | |
} | |
} | |
} | |
} | |
} | |
parseCDRR(workbook: any, lmis_facilityreport_id: number, upperBounds: number, lowerBounds: number, skipRows: any[], drugs: any[]) { | |
const sheet = workbook.Sheets[workbook.SheetNames[0]]; | |
const sheetData = (<AOA>(XLSX.utils.sheet_to_json(sheet, { header: 1 }))); | |
const mysite = this.findFacilityReport(lmis_facilityreport_id); | |
let x = 0; | |
sheetData.forEach(function(row) { | |
if ((x > upperBounds) && (x < lowerBounds) && skipRows.indexOf(x) !== -1) { | |
for (let z = 2; z < 12; z++) { | |
if ((row[z] === undefined) || (row[z] === NaN) || (row[z].trim() === '-') || (row[z] === 'N/A')) { | |
row[z] = 0; | |
} else { | |
row[z] = this.toNumber(row[z]); | |
} | |
} | |
if (drugs[y] !== undefined) { | |
const cModel = new CDRRModel(drugs[y].lmis_drug_id, row[2], row[3], row[4], row[5], row[6], row[7], | |
row[8], row[9], row[10], row[11], lmis_facilityreport_id, mysite.name) | |
this.cdrrs.push(cModel); | |
} | |
y++; | |
} | |
x++; | |
} | |
} | |
uploadSite(this: any, site: any) { | |
const myThis = this; | |
const siteCdrrs = this.cdrrs.filter(function (cdrr) { | |
if (cdrr.lmis_facilityreport_id === site.lmis_facilityreport_id) { | |
return cdrr; | |
} | |
}); | |
let myCount = 0; | |
siteCdrrs.forEach(function (cdrr) { | |
console.log(cdrr); | |
myThis.cdrrService.uploadCDRR(cdrr).subscribe( | |
data => { | |
myCount++; | |
}, | |
error => { | |
myCount++; | |
}); | |
}); | |
} | |
findFacilityReport(lmis_facilityreport_id: number, sites: any[]) { | |
return sites.find(function(site) { | |
return site.lmis_facilityreport_id === lmis_facilityreport_id | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment