본문 바로가기

JS

[JS] exceljs 사용시 편하게 함수 만들기

반응형

exceljs 속성 입력한는 부분 공통으로 만들기

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
export const setProps = (worksheet, cellNum, prop, addProps) => {
    if (cellNum instanceof Array) {
        //배열로 들어오는 경우
        for (let i = 0; i < cellNum.length; i++) {
            if (cellNum[i].indexOf(':'> 0) {
                // :으로 들어오는 경우
                let start = cellNum[i].split(':')[0]
                let end = cellNum[i].split(':')[1]
 
                let startAlphaBet =
                    start.replace(/[0-9]/g, '').charCodeAt(0>= end.replace(/[0-9]/g, '').charCodeAt(0)
                        ? end.replace(/[0-9]/g, '').charCodeAt(0)
                        : start.replace(/[0-9]/g, '').charCodeAt(0)
                let startNum =
                    Number(start.replace(/[^0-9]/g, '')) >= Number(end.replace(/[^0-9]/g, ''))
                        ? end.replace(/[^0-9]/g, '')
                        : start.replace(/[^0-9]/g, '')
                let endAlphabet =
                    start.replace(/[0-9]/g, '').charCodeAt(0<= end.replace(/[0-9]/g, '').charCodeAt(0)
                        ? end.replace(/[0-9]/g, '').charCodeAt(0)
                        : start.replace(/[0-9]/g, '').charCodeAt(0)
                let endNum =
                    Number(start.replace(/[^0-9]/g, '')) <= Number(end.replace(/[^0-9]/g, ''))
                        ? end.replace(/[^0-9]/g, '')
                        : start.replace(/[^0-9]/g, '')
 
                let tempArray = []
 
                for (let a = startAlphaBet; a <= endAlphabet; a++) {
                    for (let b = Number(startNum); b <= Number(endNum); b++) {
                        tempArray.push(String.fromCharCode(a).toUpperCase() + String(b))
                    }
                }
 
                for (let c = 0; c < tempArray.length; c++) {
                    if (!worksheet.getCell(tempArray[c])[prop]) {
                        worksheet.getCell(tempArray[c])[prop] = addProps
                    } else {
                        worksheet.getCell(tempArray[c])[prop] = extend(worksheet.getCell(tempArray[c])[prop], addProps)
                    }
                }
            } else {
                if (cellNum[i] instanceof Array) {
                    for (let j = 0; j < cellNum[i].length; j++) {
                        if (!worksheet.getCell(cellNum[i][j])[prop]) {
                            worksheet.getCell(cellNum[i][j])[prop] = addProps
                        } else {
                            worksheet.getCell(cellNum[i][j])[prop] = extend(
                                worksheet.getCell(cellNum[i][j])[prop],
                                addProps
                            )
                        }
                    }
                } else {
                    if (!worksheet.getCell(cellNum[i])[prop]) {
                        worksheet.getCell(cellNum[i])[prop] = addProps
                    } else {
                        worksheet.getCell(cellNum[i])[prop] = extend(worksheet.getCell(cellNum[i])[prop], addProps)
                    }
                }
            }
        }
    } else {
        if (cellNum.indexOf(':'> 0) {
            // :으로 들어오는 경우
            let start = cellNum.split(':')[0]
            let end = cellNum.split(':')[1]
 
            let startAlphaBet =
                start.replace(/[0-9]/g, '').charCodeAt(0>= end.replace(/[0-9]/g, '').charCodeAt(0)
                    ? end.replace(/[0-9]/g, '').charCodeAt(0)
                    : start.replace(/[0-9]/g, '').charCodeAt(0)
            let startNum =
                Number(start.replace(/[^0-9]/g, '')) >= Number(end.replace(/[^0-9]/g, ''))
                    ? end.replace(/[^0-9]/g, '')
                    : start.replace(/[^0-9]/g, '')
            let endAlphabet =
                start.replace(/[0-9]/g, '').charCodeAt(0<= end.replace(/[0-9]/g, '').charCodeAt(0)
                    ? end.replace(/[0-9]/g, '').charCodeAt(0)
                    : start.replace(/[0-9]/g, '').charCodeAt(0)
            let endNum =
                Number(start.replace(/[^0-9]/g, '')) <= Number(end.replace(/[^0-9]/g, ''))
                    ? end.replace(/[^0-9]/g, '')
                    : start.replace(/[^0-9]/g, '')
 
            let tempArray = []
 
            for (let i = startAlphaBet; i <= endAlphabet; i++) {
                for (let j = Number(startNum); j <= Number(endNum); j++) {
                    tempArray.push(String.fromCharCode(i).toUpperCase() + String(j))
                }
            }
 
            for (let k = 0; k < tempArray.length; k++) {
                if (!worksheet.getCell(tempArray[k])[prop]) {
                    worksheet.getCell(tempArray[k])[prop] = addProps
                } else {
                    worksheet.getCell(tempArray[k])[prop] = extend(worksheet.getCell(tempArray[k])[prop], addProps)
                }
            }
        } else {
            for (let i = 0; i < cellNum.length; i++) {
                if (!worksheet.getCell(cellNum)[prop]) {
                    worksheet.getCell(cellNum)[prop] = addProps
                } else {
                    worksheet.getCell(cellNum)[prop] = extend(worksheet.getCell(cellNum)[prop], addProps)
                }
            }
        }
    }
}
cs

사용방법은 

1
Common.setProps('작업을 할 시트','작업을 할 셀','속성명','속성')
cs

위처럼 작성되면 되는데 예시는

1
Common.setProps(worksheet,['B9''B30:G30'],'border',{ top: { style: 'thick' } })
cs
반응형