to-spreadsheet 1.1.0 → 1.1.1

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 CHANGED
@@ -6,6 +6,8 @@ npm package to create spreadsheet in node environment
6
6
  [![minified](https://badgen.net/bundlephobia/min/to-spreadsheet)](https://bundlephobia.com/result?p=to-spreadsheet)
7
7
  [![minified + gzipped](https://badgen.net/bundlephobia/minzip/to-spreadsheet)](https://bundlephobia.com/result?p=to-spreadsheet)
8
8
 
9
+ [![GitHub stars](https://img.shields.io/github/stars/maifeeulasad/to-spreadsheet)](https://github.com/maifeeulasad/to-spreadsheet/stargazers)
10
+ [![GitHub watchers](https://img.shields.io/github/watchers/maifeeulasad/to-spreadsheet)](https://github.com/maifeeulasad/to-spreadsheet/watchers)
9
11
 
10
12
  # NPM
11
13
  ```
@@ -14,8 +16,10 @@ npm i to-spreadsheet
14
16
 
15
17
 
16
18
  # Usage
19
+ [![Open in CodeSandbox](https://img.shields.io/badge/Open%20in-CodeSandbox-blue?logo=codesandbox)](https://codesandbox.io/s/to-spreadsheet-example-hdmrvc?file=/src/App.tsx)
20
+
17
21
  ```ts
18
- import { generateExcel , EnvironmentType } from 'to-spreadsheet/lib/index';
22
+ import { generateExcel , EnvironmentType, skipCell } from 'to-spreadsheet/lib/index';
19
23
 
20
24
  const sampleData = [
21
25
  {
@@ -27,10 +31,10 @@ const sampleData = [
27
31
  [1, 2, 3],
28
32
  ]
29
33
  },
30
- { title: 'Maifee2', content: [[1], [1, 2]] },
31
- { title: 'Maifee3', content: [['meaw', "meaw"], ["woof", 'woof']] }
34
+ { title: 'Maifee2', content: [[1], [1, skipCell(3), 2]] },
35
+ { title: 'Maifee3', content: [['meaw', undefined, "meaw"], ["woof", 'woof']] }
32
36
  ]
33
37
 
34
38
  generateExcel(sampleData); // <-- by default generate XLSX for node
35
39
  generateExcel(sampleData, EnvironmentType.BROWSER); // <-- for browser
36
- ```
40
+ ```
@@ -12,6 +12,7 @@ const theme1_xml_1 = require("./xl/theme/theme1.xml");
12
12
  const workbook_xml_1 = require("./xl/workbook.xml");
13
13
  const sheet_xml_1 = require("./xl/worksheets/sheet.xml");
14
14
  const index_1 = require("./index");
15
+ const util_1 = require("./util");
15
16
  const generateTree = (workbook) => {
16
17
  return Object.assign({ "[Content_Types].xml": (0, content_types_xml_1.generateContentTypesXml)(workbook), "_rels/.rels": (0, _rels_1.generateRels)(), "docProps/app.xml": (0, app_xml_1.generateAppXml)(workbook), "docProps/core.xml": (0, core_xml_1.generateCoreXml)({}), "xl/_rels/workbook.xml.rels": (0, workbook_xml_rels_1.generateWorkBookXmlRels)(workbook), "xl/sharedStrings.xml": (0, sharedStrings_xml_1.generateSharedStrings)(workbook), "xl/styles.xml": (0, styles_xml_1.generateStyleXml)(), "xl/theme/theme1.xml": (0, theme1_xml_1.generateTheme1)(), "xl/workbook.xml": (0, workbook_xml_1.generateWorkBookXml)(workbook) }, workbook.sheets.reduce((acc, sheet, idx) => (Object.assign(Object.assign({}, acc), { [`xl/worksheets/sheet${idx + 1}.xml`]: (0, sheet_xml_1.generateSheetXml)(sheet) })), {}));
17
18
  };
@@ -21,19 +22,29 @@ var EnvironmentType;
21
22
  EnvironmentType[EnvironmentType["BROWSER"] = 1] = "BROWSER";
22
23
  })(EnvironmentType || (EnvironmentType = {}));
23
24
  exports.EnvironmentType = EnvironmentType;
24
- const generateExcel = (dump, environmentType = EnvironmentType.BROWSER) => {
25
+ const generateExcel = (dump, environmentType = EnvironmentType.NODE) => {
25
26
  const strings = [];
26
27
  const sheets = dump.map(({ title, content }) => {
27
28
  const rows = content.map(row => {
28
- const cells = row.map(content => {
29
- const isString = typeof content === 'string';
30
- const type = isString ? index_1.ICellType.string : index_1.ICellType.number;
31
- let value = isString ? strings.indexOf(content) : content;
32
- if (isString && value === -1) {
33
- strings.push(content);
34
- value = strings.length - 1;
29
+ const cells = row.flatMap(content => {
30
+ if (typeof content === 'number') {
31
+ return { type: index_1.ICellType.number, value: content };
32
+ }
33
+ else if (typeof content === 'string') {
34
+ const type = index_1.ICellType.string;
35
+ let value = strings.indexOf(content);
36
+ if (value === -1) {
37
+ strings.push(content);
38
+ value = strings.length - 1;
39
+ }
40
+ return { type: index_1.ICellType.string, value };
41
+ }
42
+ else if (content instanceof util_1.SkipCell) {
43
+ return new Array(content.getSkipCell()).fill({ type: index_1.ICellType.skip, value: undefined });
44
+ }
45
+ else {
46
+ return { type: index_1.ICellType.skip, value: undefined };
35
47
  }
36
- return { type, value };
37
48
  });
38
49
  return { cells };
39
50
  });
@@ -68,14 +79,14 @@ const generateExcelWorkbookNode = (workbook) => {
68
79
  output.on("end", () => {
69
80
  console.debug("Data has been drained");
70
81
  });
71
- archive.on("warning", function (err) {
82
+ archive.on("warning", (err) => {
72
83
  if (err.code === "ENOENT") {
73
84
  }
74
85
  else {
75
86
  reject(err);
76
87
  }
77
88
  });
78
- archive.on("error", function (err) {
89
+ archive.on("error", (err) => {
79
90
  reject(err);
80
91
  });
81
92
  archive.pipe(output);
package/lib/index.d.ts CHANGED
@@ -1,11 +1,13 @@
1
1
  import { generateExcel, EnvironmentType } from "./generate-excel";
2
+ import { SkipCell, skipCell } from "./util";
2
3
  declare enum ICellType {
3
4
  string = "s",
4
- number = "n"
5
+ number = "n",
6
+ skip = "skip"
5
7
  }
6
8
  interface ICell {
7
9
  type: ICellType;
8
- value: any;
10
+ value: number | undefined;
9
11
  }
10
12
  interface IRows {
11
13
  cells: ICell[];
@@ -21,11 +23,17 @@ interface IWorkbook {
21
23
  }
22
24
  interface IPage {
23
25
  title: string;
24
- content: (string | number)[][];
26
+ content: (string | number | undefined | SkipCell)[][];
25
27
  }
26
28
  export { ICell, ISheet, IWorkbook, IRows, ICellType, IPage };
27
- declare const sampleData: {
29
+ declare const sampleData: ({
28
30
  title: string;
29
31
  content: (string[] | number[])[];
30
- }[];
31
- export { generateExcel, sampleData, EnvironmentType };
32
+ } | {
33
+ title: string;
34
+ content: (number | SkipCell)[][];
35
+ } | {
36
+ title: string;
37
+ content: (string | undefined)[][];
38
+ })[];
39
+ export { generateExcel, sampleData, EnvironmentType, skipCell };
package/lib/index.js CHANGED
@@ -1,13 +1,16 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.EnvironmentType = exports.sampleData = exports.generateExcel = exports.ICellType = void 0;
3
+ exports.skipCell = exports.EnvironmentType = exports.sampleData = exports.generateExcel = exports.ICellType = void 0;
4
4
  const generate_excel_1 = require("./generate-excel");
5
5
  Object.defineProperty(exports, "generateExcel", { enumerable: true, get: function () { return generate_excel_1.generateExcel; } });
6
6
  Object.defineProperty(exports, "EnvironmentType", { enumerable: true, get: function () { return generate_excel_1.EnvironmentType; } });
7
+ const util_1 = require("./util");
8
+ Object.defineProperty(exports, "skipCell", { enumerable: true, get: function () { return util_1.skipCell; } });
7
9
  var ICellType;
8
10
  (function (ICellType) {
9
11
  ICellType["string"] = "s";
10
12
  ICellType["number"] = "n";
13
+ ICellType["skip"] = "skip";
11
14
  })(ICellType || (ICellType = {}));
12
15
  exports.ICellType = ICellType;
13
16
  const sampleData = [
@@ -20,7 +23,7 @@ const sampleData = [
20
23
  [1, 2, 3],
21
24
  ]
22
25
  },
23
- { title: 'Maifee2', content: [[1], [1, 2]] },
24
- { title: 'Maifee3', content: [['meaw', "meaw"], ["woof", 'woof']] }
26
+ { title: 'Maifee2', content: [[1], [1, (0, util_1.skipCell)(3), 2]] },
27
+ { title: 'Maifee3', content: [['meaw', undefined, "meaw"], ["woof", 'woof']] }
25
28
  ];
26
29
  exports.sampleData = sampleData;
package/lib/util.d.ts CHANGED
@@ -4,4 +4,10 @@ declare const indexToVbRelationIndex: (index: number) => number;
4
4
  declare const indexToRowIndex: (index: number) => string;
5
5
  declare const rowColumnToVbPosition: (row: number, col: number) => string;
6
6
  declare const calculateExtant: (rows: IRows[]) => string;
7
- export { indexToVbIndex, indexToVbRelationIndex, indexToRowIndex, rowColumnToVbPosition, calculateExtant };
7
+ declare class SkipCell {
8
+ private skipCell;
9
+ getSkipCell: () => number;
10
+ constructor(skipCell: number);
11
+ }
12
+ declare const skipCell: (skipCell: number) => SkipCell;
13
+ export { indexToVbIndex, indexToVbRelationIndex, indexToRowIndex, rowColumnToVbPosition, calculateExtant, SkipCell, skipCell };
package/lib/util.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.calculateExtant = exports.rowColumnToVbPosition = exports.indexToRowIndex = exports.indexToVbRelationIndex = exports.indexToVbIndex = void 0;
3
+ exports.skipCell = exports.SkipCell = exports.calculateExtant = exports.rowColumnToVbPosition = exports.indexToRowIndex = exports.indexToVbRelationIndex = exports.indexToVbIndex = void 0;
4
4
  const indexToVbIndex = (index) => index + 1;
5
5
  exports.indexToVbIndex = indexToVbIndex;
6
6
  const indexToVbRelationIndex = (index) => indexToVbIndex(index) + 2;
@@ -20,3 +20,12 @@ const rowColumnToVbPosition = (row, col) => indexToRowIndex(indexToVbIndex(row))
20
20
  exports.rowColumnToVbPosition = rowColumnToVbPosition;
21
21
  const calculateExtant = (rows) => rowColumnToVbPosition(Math.max(...rows.map(row => row.cells.length)) - 1, rows.length - 1);
22
22
  exports.calculateExtant = calculateExtant;
23
+ class SkipCell {
24
+ constructor(skipCell) {
25
+ this.getSkipCell = () => this.skipCell;
26
+ this.skipCell = skipCell;
27
+ }
28
+ }
29
+ exports.SkipCell = SkipCell;
30
+ const skipCell = (skipCell) => new SkipCell(skipCell);
31
+ exports.skipCell = skipCell;
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.generateSheetXml = void 0;
4
+ const __1 = require("../..");
4
5
  const util_1 = require("../../util");
5
6
  const generateSheetXml = (sheet) => {
6
7
  return `<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
@@ -20,12 +21,16 @@ const generateSheetXml = (sheet) => {
20
21
  </cols>
21
22
  <sheetData>
22
23
  ${(sheet.rows).map((row, indexCol) => `
23
- <row r="${(0, util_1.indexToVbIndex)(indexCol)}">
24
- ${(row.cells).map((cell, indexRow) => `
25
- <c r="${(0, util_1.rowColumnToVbPosition)(indexRow, indexCol)}" t="${cell.type}"><v>${cell.value}</v></c>
24
+ <row r="${(0, util_1.indexToVbIndex)(indexCol)}">
25
+ ${(row.cells).map((cell, indexRow) => cell.type === __1.ICellType.skip
26
+ ? ""
27
+ : `
28
+ <c r="${(0, util_1.rowColumnToVbPosition)(indexRow, indexCol)}" t="${cell.type}">
29
+ <v>${cell.value}</v>
30
+ </c>
31
+ `).join('\n')}
32
+ </row>
26
33
  `).join('\n')}
27
- </row>
28
- `).join('\n')}
29
34
  </sheetData>
30
35
  <pageMargins left="0.7" right="0.7" top="0.75" bottom="0.75" header="0.3" footer="0.3"/>
31
36
  </worksheet>
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "to-spreadsheet",
3
- "version": "1.1.0",
3
+ "version": "1.1.1",
4
4
  "description": "npm package to create spreadsheet in node environment",
5
5
  "main": "src/index.ts",
6
6
  "scripts": {
package/src/index.ts CHANGED
@@ -1,13 +1,15 @@
1
1
  import { generateExcel, EnvironmentType } from "./generate-excel";
2
+ import { SkipCell, skipCell } from "./util";
2
3
 
3
4
  enum ICellType {
4
5
  string = "s",
5
- number = "n"
6
+ number = "n",
7
+ skip = "skip",
6
8
  }
7
9
 
8
10
  interface ICell {
9
11
  type: ICellType;
10
- value: any;
12
+ value: number | undefined;
11
13
  }
12
14
 
13
15
  interface IRows {
@@ -27,7 +29,7 @@ interface IWorkbook {
27
29
 
28
30
  interface IPage {
29
31
  title: string
30
- content: (string | number)[][]
32
+ content: (string | number | undefined | SkipCell)[][]
31
33
  }
32
34
 
33
35
  export { ICell, ISheet, IWorkbook, IRows, ICellType, IPage }
@@ -43,8 +45,10 @@ const sampleData = [
43
45
  [1, 2, 3],
44
46
  ]
45
47
  },
46
- { title: 'Maifee2', content: [[1], [1, 2]] },
47
- { title: 'Maifee3', content: [['meaw', "meaw"], ["woof", 'woof']] }
48
+ { title: 'Maifee2', content: [[1], [1, skipCell(3), 2]] },
49
+ { title: 'Maifee3', content: [['meaw', undefined, "meaw"], ["woof", 'woof']] }
48
50
  ]
49
51
 
50
- export { generateExcel, sampleData, EnvironmentType };
52
+ // generateExcel(sampleData) // <- check before releasing with `yarn test:compile`
53
+
54
+ export { generateExcel, sampleData, EnvironmentType, skipCell };