Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2019
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var fs = require('fs')
  2. var XLSX = require('xlsx')
  3.  
  4. var content
  5.  
  6. function convertData () {
  7.   var workbook = XLSX.readFile('cache/data.xlsx')
  8.   var worksheet = workbook.Sheets.Sheet1
  9.  
  10.   content = XLSX.utils.sheet_to_json(worksheet)
  11.  
  12.   console.log('Data Converted to JSON')
  13.  
  14.   cleanData()
  15. }
  16.  
  17. // Get rid of the lunches, keynotes, help desks, etc.
  18. function cleanData () {
  19.   let i
  20.   for (i = 0; i < content.length; i++) {
  21.     if (content[i]) {
  22.       var sessionTags = content[i]['Session Tags']
  23.  
  24.       if (!sessionTags) {
  25.         delete content[i]
  26.       } else {
  27.         var splitTags = content[i]['Session Tags'].split(',')
  28.         var joinedTags = splitTags.join(', ')
  29.         content[i]['Session Tags'] = joinedTags
  30.       }
  31.     }
  32.   }
  33.   console.log('Blank Session Tags Removed')
  34.  
  35.   fixDates()
  36. }
  37.  
  38. function fixDates () {
  39.   let i
  40.   for (i = 0; i < content.length; i++) {
  41.     if (content[i]) {
  42.       var startDate = content[i]['Start Date/Time']
  43.       var endDate = content[i]['End Date/Time']
  44.  
  45.       content[i]['Start Date/Time'] = getJsDateFromExcel(startDate)
  46.       content[i]['End Date/Time'] = getJsDateFromExcel(endDate)
  47.     }
  48.   }
  49.  
  50.   console.log('Dates Fixed')
  51.  
  52.   fixNames()
  53. }
  54.  
  55. String.prototype.InsertAt = function (CharToInsert, Position) {
  56.   return this.slice(0, Position) + CharToInsert + this.slice(Position)
  57. }
  58.  
  59. function splitNames (speakers) {
  60.   const splitSpeakers = speakers.split(',')
  61.   return splitSpeakers
  62. }
  63.  
  64. function addSpace (speaker) {
  65.   let i
  66.   const matchIndexes = []
  67.  
  68.   for (i = 0; i < speaker.length; i++) {
  69.     if (speaker.charAt(i).match(/[A-Z]/g)) {
  70.       matchIndexes.push(i)
  71.     }
  72.   }
  73.  
  74.   if (speaker === 'KennyO\'Dell') {
  75.     return 'Kenny O\'Dell'
  76.   }
  77.  
  78.   // Skip the first capital letter, insert at the next one
  79.   return speaker.InsertAt(' ', matchIndexes[1])
  80. }
  81.  
  82. function fixNames () {
  83.   let i
  84.   for (i = 0; i < content.length; i++) {
  85.     if (content[i]) {
  86.       if (content[i]['Speaker Code']) {
  87.         var speakerNames = content[i]['Speaker Code']
  88.         // Are there more than one speaker?
  89.         if (speakerNames.indexOf(',') === -1) {
  90.           // If not, just add a space to the single name
  91.           var fixedSpeaker = addSpace(speakerNames)
  92.           content[i]['Speaker Code'] = fixedSpeaker
  93.         } else {
  94.           // Otherwise split the list up so we can add spaces to each name.
  95.           var splitSpeakers = splitNames(speakerNames)
  96.           var splitFixedSpeakers = []
  97.           for (var index in splitSpeakers) {
  98.             splitFixedSpeakers.push(addSpace(splitSpeakers[index]))
  99.           }
  100.           var fixedSpeakers = splitFixedSpeakers.join(', ')
  101.           content[i]['Speaker Code'] = fixedSpeakers
  102.         }
  103.       }
  104.     }
  105.   }
  106.   console.log('Names Fixed')
  107.  
  108.   splitData()
  109. }
  110.  
  111. function splitData () {
  112.   let i
  113.   var sessions = []
  114.   var learningLabs = []
  115.  
  116.   // Iterate over entire Dataset, seperate out Sessions & Learning Labs
  117.   for (i = 0; i < content.length; i++) {
  118.     if (content[i]) {
  119.       if (content[i].Category === 'Conference Sessions') {
  120.         sessions.push(content[i])
  121.       } else if (content[i].Category === 'Learning Labs') {
  122.         learningLabs.push(content[i])
  123.       }
  124.     }
  125.   }
  126.  
  127.   // Write Sessions to File
  128.   fs.writeFile('cache/sessions.json', JSON.stringify(sessions), 'utf8', function (err) {
  129.     if (err) {
  130.       return console.log(err)
  131.     }
  132.     console.log('Sessions File Written')
  133.   })
  134.  
  135.   // Write Learning Labs to File
  136.   fs.writeFile('cache/learninglabs.json', JSON.stringify(learningLabs), 'utf8', function (err) {
  137.     if (err) {
  138.       return console.log(err)
  139.     }
  140.     console.log('Learning Labs File Written')
  141.   })
  142. }
  143.  
  144. // https://gist.github.com/christopherscott/2782634
  145. function getJsDateFromExcel (serialDate) {
  146.   return new Date(-2209075200000 + (serialDate - (serialDate < 61 ? 0 : 1)) * 86400000)
  147. }
  148.  
  149. module.exports = convertData
  150.  
  151. convertData()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement