json-as-xlsx 2.2.3 → 2.2.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/index.js CHANGED
@@ -1 +1 @@
1
- "use strict";exports.__esModule=true;exports.getWorksheetColumnWidths=exports.getJsonSheetRow=exports.getContentProperty=void 0;var xlsx_1=require("xlsx");function getContentProperty(content,property){function accessContentProperties(content,properties){var value=content[properties[0]];if(properties.length===1){return value!==null&&value!==void 0?value:""}if(value===undefined||typeof value==="string"||typeof value==="boolean"||typeof value==="number"||value instanceof Date){return""}return accessContentProperties(value,properties.slice(1))}var properties=property.split(".");return accessContentProperties(content,properties)}exports.getContentProperty=getContentProperty;function getJsonSheetRow(content,columns){var jsonSheetRow={};columns.forEach(function(column){if(typeof column.value==="function"){jsonSheetRow[column.label]=column.value(content)}else{jsonSheetRow[column.label]=getContentProperty(content,column.value)}});return jsonSheetRow}exports.getJsonSheetRow=getJsonSheetRow;function getWorksheetColumnWidths(worksheet,extraLength){var _a;if(extraLength===void 0){extraLength=1}var columnRange=xlsx_1.utils.decode_range((_a=worksheet["!ref"])!==null&&_a!==void 0?_a:"");var columnLetters=[];for(var C=columnRange.s.c;C<=columnRange.e.c;C++){var address=xlsx_1.utils.encode_col(C);columnLetters.push(address)}return columnLetters.map(function(column){var columnCells=Object.keys(worksheet).filter(function(cell){return cell.charAt(0)===column||cell.slice(0,2)===column});var maxWidthCell=columnCells.reduce(function(previousCell,currentCell){return worksheet[previousCell].v.length>worksheet[currentCell].v.length?previousCell:currentCell});return{width:worksheet[maxWidthCell].v.length+extraLength}})}exports.getWorksheetColumnWidths=getWorksheetColumnWidths;function getWorksheet(jsonSheet,settings){var jsonSheetRows=jsonSheet.content.map(function(contentItem){return getJsonSheetRow(contentItem,jsonSheet.columns)});var worksheet=xlsx_1.utils.json_to_sheet(jsonSheetRows);worksheet["!cols"]=getWorksheetColumnWidths(worksheet,settings.extraLength);return worksheet}function writeWorkbook(workbook,settings){var _a,_b;if(settings===void 0){settings={}}var filename=((_a=settings.fileName)!==null&&_a!==void 0?_a:"Spreadsheet")+".xlsx";var writeOptions=(_b=settings.writeOptions)!==null&&_b!==void 0?_b:{};return writeOptions.type==="buffer"?(0,xlsx_1.write)(workbook,writeOptions):(0,xlsx_1.writeFile)(workbook,filename,writeOptions)}function xlsx(jsonSheets,settings){if(settings===void 0){settings={}}if(jsonSheets.length===0){return}var workbook=xlsx_1.utils.book_new();jsonSheets.forEach(function(actualSheet,actualIndex){var _a;var worksheet=getWorksheet(actualSheet,settings);var worksheetName=(_a=actualSheet.sheet)!==null&&_a!==void 0?_a:"Sheet "+(actualIndex+1);xlsx_1.utils.book_append_sheet(workbook,worksheet,worksheetName)});return writeWorkbook(workbook,settings)}exports["default"]=xlsx;module.exports=xlsx;module.exports.getContentProperty=getContentProperty;module.exports.getJsonSheetRow=getJsonSheetRow;module.exports.getWorksheetColumnWidths=getWorksheetColumnWidths;
1
+ "use strict";exports.__esModule=true;exports.getWorksheetColumnWidths=exports.getJsonSheetRow=exports.getContentProperty=void 0;var xlsx_1=require("xlsx");function getContentProperty(content,property){function accessContentProperties(content,properties){var value=content[properties[0]];if(properties.length===1){return value!==null&&value!==void 0?value:""}if(value===undefined||typeof value==="string"||typeof value==="boolean"||typeof value==="number"||value instanceof Date){return""}return accessContentProperties(value,properties.slice(1))}var properties=property.split(".");return accessContentProperties(content,properties)}exports.getContentProperty=getContentProperty;function getJsonSheetRow(content,columns){var jsonSheetRow={};columns.forEach(function(column){if(typeof column.value==="function"){jsonSheetRow[column.label]=column.value(content)}else{jsonSheetRow[column.label]=getContentProperty(content,column.value)}});return jsonSheetRow}exports.getJsonSheetRow=getJsonSheetRow;function getWorksheetColumnWidths(worksheet,extraLength){var _a;if(extraLength===void 0){extraLength=1}var columnRange=xlsx_1.utils.decode_range((_a=worksheet["!ref"])!==null&&_a!==void 0?_a:"");var columnLetters=[];for(var C=columnRange.s.c;C<=columnRange.e.c;C++){var address=xlsx_1.utils.encode_col(C);columnLetters.push(address)}return columnLetters.map(function(column){var columnCells=Object.keys(worksheet).filter(function(cell){return cell.charAt(0)===column||cell.slice(0,2)===column});var maxWidthCell=columnCells.reduce(function(previousCell,currentCell){return worksheet[previousCell].v.length>worksheet[currentCell].v.length?previousCell:currentCell});return{width:worksheet[maxWidthCell].v.length+extraLength}})}exports.getWorksheetColumnWidths=getWorksheetColumnWidths;function getWorksheet(jsonSheet,settings){var jsonSheetRows;if(jsonSheet.content.length>0){jsonSheetRows=jsonSheet.content.map(function(contentItem){return getJsonSheetRow(contentItem,jsonSheet.columns)})}else{jsonSheetRows=jsonSheet.columns.map(function(column){var _a;return _a={},_a[column.label]="",_a})}var worksheet=xlsx_1.utils.json_to_sheet(jsonSheetRows);worksheet["!cols"]=getWorksheetColumnWidths(worksheet,settings.extraLength);return worksheet}function writeWorkbook(workbook,settings){var _a,_b;if(settings===void 0){settings={}}var filename=((_a=settings.fileName)!==null&&_a!==void 0?_a:"Spreadsheet")+".xlsx";var writeOptions=(_b=settings.writeOptions)!==null&&_b!==void 0?_b:{};return writeOptions.type==="buffer"?(0,xlsx_1.write)(workbook,writeOptions):(0,xlsx_1.writeFile)(workbook,filename,writeOptions)}function xlsx(jsonSheets,settings){if(settings===void 0){settings={}}if(jsonSheets.length===0){return}var workbook=xlsx_1.utils.book_new();jsonSheets.forEach(function(actualSheet,actualIndex){var _a;var worksheet=getWorksheet(actualSheet,settings);var worksheetName=(_a=actualSheet.sheet)!==null&&_a!==void 0?_a:"Sheet "+(actualIndex+1);xlsx_1.utils.book_append_sheet(workbook,worksheet,worksheetName)});return writeWorkbook(workbook,settings)}exports["default"]=xlsx;module.exports=xlsx;module.exports.getContentProperty=getContentProperty;module.exports.getJsonSheetRow=getJsonSheetRow;module.exports.getWorksheetColumnWidths=getWorksheetColumnWidths;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "json-as-xlsx",
3
- "version": "2.2.3",
3
+ "version": "2.2.4",
4
4
  "main": "index.js",
5
5
  "license": "MIT",
6
6
  "description": "Create excel from json",
package/types/index.d.ts CHANGED
@@ -11,8 +11,8 @@ export interface IContent {
11
11
 
12
12
  export interface IJsonSheet {
13
13
  sheet?: string
14
- columns: [IColumn, ...IColumn[]]
15
- content: [IContent, ...IContent[]]
14
+ columns: IColumn[]
15
+ content: IContent[]
16
16
  }
17
17
 
18
18
  export interface ISettings {