From dc6dc60bd7012778fcdb5c41c81b4c752d966ed4 Mon Sep 17 00:00:00 2001 From: Boaz Adato Date: Tue, 15 May 2018 21:34:06 +0300 Subject: [PATCH 1/2] 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} --- dist/ExcelPlugin/utils/DataUtil.js | 12 +++++++++++- src/ExcelPlugin/utils/DataUtil.js | 12 +++++++++++- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/dist/ExcelPlugin/utils/DataUtil.js b/dist/ExcelPlugin/utils/DataUtil.js index 0c9211fd..5fcbd066 100644 --- a/dist/ExcelPlugin/utils/DataUtil.js +++ b/dist/ExcelPlugin/utils/DataUtil.js @@ -64,16 +64,26 @@ var excelSheetFromDataSet = function excelSheetFromDataSet(dataSet) { rowCount += ySteps; + var columnsWidth = []; if (columns.length >= 0) { columns.forEach(function (col, index) { var cellRef = _xlsx2.default.utils.encode_cell({ c: xSteps + index, r: rowCount }); fixRange(range, 0, 0, rowCount, xSteps, ySteps); - getHeaderCell(col, cellRef, ws); + var colTitle = col; + if ((typeof col === 'undefined' ? 'undefined' : _typeof(col)) === 'object') { + colTitle = col.title; + columnsWidth.push(col.width || { wpx: 80 }); /* wch (chars), wpx (pixels) - e.g. [{wch:6},{wpx:50}] */ + } + getHeaderCell(colTitle, cellRef, ws); }); rowCount += 1; } + if (columnsWidth.length > 0) { + ws['!cols'] = columnsWidth; + } + for (var R = 0; R != data.length; ++R, rowCount++) { for (var C = 0; C != data[R].length; ++C) { var cellRef = _xlsx2.default.utils.encode_cell({ c: C + xSteps, r: rowCount }); diff --git a/src/ExcelPlugin/utils/DataUtil.js b/src/ExcelPlugin/utils/DataUtil.js index 13f6b110..861836b4 100644 --- a/src/ExcelPlugin/utils/DataUtil.js +++ b/src/ExcelPlugin/utils/DataUtil.js @@ -51,16 +51,26 @@ const excelSheetFromDataSet = (dataSet) => { rowCount += ySteps; + var columnsWidth = [] if (columns.length >= 0) { columns.forEach((col, index) => { var cellRef = XLSX.utils.encode_cell({c: xSteps + index, r: rowCount}); fixRange(range, 0, 0, rowCount, xSteps, ySteps); - getHeaderCell(col, cellRef, ws); + var colTitle = col; + if (typeof col === 'object'){ + colTitle = col.title; + columnsWidth.push(col.width || {wpx:80}); /* wch (chars), wpx (pixels) - e.g. [{wch:6},{wpx:50}] */ + } + getHeaderCell(colTitle, cellRef, ws); }); rowCount += 1; } + if (columnsWidth.length > 0){ + ws['!cols'] = columnsWidth; + } + for (var R = 0; R != data.length; ++R, rowCount++) { for (var C = 0; C != data[R].length; ++C) { var cellRef = XLSX.utils.encode_cell({c: C + xSteps, r: rowCount}); From 4863aaf10f3bce9823167ddb9fb49e45a58546a7 Mon Sep 17 00:00:00 2001 From: Boaz Adato Date: Tue, 15 May 2018 21:41:23 +0300 Subject: [PATCH 2/2] feat: update example with col widths --- examples/styled_excel_sheet.md | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/examples/styled_excel_sheet.md b/examples/styled_excel_sheet.md index 37bce9dc..20d3cb97 100644 --- a/examples/styled_excel_sheet.md +++ b/examples/styled_excel_sheet.md @@ -8,7 +8,11 @@ const ExcelSheet = ReactExport.ExcelFile.ExcelSheet; const multiDataSet = [ { - columns: ["Headings", "Text Style", "Colors"], + columns: [ + {title: "Headings", width: {wpx: 80}},//pixels width + {title: "Text Style", width: {wch: 40}},//char width + {title: "Colors", width: {wpx: 90}}, + ], data: [ [ {value: "H1", style: {font: {sz: "24", bold: true}}},