Skip to content

Instantly share code, notes, and snippets.

@afucher
Created November 5, 2016 12:45
Show Gist options
  • Save afucher/49a4bf98d4f41253d2438c4b3e5c7e03 to your computer and use it in GitHub Desktop.
Save afucher/49a4bf98d4f41253d2438c4b3e5c7e03 to your computer and use it in GitHub Desktop.
'use strict';
var schedAPI = require('./schedAPI');
var handlebars = require("handlebars");
var fs = require('fs');
var partialsDir = __dirname + '/template/partials';
var XLSX = require('xlsx');
var Excel = require('exceljs');
/** Retorna as informações do sched e gera as placas em html */
schedAPI.getSessionExport((err,session) => {
var venues = getVenues(session);
session = session.filter((session)=>{
return session.venue !== "Foyer" && session.active == "Y";
})
generate2(venues, session);
//generate(venues, session);
})
function generate2(venues, sessions){
var workbook = new Excel.Workbook();
var worksheets = {};
venues.forEach((venue)=>{
worksheets[venue] = workbook.addWorksheet(venue);
worksheets[venue].columns = [
{ header: 'DATA', key: 'date', width: 10 },
{ header: 'HORÁRIO', key: 'hour', width: 32 },
{ header: 'ATIVIDATE', key: 'activity', width : 50},
{ header: 'VERDE', key: 'green', width: 32},
{ header: 'AMARELO', key: 'yellow', width: 32 },
{ header: 'VERMELHO', key: 'red', width: 32 }
];
})
sessions.forEach((session)=>{
worksheets[session.venue].addRow([new Date(session.start_date),session.event_start_time,session.name]);
})
workbook.xlsx.writeFile("test.xlsx")
.then(function() {
console.log("terminou");// done
});
}
/** Gera o html com as placas baseado no template */
function generate(venues,sessions){
var wb = new Workbook();//, ws = sheet_from_array_of_arrays(data);
/* add worksheet to workbook */
var ws = {};
venues.forEach((venue)=>{
wb.SheetNames.push(venue);
})
var cell = {v: "teste", t:'s' };
var cell_ref = XLSX.utils.encode_cell({c:1,r:1});
console.log(cell_ref);
ws[cell_ref] = cell;
console.log(venues[1]);
wb.Sheets[venues[1]] = ws;
//sessions.forEach((session)=>{
//wb.Sheets[session.venue] = [{v:"teste"}];
//})
//wb.Sheets["Teste"] = [1,2,3];
/* write file */
XLSX.writeFile(wb, 'test.xlsx');
}
function Workbook() {
if(!(this instanceof Workbook)) return new Workbook();
this.SheetNames = [];
this.Sheets = {};
}
function getVenues(sessionList){
var venues = sessionList.reduce(function(prev,curr,index,arr){
if(prev.indexOf(curr.venue) < 0){
prev.push(curr.venue);
}
return prev;
},[]);
let index = venues.indexOf('Foyer');
if (index > -1){
venues.splice(index, 1);
}
return venues;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment