json-as-xlsx 2.4.3 → 2.4.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/README.md +3 -0
- package/package.json +1 -1
- package/src/index.js +1 -1
- package/types/index.d.ts +1 -0
package/README.md
CHANGED
|
@@ -11,6 +11,8 @@ You can see a live example of it working on any of this sites (there are many ju
|
|
|
11
11
|
## Usage
|
|
12
12
|
|
|
13
13
|
```js
|
|
14
|
+
import xlsx from "json-as-xlsx"
|
|
15
|
+
// or require
|
|
14
16
|
let xlsx = require("json-as-xlsx")
|
|
15
17
|
|
|
16
18
|
let data = [
|
|
@@ -43,6 +45,7 @@ let data = [
|
|
|
43
45
|
let settings = {
|
|
44
46
|
fileName: "MySpreadsheet", // Name of the resulting spreadsheet
|
|
45
47
|
extraLength: 3, // A bigger number means that columns will be wider
|
|
48
|
+
writeMode: 'writeFile', // The available parameters are 'WriteFile' and 'write'. This setting is optional. Useful in such cases https://docs.sheetjs.com/docs/solutions/output#example-remote-file
|
|
46
49
|
writeOptions: {}, // Style options from https://github.com/SheetJS/sheetjs#writing-options
|
|
47
50
|
RTL: true, // Display the columns from right-to-left (the default value is false)
|
|
48
51
|
}
|
package/package.json
CHANGED
package/src/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";exports.__esModule=true;exports.utils=exports.getWorksheetColumnWidths=exports.getJsonSheetRow=exports.getContentProperty=void 0;var xlsx_mini_min_1=require("xlsx/dist/xlsx.mini.min");exports.utils=xlsx_mini_min_1.utils;var getContentProperty=function(content,property){var accessContentProperties=function(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;var getJsonSheetRow=function(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;var applyColumnFormat=function(worksheet,columnIds,columnFormats){var _a;for(var i=0;i<columnIds.length;i+=1){var columnFormat=columnFormats[i];if(!columnFormat){continue}var column=xlsx_mini_min_1.utils.decode_col(columnIds[i]);var range=xlsx_mini_min_1.utils.decode_range((_a=worksheet["!ref"])!==null&&_a!==void 0?_a:"");for(var row=range.s.r+1;row<=range.e.r;++row){var ref=xlsx_mini_min_1.utils.encode_cell({r:row,c:column});if(worksheet[ref]){worksheet[ref].z=columnFormat}}}};var getWorksheetColumnIds=function(worksheet){var _a;var columnRange=xlsx_mini_min_1.utils.decode_range((_a=worksheet["!ref"])!==null&&_a!==void 0?_a:"");var columnIds=[];for(var C=columnRange.s.c;C<=columnRange.e.c;C++){var address=xlsx_mini_min_1.utils.encode_col(C);columnIds.push(address)}return columnIds};var getObjectLength=function(object){if(typeof object==="string"){return object.length}if(typeof object==="number"){return object.toString().length}if(typeof object==="boolean"){return object?"true".length:"false".length}if(object instanceof Date){return object.toString().length}return 0};var getWorksheetColumnWidths=function(worksheet,extraLength){if(extraLength===void 0){extraLength=1}var columnLetters=getWorksheetColumnIds(worksheet);return columnLetters.map(function(column){var columnCells=Object.keys(worksheet).filter(function(cell){return cell.replace(/[0-9]/g,"")===column});var maxWidthCell=columnCells.reduce(function(maxWidth,cellId){var cell=worksheet[cellId];var cellContentLength=getObjectLength(cell.v);if(!cell.z){return Math.max(maxWidth,cellContentLength)}var cellFormatLength=cell.z.length;var largestWidth=Math.max(cellContentLength,cellFormatLength);return Math.max(maxWidth,largestWidth)},0);return{width:maxWidthCell+extraLength}})};exports.getWorksheetColumnWidths=getWorksheetColumnWidths;var getWorksheet=function(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_mini_min_1.utils.json_to_sheet(jsonSheetRows);var worksheetColumnIds=getWorksheetColumnIds(worksheet);var worksheetColumnFormats=jsonSheet.columns.map(function(jsonSheetColumn){var _a;return(_a=jsonSheetColumn.format)!==null&&_a!==void 0?_a:null});applyColumnFormat(worksheet,worksheetColumnIds,worksheetColumnFormats);worksheet["!cols"]=getWorksheetColumnWidths(worksheet,settings.extraLength);return worksheet};var writeWorkbook=function(workbook,settings){var _a,_b,_c,_d;var _e;if(settings===void 0){settings={}}var RTL=Boolean(settings.RTL);(_a=workbook.Workbook)!==null&&_a!==void 0?_a:workbook.Workbook={};(_b=(_e=workbook.Workbook).Views)!==null&&_b!==void 0?_b:_e.Views=[{}];workbook.Workbook.Views.forEach(function(view){view.RTL=RTL});var filename="".concat((_c=settings.fileName)!==null&&_c!==void 0?_c:"Spreadsheet",".xlsx");var writeOptions=(_d=settings.writeOptions)!==null&&_d!==void 0?_d:{};return writeOptions.type==="buffer"?(0,xlsx_mini_min_1.write)(workbook,writeOptions):(0,xlsx_mini_min_1.writeFile)(workbook,filename,writeOptions)};var xlsx=function(jsonSheets,settings,workbookCallback){if(settings===void 0){settings={}}if(workbookCallback===void 0){workbookCallback=function(){}}if(jsonSheets.length===0)return;var workbook=xlsx_mini_min_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 ".concat(actualIndex+1);xlsx_mini_min_1.utils.book_append_sheet(workbook,worksheet,worksheetName)});workbookCallback(workbook);return writeWorkbook(workbook,settings)};exports["default"]=xlsx;module.exports=xlsx;module.exports.getContentProperty=getContentProperty;module.exports.getJsonSheetRow=getJsonSheetRow;module.exports.getWorksheetColumnWidths=getWorksheetColumnWidths;module.exports.utils=xlsx_mini_min_1.utils;
|
|
1
|
+
"use strict";exports.__esModule=true;exports.utils=exports.getWorksheetColumnWidths=exports.getJsonSheetRow=exports.getContentProperty=void 0;var xlsx_mini_min_1=require("xlsx/dist/xlsx.mini.min");exports.utils=xlsx_mini_min_1.utils;var getContentProperty=function(content,property){var accessContentProperties=function(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;var getJsonSheetRow=function(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;var applyColumnFormat=function(worksheet,columnIds,columnFormats){var _a;for(var i=0;i<columnIds.length;i+=1){var columnFormat=columnFormats[i];if(!columnFormat){continue}var column=xlsx_mini_min_1.utils.decode_col(columnIds[i]);var range=xlsx_mini_min_1.utils.decode_range((_a=worksheet["!ref"])!==null&&_a!==void 0?_a:"");for(var row=range.s.r+1;row<=range.e.r;++row){var ref=xlsx_mini_min_1.utils.encode_cell({r:row,c:column});if(worksheet[ref]){worksheet[ref].z=columnFormat}}}};var getWorksheetColumnIds=function(worksheet){var _a;var columnRange=xlsx_mini_min_1.utils.decode_range((_a=worksheet["!ref"])!==null&&_a!==void 0?_a:"");var columnIds=[];for(var C=columnRange.s.c;C<=columnRange.e.c;C++){var address=xlsx_mini_min_1.utils.encode_col(C);columnIds.push(address)}return columnIds};var getObjectLength=function(object){if(typeof object==="string"){return object.length}if(typeof object==="number"){return object.toString().length}if(typeof object==="boolean"){return object?"true".length:"false".length}if(object instanceof Date){return object.toString().length}return 0};var getWorksheetColumnWidths=function(worksheet,extraLength){if(extraLength===void 0){extraLength=1}var columnLetters=getWorksheetColumnIds(worksheet);return columnLetters.map(function(column){var columnCells=Object.keys(worksheet).filter(function(cell){return cell.replace(/[0-9]/g,"")===column});var maxWidthCell=columnCells.reduce(function(maxWidth,cellId){var cell=worksheet[cellId];var cellContentLength=getObjectLength(cell.v);if(!cell.z){return Math.max(maxWidth,cellContentLength)}var cellFormatLength=cell.z.length;var largestWidth=Math.max(cellContentLength,cellFormatLength);return Math.max(maxWidth,largestWidth)},0);return{width:maxWidthCell+extraLength}})};exports.getWorksheetColumnWidths=getWorksheetColumnWidths;var getWorksheet=function(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_mini_min_1.utils.json_to_sheet(jsonSheetRows);var worksheetColumnIds=getWorksheetColumnIds(worksheet);var worksheetColumnFormats=jsonSheet.columns.map(function(jsonSheetColumn){var _a;return(_a=jsonSheetColumn.format)!==null&&_a!==void 0?_a:null});applyColumnFormat(worksheet,worksheetColumnIds,worksheetColumnFormats);worksheet["!cols"]=getWorksheetColumnWidths(worksheet,settings.extraLength);return worksheet};var writeWorkbook=function(workbook,settings){var _a,_b,_c,_d;var _e;if(settings===void 0){settings={}}var RTL=Boolean(settings.RTL);(_a=workbook.Workbook)!==null&&_a!==void 0?_a:workbook.Workbook={};(_b=(_e=workbook.Workbook).Views)!==null&&_b!==void 0?_b:_e.Views=[{}];workbook.Workbook.Views.forEach(function(view){view.RTL=RTL});var filename="".concat((_c=settings.fileName)!==null&&_c!==void 0?_c:"Spreadsheet",".xlsx");var writeOptions=(_d=settings.writeOptions)!==null&&_d!==void 0?_d:{};if(settings.writeMode==="write"){return(0,xlsx_mini_min_1.write)(workbook,writeOptions)}else if(settings.writeMode==="writeFile"){return(0,xlsx_mini_min_1.writeFile)(workbook,filename,writeOptions)}else{return writeOptions.type==="buffer"?(0,xlsx_mini_min_1.write)(workbook,writeOptions):(0,xlsx_mini_min_1.writeFile)(workbook,filename,writeOptions)}};var xlsx=function(jsonSheets,settings,workbookCallback){if(settings===void 0){settings={}}if(workbookCallback===void 0){workbookCallback=function(){}}if(jsonSheets.length===0)return;var workbook=xlsx_mini_min_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 ".concat(actualIndex+1);xlsx_mini_min_1.utils.book_append_sheet(workbook,worksheet,worksheetName)});workbookCallback(workbook);return writeWorkbook(workbook,settings)};exports["default"]=xlsx;module.exports=xlsx;module.exports.getContentProperty=getContentProperty;module.exports.getJsonSheetRow=getJsonSheetRow;module.exports.getWorksheetColumnWidths=getWorksheetColumnWidths;module.exports.utils=xlsx_mini_min_1.utils;
|