Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var fs = require('fs')
- var XLSX = require('xlsx')
- var content
- function convertData () {
- var workbook = XLSX.readFile('cache/data.xlsx')
- var worksheet = workbook.Sheets.Sheet1
- content = XLSX.utils.sheet_to_json(worksheet)
- console.log('Data Converted to JSON')
- cleanData()
- }
- // Get rid of the lunches, keynotes, help desks, etc.
- function cleanData () {
- let i
- for (i = 0; i < content.length; i++) {
- if (content[i]) {
- var sessionTags = content[i]['Session Tags']
- if (!sessionTags) {
- delete content[i]
- } else {
- var splitTags = content[i]['Session Tags'].split(',')
- var joinedTags = splitTags.join(', ')
- content[i]['Session Tags'] = joinedTags
- }
- }
- }
- console.log('Blank Session Tags Removed')
- fixDates()
- }
- function fixDates () {
- let i
- for (i = 0; i < content.length; i++) {
- if (content[i]) {
- var startDate = content[i]['Start Date/Time']
- var endDate = content[i]['End Date/Time']
- content[i]['Start Date/Time'] = getJsDateFromExcel(startDate)
- content[i]['End Date/Time'] = getJsDateFromExcel(endDate)
- }
- }
- console.log('Dates Fixed')
- fixNames()
- }
- String.prototype.InsertAt = function (CharToInsert, Position) {
- return this.slice(0, Position) + CharToInsert + this.slice(Position)
- }
- function splitNames (speakers) {
- const splitSpeakers = speakers.split(',')
- return splitSpeakers
- }
- function addSpace (speaker) {
- let i
- const matchIndexes = []
- for (i = 0; i < speaker.length; i++) {
- if (speaker.charAt(i).match(/[A-Z]/g)) {
- matchIndexes.push(i)
- }
- }
- if (speaker === 'KennyO\'Dell') {
- return 'Kenny O\'Dell'
- }
- // Skip the first capital letter, insert at the next one
- return speaker.InsertAt(' ', matchIndexes[1])
- }
- function fixNames () {
- let i
- for (i = 0; i < content.length; i++) {
- if (content[i]) {
- if (content[i]['Speaker Code']) {
- var speakerNames = content[i]['Speaker Code']
- // Are there more than one speaker?
- if (speakerNames.indexOf(',') === -1) {
- // If not, just add a space to the single name
- var fixedSpeaker = addSpace(speakerNames)
- content[i]['Speaker Code'] = fixedSpeaker
- } else {
- // Otherwise split the list up so we can add spaces to each name.
- var splitSpeakers = splitNames(speakerNames)
- var splitFixedSpeakers = []
- for (var index in splitSpeakers) {
- splitFixedSpeakers.push(addSpace(splitSpeakers[index]))
- }
- var fixedSpeakers = splitFixedSpeakers.join(', ')
- content[i]['Speaker Code'] = fixedSpeakers
- }
- }
- }
- }
- console.log('Names Fixed')
- splitData()
- }
- function splitData () {
- let i
- var sessions = []
- var learningLabs = []
- // Iterate over entire Dataset, seperate out Sessions & Learning Labs
- for (i = 0; i < content.length; i++) {
- if (content[i]) {
- if (content[i].Category === 'Conference Sessions') {
- sessions.push(content[i])
- } else if (content[i].Category === 'Learning Labs') {
- learningLabs.push(content[i])
- }
- }
- }
- // Write Sessions to File
- fs.writeFile('cache/sessions.json', JSON.stringify(sessions), 'utf8', function (err) {
- if (err) {
- return console.log(err)
- }
- console.log('Sessions File Written')
- })
- // Write Learning Labs to File
- fs.writeFile('cache/learninglabs.json', JSON.stringify(learningLabs), 'utf8', function (err) {
- if (err) {
- return console.log(err)
- }
- console.log('Learning Labs File Written')
- })
- }
- // https://gist.github.com/christopherscott/2782634
- function getJsDateFromExcel (serialDate) {
- return new Date(-2209075200000 + (serialDate - (serialDate < 61 ? 0 : 1)) * 86400000)
- }
- module.exports = convertData
- convertData()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement