Skip to content

Commit dc6dc60

Browse files
committed
feat: support col width
support col width by specifing cols in a columns: {title:some-title, width: {wpx:num-of-pixes}} or {wch:num-of-chars}
1 parent 9a630c4 commit dc6dc60

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

dist/ExcelPlugin/utils/DataUtil.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,16 +64,26 @@ var excelSheetFromDataSet = function excelSheetFromDataSet(dataSet) {
6464

6565
rowCount += ySteps;
6666

67+
var columnsWidth = [];
6768
if (columns.length >= 0) {
6869
columns.forEach(function (col, index) {
6970
var cellRef = _xlsx2.default.utils.encode_cell({ c: xSteps + index, r: rowCount });
7071
fixRange(range, 0, 0, rowCount, xSteps, ySteps);
71-
getHeaderCell(col, cellRef, ws);
72+
var colTitle = col;
73+
if ((typeof col === 'undefined' ? 'undefined' : _typeof(col)) === 'object') {
74+
colTitle = col.title;
75+
columnsWidth.push(col.width || { wpx: 80 }); /* wch (chars), wpx (pixels) - e.g. [{wch:6},{wpx:50}] */
76+
}
77+
getHeaderCell(colTitle, cellRef, ws);
7278
});
7379

7480
rowCount += 1;
7581
}
7682

83+
if (columnsWidth.length > 0) {
84+
ws['!cols'] = columnsWidth;
85+
}
86+
7787
for (var R = 0; R != data.length; ++R, rowCount++) {
7888
for (var C = 0; C != data[R].length; ++C) {
7989
var cellRef = _xlsx2.default.utils.encode_cell({ c: C + xSteps, r: rowCount });

src/ExcelPlugin/utils/DataUtil.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,16 +51,26 @@ const excelSheetFromDataSet = (dataSet) => {
5151

5252
rowCount += ySteps;
5353

54+
var columnsWidth = []
5455
if (columns.length >= 0) {
5556
columns.forEach((col, index) => {
5657
var cellRef = XLSX.utils.encode_cell({c: xSteps + index, r: rowCount});
5758
fixRange(range, 0, 0, rowCount, xSteps, ySteps);
58-
getHeaderCell(col, cellRef, ws);
59+
var colTitle = col;
60+
if (typeof col === 'object'){
61+
colTitle = col.title;
62+
columnsWidth.push(col.width || {wpx:80}); /* wch (chars), wpx (pixels) - e.g. [{wch:6},{wpx:50}] */
63+
}
64+
getHeaderCell(colTitle, cellRef, ws);
5965
});
6066

6167
rowCount += 1;
6268
}
6369

70+
if (columnsWidth.length > 0){
71+
ws['!cols'] = columnsWidth;
72+
}
73+
6474
for (var R = 0; R != data.length; ++R, rowCount++) {
6575
for (var C = 0; C != data[R].length; ++C) {
6676
var cellRef = XLSX.utils.encode_cell({c: C + xSteps, r: rowCount});

0 commit comments

Comments
 (0)