-
Notifications
You must be signed in to change notification settings - Fork 4
/
sheet.js
60 lines (49 loc) · 1.73 KB
/
sheet.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
const { sheetFront, sheetBack, columnDefinition } = require('./constants')
const { addMergeCell, addCell, convertToBit } = require('./helpers')
function buildRow (columns, data, rowIndex) {
const rows = [`<x:row r="${rowIndex}" spans="1:${columns.length}">`]
for (var i = 0; i < data.length; i++) {
const cellIndex = i + 1
const isObject = typeof data[i] === 'object'
const cellData = isObject ? data[i].value : data[i]
const styleIndex = isObject ? data[i].style : null
const cellType = columns[i].type
const options = {
cellIndex,
value: cellData,
styleIndex,
rowIndex
}
switch (cellType) {
case 'number':
case 'date':
rows.push(addCell(Object.assign({ type: 'n' }, options)))
break
case 'bool':
rows.push(addCell(Object.assign({ value: convertToBit(cellData), type: 'b' }, options)))
break
default:
rows.push(addCell(options))
}
}
rows.push('</x:row>')
return rows.join('')
}
function buildMergeCells (merge) {
if (merge) {
const mergeCells = merge.map((mergeCell) => addMergeCell(mergeCell)).join('')
return `<mergeCells count="${merge.length}">${mergeCells}</mergeCells>`
} else {
return ''
}
}
function Sheet ({ columns, rows, merge }) {
if (!columns) throw 'No columns provided'
if (!rows) throw 'No row data provided'
// build column elements
const columnDefinitions = columns.map((column, i) => columnDefinition(column.width, i + 1)).join('')
// build xml rows
const xmlRows = rows.map((rowData, i) => buildRow(columns, rowData, i + 1)).join('')
return `${sheetFront}<cols>${columnDefinitions}</cols><x:sheetData>${xmlRows}</x:sheetData>${buildMergeCells(merge)}${sheetBack}`
}
module.exports = Sheet