-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathexcel-helpers.js
168 lines (157 loc) · 4.52 KB
/
excel-helpers.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
"use strict";
let sheets = {};
function populate_sheet_from_array_of_arrays(sheet, array) {
for (let i=0; i<array.length; i++){
var row = array[i];
for (var j=0; j<row.length; j++){
var cell = row[j];
let cellProps = {};
cellProps = {
'set': cell.value,
};
if (typeof(cell) !== 'object'){
cellProps.set = cell;
}
else{
if (cell.style){
for (var prop in cell.style){
if (cell.style.hasOwnProperty(prop)){
cellProps[prop] = cell.style[prop];
}
}
}
}
if (cellProps.set !== undefined && cellProps.set !== "" ){
//the column/rows is switched from what you might think, j (column) goes first
sheet.set(j + 1, i + 1, cellProps);
}
//we do not set undefined values
}
}
return sheet;
}
function getDimensionsForNewSheet(sheetArray){
let rows = sheetArray.length;
let columns = 1;
for (let i=0; i<sheetArray.length; i++){
var row = sheetArray[i];
var rowLength = row.length;
if (rowLength > columns){columns = rowLength;}
}
return {rows, columns};
}
function makeNewSheet(workbook, inputArray, name, populateNow = false){
//without populateNow create an empty sheet properly sized for the data
//with populateNow, add the data in as well. This option is probably used 95% of the time.
let dimensions = getDimensionsForNewSheet(inputArray);
let newSheet = workbook.createSheet(name, dimensions.columns, dimensions.rows);
setReasonableColumnWidths(dimensions, newSheet);
sheets[name] = {sheet:newSheet, dimensions};
if (populateNow){
newSheet = populate_sheet_from_array_of_arrays(newSheet, inputArray);
}
return newSheet;
}
function applyStylesToTheWholeSheet(sheetObj, styleObj){
//do a little method overloading to let both enhanced sheet Objs
//(those with dimension attributes, created by makeNewSheet)
//and normal unmodified sheetObjs work
var cols = sheetObj.cols || sheetObj.sheet.cols;
var rows = sheetObj.rows || sheetObj.sheet.rows;
var sheet = sheetObj.sheet || sheetObj;
for (let col=1; col<=cols; col++){
for (let row=1; row<=rows; row++){
if (styleObj.hasOwnProperty("font")){
sheet.font(col, row, styleObj.font);
}
if (styleObj.hasOwnProperty("border")){
sheet.border(col, row, styleObj.border);
}
if (styleObj.hasOwnProperty("fill")){
sheet.fill(col, row, styleObj.fill);
}
}
}
}
function setReasonableColumnWidths(dimensions, worksheet){
//by default, the column width will be only 8.43, or 64 pixels.
//this is pretty miserable to work with
//a more reasonable value is a width of 20
//with the first column in a file always being shorter, at 15
setColumnWidths(dimensions, worksheet, 20, 15);
}
function setColumnWidths (dimensions, worksheet, defaultWidth, firstWidth){
for (let i=0; i<dimensions.columns; i++){
if (i === 0 && firstWidth >= 0){
worksheet.width(i+1, firstWidth);
}
else{
worksheet.width(i+1, defaultWidth);
}
}
}
function setRowHeights (dimensions, worksheet, defaultHeight, firstHeight){
for (let i=0; i<dimensions.rows; i++){
if (i === 0 && firstHeight >= 0){
worksheet.height(i+1, firstHeight);
}
else{
worksheet.height(i+1, defaultHeight);
}
}
}
function setBorder(sheet, style, startLocation, direction){
let x, y = 0;
if (Array.isArray(startLocation)){
//do some overload detection - if an array with two elements exists
if (startLocation.length === 2){
// then it is [x,y] and we convert to a property
x = startLocation[0];
y = startLocation[1];
startLocation = {column:y, row:x};
}
else {
throw ("startLocation dimensions are wrong");
}
}
else{
if (startLocation.hasOwnProperty('column') && startLocation.hasOwnProperty('row')) {
startLocation = {column:y, row:x};
}
else{
throw ("no startLocation present");
}
}
var column = startLocation.column;
var row = startLocation.row;
//direction gives us up or down, and length. For example, {long:5}
if (typeof(direction) === 'number'){
//we do some overloading, if direction is a number we just assume length
direction = {'long':direction};
}
if (direction.hasOwnProperty('long')){
var length = direction.long - 1;
for (let i=column; i<length+column; i++){
sheet.border(i,row,style);
}
}
else {if (direction.hasOwnProperty('tall')){
var height = direction.height - 1;
for (let i=row; i<height+row; i++){
sheet.border(row,i,style);
}
}}
}
function getSheets(){
return sheets;
}
module.exports = {
getDimensionsForNewSheet,
makeNewSheet,
applyStylesToTheWholeSheet,
setBorder,
setRowHeights,
setColumnWidths,
getSheets,
populate_sheet_from_array_of_arrays
};