Skip to content

Commit 7c0baae

Browse files
Merge pull request #52 from boazadato/support-col-wid
feat: support col width
2 parents 6f64d67 + fe46de8 commit 7c0baae

File tree

3 files changed

+27
-3
lines changed

3 files changed

+27
-3
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 });

examples/styled_excel_sheet.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,11 @@ const ExcelSheet = ReactExport.ExcelFile.ExcelSheet;
88

99
const multiDataSet = [
1010
{
11-
columns: ["Headings", "Text Style", "Colors"],
11+
columns: [
12+
{title: "Headings", width: {wpx: 80}},//pixels width
13+
{title: "Text Style", width: {wch: 40}},//char width
14+
{title: "Colors", width: {wpx: 90}},
15+
],
1216
data: [
1317
[
1418
{value: "H1", style: {font: {sz: "24", bold: true}}},

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)