to-spreadsheet 0.0.3 → 1.0.0

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.
Files changed (38) hide show
  1. package/README.md +8 -0
  2. package/lib/_rels/.rels.d.ts +2 -0
  3. package/{src/_rels/.rels.ts → lib/_rels/.rels.js} +5 -4
  4. package/lib/content-types.xml.d.ts +3 -0
  5. package/lib/content-types.xml.js +20 -0
  6. package/lib/docProps/app.xml.d.ts +3 -0
  7. package/lib/docProps/app.xml.js +50 -0
  8. package/lib/docProps/core.xml.d.ts +10 -0
  9. package/lib/docProps/core.xml.js +32 -0
  10. package/lib/generate-excel.d.ts +4 -0
  11. package/lib/generate-excel.js +74 -0
  12. package/lib/index.d.ts +31 -0
  13. package/lib/index.js +26 -0
  14. package/lib/util.d.ts +7 -0
  15. package/lib/util.js +22 -0
  16. package/lib/xl/_rels/workbook.xml.rels.d.ts +3 -0
  17. package/lib/xl/_rels/workbook.xml.rels.js +14 -0
  18. package/lib/xl/sharedStrings.xml.d.ts +3 -0
  19. package/lib/xl/sharedStrings.xml.js +13 -0
  20. package/lib/xl/styles.xml.d.ts +2 -0
  21. package/{src/xl/styles.xml.ts → lib/xl/styles.xml.js} +5 -4
  22. package/lib/xl/theme/theme1.xml.d.ts +2 -0
  23. package/{src/xl/theme/theme1.xml.ts → lib/xl/theme/theme1.xml.js} +5 -4
  24. package/lib/xl/workbook.xml.d.ts +3 -0
  25. package/{src/xl/workbook.xml.ts → lib/xl/workbook.xml.js} +14 -9
  26. package/lib/xl/worksheets/sheet.xml.d.ts +3 -0
  27. package/lib/xl/worksheets/sheet.xml.js +34 -0
  28. package/package.json +6 -3
  29. package/src/index.ts +52 -44
  30. package/.github/workflows/automation.yaml +0 -29
  31. package/DEVELOPMENT.md +0 -24
  32. package/src/content-types.xml.ts +0 -15
  33. package/src/docProps/app.xml.ts +0 -51
  34. package/src/docProps/core.xml.ts +0 -24
  35. package/src/generate-excel.ts +0 -66
  36. package/src/xl/_rels/workbook.xml.rels.ts +0 -10
  37. package/src/xl/worksheets/sheet1.xml.ts +0 -54
  38. package/tsconfig.json +0 -8
package/README.md CHANGED
@@ -1,2 +1,10 @@
1
1
  # to-spreadsheet
2
2
  npm to package to create spreadsheet in node environment
3
+
4
+
5
+ # NPM
6
+ ```
7
+ npm i to-spreadsheet
8
+ ```
9
+
10
+ https://www.npmjs.com/package/to-spreadsheet
@@ -0,0 +1,2 @@
1
+ declare const generateRels: () => string;
2
+ export { generateRels };
@@ -1,10 +1,11 @@
1
- const generateRels = () =>
2
- `<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.generateRels = void 0;
4
+ const generateRels = () => `<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
3
5
  <Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">
4
6
  <Relationship Id="rId3" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/extended-properties" Target="docProps/app.xml" />
5
7
  <Relationship Id="rId2" Type="http://schemas.openxmlformats.org/package/2006/relationships/metadata/core-properties" Target="docProps/core.xml" />
6
8
  <Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument" Target="xl/workbook.xml" />
7
9
  </Relationships>
8
10
  `;
9
-
10
- export { generateRels };
11
+ exports.generateRels = generateRels;
@@ -0,0 +1,3 @@
1
+ import { IWorkbook } from "./index";
2
+ declare const generateContentTypesXml: (workbook: IWorkbook) => string;
3
+ export { generateContentTypesXml };
@@ -0,0 +1,20 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.generateContentTypesXml = void 0;
4
+ const util_1 = require("./util");
5
+ const generateContentTypesXml = (workbook) => `<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
6
+ <Types xmlns="http://schemas.openxmlformats.org/package/2006/content-types">
7
+ <Default Extension="xml" ContentType="application/xml"/>
8
+ <Default Extension="rels" ContentType="application/vnd.openxmlformats-package.relationships+xml"/>
9
+ <Override PartName="/xl/_rels/workbook.xml.rels" ContentType="application/vnd.openxmlformats-package.relationships+xml"/>
10
+ <Override PartName="/xl/sharedStrings.xml" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.sharedStrings+xml"/>
11
+ ${workbook.sheets.map((_, index) => `
12
+ <Override PartName="/xl/worksheets/sheet${(0, util_1.indexToVbIndex)(index)}.xml" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet+xml"/>
13
+ `).join('\n')}
14
+ <Override PartName="/xl/workbook.xml" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml"/>
15
+ <Override PartName="/xl/styles.xml" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.styles+xml"/>
16
+ <Override PartName="/_rels/.rels" ContentType="application/vnd.openxmlformats-package.relationships+xml"/>
17
+ <Override PartName="/docProps/app.xml" ContentType="application/vnd.openxmlformats-officedocument.extended-properties+xml"/>
18
+ <Override PartName="/docProps/core.xml" ContentType="application/vnd.openxmlformats-package.core-properties+xml"/>
19
+ </Types>`;
20
+ exports.generateContentTypesXml = generateContentTypesXml;
@@ -0,0 +1,3 @@
1
+ import { IWorkbook } from '../index';
2
+ declare const generateAppXml: (workbook: IWorkbook) => string;
3
+ export { generateAppXml };
@@ -0,0 +1,50 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.generateAppXml = void 0;
4
+ const generateAppXml = (workbook) => `
5
+ <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
6
+ <Properties xmlns="http://schemas.openxmlformats.org/officeDocument/2006/extended-properties"
7
+ xmlns:vt="http://schemas.openxmlformats.org/officeDocument/2006/docPropsVTypes">
8
+ <Template></Template>
9
+ <TotalTime>0</TotalTime>
10
+ <Application>
11
+ Microsoft Excel
12
+ </Application>
13
+ <DocSecurity>
14
+ 0
15
+ </DocSecurity>
16
+ <ScaleCrop>
17
+ false
18
+ </ScaleCrop>
19
+ <HeadingPairs>
20
+ <vt:vector size="${workbook.sheets.length}" baseType="variant">
21
+ <vt:variant>
22
+ <vt:lpstr>Worksheets</vt:lpstr>
23
+ </vt:variant>
24
+ <vt:variant>
25
+ <vt:i4>${workbook.sheets.length}</vt:i4>
26
+ </vt:variant>
27
+ </vt:vector>
28
+ </HeadingPairs>
29
+ <TitlesOfParts>
30
+ <vt:vector size="${workbook.sheets.length}" baseType="lpstr">
31
+ ${workbook.sheets.map(sheet => `
32
+ <vt:lpstr>${sheet.title}</vt:lpstr>
33
+ `)}
34
+ </vt:vector>
35
+ </TitlesOfParts>
36
+ <LinksUpToDate>
37
+ false
38
+ </LinksUpToDate>
39
+ <SharedDoc>
40
+ false
41
+ </SharedDoc>
42
+ <HyperlinksChanged>
43
+ false
44
+ </HyperlinksChanged>
45
+ <AppVersion>
46
+ 16.0300
47
+ </AppVersion>
48
+ </Properties>
49
+ `;
50
+ exports.generateAppXml = generateAppXml;
@@ -0,0 +1,10 @@
1
+ interface IGenerateCoreXml {
2
+ username?: string;
3
+ description?: string;
4
+ language?: string;
5
+ version?: string;
6
+ subject?: string;
7
+ title?: string;
8
+ }
9
+ declare const generateCoreXml: ({ username, description, language, version, subject, title }: IGenerateCoreXml) => string;
10
+ export { generateCoreXml };
@@ -0,0 +1,32 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.generateCoreXml = void 0;
4
+ const date_fns_1 = require("date-fns");
5
+ const generateCoreXml = ({ username = "to-spreadsheet", description = "", language = "en-US", version = "1", subject = "", title = "" }) => {
6
+ const date = (0, date_fns_1.format)(Date.now(), "yyyy-MM-dd") +
7
+ "T" +
8
+ (0, date_fns_1.format)(Date.now(), "HH:mm:ss") +
9
+ "Z";
10
+ return `<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
11
+ <cp:coreProperties
12
+ xmlns:cp="http://schemas.openxmlformats.org/package/2006/metadata/core-properties"
13
+ xmlns:dc="http://purl.org/dc/elements/1.1/"
14
+ xmlns:dcterms="http://purl.org/dc/terms/"
15
+ xmlns:dcmitype="http://purl.org/dc/dcmitype/"
16
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
17
+ <dcterms:created xsi:type="dcterms:W3CDTF">${date}</dcterms:created>
18
+ <dc:creator>
19
+ ${username}
20
+ </dc:creator>
21
+ <dc:description>
22
+ ${description}</dc:description>
23
+ <dc:language>${language}</dc:language>
24
+ <cp:lastModifiedBy>${date}</cp:lastModifiedBy>
25
+ <dcterms:modified xsi:type="dcterms:W3CDTF">${date}</dcterms:modified>
26
+ <cp:revision>${version}</cp:revision>
27
+ <dc:subject>${subject}</dc:subject>
28
+ <dc:title>${title}</dc:title>
29
+ </cp:coreProperties>
30
+ `;
31
+ };
32
+ exports.generateCoreXml = generateCoreXml;
@@ -0,0 +1,4 @@
1
+ import { IPage, IWorkbook } from "./index";
2
+ declare const generateExcel: (dump: IPage[]) => void;
3
+ declare const generateExcelWorkbook: (workbook: IWorkbook) => void;
4
+ export { generateExcel, generateExcelWorkbook };
@@ -0,0 +1,74 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.generateExcelWorkbook = exports.generateExcel = void 0;
4
+ const content_types_xml_1 = require("./content-types.xml");
5
+ const _rels_1 = require("./_rels/.rels");
6
+ const app_xml_1 = require("./docProps/app.xml");
7
+ const core_xml_1 = require("./docProps/core.xml");
8
+ const workbook_xml_rels_1 = require("./xl/_rels/workbook.xml.rels");
9
+ const sharedStrings_xml_1 = require("./xl/sharedStrings.xml");
10
+ const styles_xml_1 = require("./xl/styles.xml");
11
+ const theme1_xml_1 = require("./xl/theme/theme1.xml");
12
+ const workbook_xml_1 = require("./xl/workbook.xml");
13
+ const sheet_xml_1 = require("./xl/worksheets/sheet.xml");
14
+ const index_1 = require("./index");
15
+ const fs = require("fs");
16
+ const archiver = require("archiver");
17
+ const generateTree = (workbook) => {
18
+ 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) })), {}));
19
+ };
20
+ const generateExcel = (dump) => {
21
+ const strings = [];
22
+ const sheets = dump.map(({ title, content }) => {
23
+ const rows = content.map(row => {
24
+ const cells = row.map(content => {
25
+ const isString = typeof content === 'string';
26
+ const type = isString ? index_1.ICellType.string : index_1.ICellType.number;
27
+ let value = isString ? strings.indexOf(content) : content;
28
+ if (isString && value === -1) {
29
+ strings.push(content);
30
+ value = strings.length - 1;
31
+ }
32
+ return { type, value };
33
+ });
34
+ return { cells };
35
+ });
36
+ return { title, rows };
37
+ });
38
+ const workbook = {
39
+ sheets,
40
+ strings,
41
+ filename: "tem.xlsx"
42
+ };
43
+ generateExcelWorkbook(workbook);
44
+ };
45
+ exports.generateExcel = generateExcel;
46
+ const generateExcelWorkbook = (workbook) => {
47
+ const output = fs.createWriteStream(`${__dirname}/${workbook.filename}.xlsx`);
48
+ const archive = archiver("zip", {
49
+ zlib: { level: 9 },
50
+ });
51
+ output.on("close", function () {
52
+ console.debug(archive.pointer() + " total bytes");
53
+ console.debug("archiver has been finalized and the output file descriptor has closed.");
54
+ });
55
+ output.on("end", function () {
56
+ console.debug("Data has been drained");
57
+ });
58
+ archive.on("warning", function (err) {
59
+ if (err.code === "ENOENT") {
60
+ }
61
+ else {
62
+ throw err;
63
+ }
64
+ });
65
+ archive.on("error", function (err) {
66
+ throw err;
67
+ });
68
+ archive.pipe(output);
69
+ Object.entries(generateTree(workbook)).map(([filename, fileContent]) => {
70
+ archive.append(fileContent, { name: filename });
71
+ });
72
+ archive.finalize();
73
+ };
74
+ exports.generateExcelWorkbook = generateExcelWorkbook;
package/lib/index.d.ts ADDED
@@ -0,0 +1,31 @@
1
+ import { generateExcel, generateExcelWorkbook } from "./generate-excel";
2
+ declare enum ICellType {
3
+ string = "s",
4
+ number = "n"
5
+ }
6
+ interface ICell {
7
+ type: ICellType;
8
+ value: any;
9
+ }
10
+ interface IRows {
11
+ cells: ICell[];
12
+ }
13
+ interface ISheet {
14
+ title: string;
15
+ rows: IRows[];
16
+ }
17
+ interface IWorkbook {
18
+ filename: string;
19
+ sheets: ISheet[];
20
+ strings: string[];
21
+ }
22
+ interface IPage {
23
+ title: string;
24
+ content: (string | number)[][];
25
+ }
26
+ export { ICell, ISheet, IWorkbook, IRows, ICellType, IPage };
27
+ declare const sampleData: {
28
+ title: string;
29
+ content: (string[] | number[])[];
30
+ }[];
31
+ export { generateExcel, generateExcelWorkbook, sampleData };
package/lib/index.js ADDED
@@ -0,0 +1,26 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.sampleData = exports.generateExcelWorkbook = exports.generateExcel = exports.ICellType = void 0;
4
+ const generate_excel_1 = require("./generate-excel");
5
+ Object.defineProperty(exports, "generateExcel", { enumerable: true, get: function () { return generate_excel_1.generateExcel; } });
6
+ Object.defineProperty(exports, "generateExcelWorkbook", { enumerable: true, get: function () { return generate_excel_1.generateExcelWorkbook; } });
7
+ var ICellType;
8
+ (function (ICellType) {
9
+ ICellType["string"] = "s";
10
+ ICellType["number"] = "n";
11
+ })(ICellType || (ICellType = {}));
12
+ exports.ICellType = ICellType;
13
+ const sampleData = [
14
+ {
15
+ title: 'Maifee1', content: [
16
+ ['meaw', 'grrr'],
17
+ ['woof', 'smack'],
18
+ [1],
19
+ [1, 2],
20
+ [1, 2, 3],
21
+ ]
22
+ },
23
+ { title: 'Maifee2', content: [[1], [1, 2]] },
24
+ { title: 'Maifee3', content: [['meaw', "meaw"], ["woof", 'woof']] }
25
+ ];
26
+ exports.sampleData = sampleData;
package/lib/util.d.ts ADDED
@@ -0,0 +1,7 @@
1
+ import { IRows } from ".";
2
+ declare const indexToVbIndex: (index: number) => number;
3
+ declare const indexToVbRelationIndex: (index: number) => number;
4
+ declare const indexToRowIndex: (index: number) => string;
5
+ declare const rowColumnToVbPosition: (row: number, col: number) => string;
6
+ declare const calculateExtant: (rows: IRows[]) => string;
7
+ export { indexToVbIndex, indexToVbRelationIndex, indexToRowIndex, rowColumnToVbPosition, calculateExtant };
package/lib/util.js ADDED
@@ -0,0 +1,22 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.calculateExtant = exports.rowColumnToVbPosition = exports.indexToRowIndex = exports.indexToVbRelationIndex = exports.indexToVbIndex = void 0;
4
+ const indexToVbIndex = (index) => index + 1;
5
+ exports.indexToVbIndex = indexToVbIndex;
6
+ const indexToVbRelationIndex = (index) => indexToVbIndex(index) + 2;
7
+ exports.indexToVbRelationIndex = indexToVbRelationIndex;
8
+ const indexToRowIndex = (index) => {
9
+ const base = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".split("");
10
+ let result = "";
11
+ do {
12
+ const remainder = index % 26;
13
+ result = base[(remainder || 26) - 1] + result;
14
+ index = Math.floor(index / 26);
15
+ } while (index > 0);
16
+ return result;
17
+ };
18
+ exports.indexToRowIndex = indexToRowIndex;
19
+ const rowColumnToVbPosition = (row, col) => indexToRowIndex(indexToVbIndex(row)) + indexToVbIndex(col);
20
+ exports.rowColumnToVbPosition = rowColumnToVbPosition;
21
+ const calculateExtant = (rows) => rowColumnToVbPosition(Math.max(...rows.map(row => row.cells.length)) - 1, rows.length - 1);
22
+ exports.calculateExtant = calculateExtant;
@@ -0,0 +1,3 @@
1
+ import { IWorkbook } from "../..";
2
+ declare const generateWorkBookXmlRels: (workbook: IWorkbook) => string;
3
+ export { generateWorkBookXmlRels };
@@ -0,0 +1,14 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.generateWorkBookXmlRels = void 0;
4
+ const util_1 = require("../../util");
5
+ const generateWorkBookXmlRels = (workbook) => `<?xml version="1.0" encoding="UTF-8"?>
6
+ <Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">
7
+ <Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles" Target="styles.xml"/>
8
+ <Relationship Id="rId2" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/sharedStrings" Target="sharedStrings.xml"/>
9
+ ${workbook.sheets.map((_, index) => `
10
+ <Relationship Id="rId${(0, util_1.indexToVbRelationIndex)(index)}" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet" Target="worksheets/sheet${(0, util_1.indexToVbIndex)(index)}.xml"/>
11
+ `).join('\n')}
12
+ </Relationships>
13
+ `;
14
+ exports.generateWorkBookXmlRels = generateWorkBookXmlRels;
@@ -0,0 +1,3 @@
1
+ import { IWorkbook } from "..";
2
+ declare const generateSharedStrings: (workbook: IWorkbook) => string;
3
+ export { generateSharedStrings };
@@ -0,0 +1,13 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.generateSharedStrings = void 0;
4
+ const generateSharedStrings = (workbook) => {
5
+ const uniqueCount = workbook.strings.reduce((acc, cur) => acc.includes(cur) ? acc : [...acc, cur], []).length;
6
+ return `<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
7
+ <sst xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" count="${workbook.strings.length}" uniqueCount="${uniqueCount}">
8
+ ${workbook.strings.map(string => `
9
+ <si><t>${string}</t></si>
10
+ `).join('\n')}
11
+ </sst>`;
12
+ };
13
+ exports.generateSharedStrings = generateSharedStrings;
@@ -0,0 +1,2 @@
1
+ declare const generateStyleXml: () => string;
2
+ export { generateStyleXml };
@@ -1,5 +1,7 @@
1
- const generateStyleXml = () =>
2
- `<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.generateStyleXml = void 0;
4
+ const generateStyleXml = () => `<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
3
5
  <styleSheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="x14ac x16r2 xr" xmlns:x14ac="http://schemas.microsoft.com/office/spreadsheetml/2009/9/ac" xmlns:x16r2="http://schemas.microsoft.com/office/spreadsheetml/2015/02/main" xmlns:xr="http://schemas.microsoft.com/office/spreadsheetml/2014/revision">
4
6
  <fonts count="1" x14ac:knownFonts="1">
5
7
  <font>
@@ -48,5 +50,4 @@ const generateStyleXml = () =>
48
50
  </extLst>
49
51
  </styleSheet>
50
52
  `;
51
-
52
- export {generateStyleXml}
53
+ exports.generateStyleXml = generateStyleXml;
@@ -0,0 +1,2 @@
1
+ declare const generateTheme1: () => string;
2
+ export { generateTheme1 };
@@ -1,5 +1,7 @@
1
- const generateTheme1 = () =>
2
- `<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.generateTheme1 = void 0;
4
+ const generateTheme1 = () => `<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
3
5
  <theme xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main" name="Office Theme">
4
6
  <themeElements>
5
7
  <clrScheme name="Office">
@@ -293,5 +295,4 @@ const generateTheme1 = () =>
293
295
  </extLst>
294
296
  </theme>
295
297
  `;
296
-
297
- export {generateTheme1}
298
+ exports.generateTheme1 = generateTheme1;
@@ -0,0 +1,3 @@
1
+ import { IWorkbook } from "..";
2
+ declare const generateWorkBookXml: (workbook: IWorkbook) => string;
3
+ export { generateWorkBookXml };
@@ -1,20 +1,26 @@
1
- const generateWorkBookXml = () =>
2
- `<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.generateWorkBookXml = void 0;
4
+ const util_1 = require("../util");
5
+ const generateWorkBookXml = (workbook) => `<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
3
6
  <workbook xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="x15 xr xr6 xr10 xr2" xmlns:x15="http://schemas.microsoft.com/office/spreadsheetml/2010/11/main" xmlns:xr="http://schemas.microsoft.com/office/spreadsheetml/2014/revision" xmlns:xr6="http://schemas.microsoft.com/office/spreadsheetml/2016/revision6" xmlns:xr10="http://schemas.microsoft.com/office/spreadsheetml/2016/revision10" xmlns:xr2="http://schemas.microsoft.com/office/spreadsheetml/2015/revision2">
4
7
  <fileVersion appName="xl" lastEdited="7" lowestEdited="7" rupBuild="25427" />
8
+ <workbookProtection/>
9
+ <sheets>
10
+ ${workbook.sheets.map((sheet, index) => `
11
+ <sheet name="${sheet.title}" sheetId="${(0, util_1.indexToVbIndex)(index)}" r:id="rId${(0, util_1.indexToVbRelationIndex)(index)}"/>
12
+ `).join('\n')}
13
+ </sheets>
5
14
  <workbookPr defaultThemeVersion="166925" />
6
15
  <AlternateContent xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006">
7
16
  <Choice Requires="x15">
8
- <absPath url="D:\\projects\ o-spreadsheet\\files\\" xmlns:x15ac="http://schemas.microsoft.com/office/spreadsheetml/2010/11/ac" />
17
+ <absPath url="C:\\projects\\to-spreadsheet\\files\\" xmlns:x15ac="http://schemas.microsoft.com/office/spreadsheetml/2010/11/ac" />
9
18
  </Choice>
10
19
  </AlternateContent>
11
20
  <revisionPtr revIDLastSave="0" documentId="13_ncr:1_{0266953E-4E47-4F87-8C93-A72373D927CC}" xr6:coauthVersionLast="47" xr6:coauthVersionMax="47" xr10:uidLastSave="{00000000-0000-0000-0000-000000000000}" />
12
21
  <bookViews>
13
22
  <workbookView xWindow="825" yWindow="-120" windowWidth="28095" windowHeight="16440" xr2:uid="{BABDBE04-4BCD-4C42-B58D-30BEF5B5054D}" />
14
23
  </bookViews>
15
- <sheets>
16
- <sheet name="Sheet1" sheetId="1" r:id="rId1" />
17
- </sheets>
18
24
  <calcPr calcId="181029" />
19
25
  <extLst>
20
26
  <ext uri="{140A7094-0E35-4892-8432-C4D2E57EDEB5}" xmlns:x15="http://schemas.microsoft.com/office/spreadsheetml/2010/11/main">
@@ -30,6 +36,5 @@ const generateWorkBookXml = () =>
30
36
  </ext>
31
37
  </extLst>
32
38
  </workbook>
33
- `
34
-
35
- export {generateWorkBookXml}
39
+ `;
40
+ exports.generateWorkBookXml = generateWorkBookXml;
@@ -0,0 +1,3 @@
1
+ import { ISheet } from "../..";
2
+ declare const generateSheetXml: (sheet: ISheet) => string;
3
+ export { generateSheetXml };
@@ -0,0 +1,34 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.generateSheetXml = void 0;
4
+ const util_1 = require("../../util");
5
+ const generateSheetXml = (sheet) => {
6
+ return `<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
7
+ <worksheet
8
+ xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main"
9
+ xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships">
10
+ <sheetPr filterMode="false">
11
+ <pageSetUpPr fitToPage="false"/>
12
+ </sheetPr>
13
+ <dimension ref="A1:${(0, util_1.calculateExtant)(sheet.rows)}"/>
14
+ <sheetViews>
15
+ <sheetView tabSelected="1" zoomScale="79" zoomScaleNormal="79" workbookViewId="0"/>
16
+ </sheetViews>
17
+ <sheetFormatPr defaultRowHeight="12.8"></sheetFormatPr>
18
+ <cols>
19
+ <col max="1025" min="1" style="0" width="11.52"/>
20
+ </cols>
21
+ <sheetData>
22
+ ${(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>
26
+ `).join('\n')}
27
+ </row>
28
+ `).join('\n')}
29
+ </sheetData>
30
+ <pageMargins left="0.7" right="0.7" top="0.75" bottom="0.75" header="0.3" footer="0.3"/>
31
+ </worksheet>
32
+ `;
33
+ };
34
+ exports.generateSheetXml = generateSheetXml;
package/package.json CHANGED
@@ -1,9 +1,11 @@
1
1
  {
2
2
  "name": "to-spreadsheet",
3
- "version": "0.0.3",
4
- "description": "npm to package to create spreadsheet in node environment",
3
+ "version": "1.0.0",
4
+ "description": "npm package to create spreadsheet in node environment",
5
+ "main": "src/index.ts",
5
6
  "scripts": {
6
- "build": "npx tsc",
7
+ "prepare": "npm run build",
8
+ "build": "tsc",
7
9
  "test:compile": "npx ts-node src/index.ts"
8
10
  },
9
11
  "repository": {
@@ -20,6 +22,7 @@
20
22
  "archiver": "^5.3.1",
21
23
  "date-fns": "^2.29.3"
22
24
  },
25
+ "files": ["lib"],
23
26
  "devDependencies": {
24
27
  "@types/node": "^17.0.35",
25
28
  "ts-node": "^10.8.0",
package/src/index.ts CHANGED
@@ -1,44 +1,52 @@
1
- import { generateExcel } from "./generate-excel";
2
-
3
- let data = [
4
- [
5
- { readOnly: true, value: "" },
6
- { value: "A", readOnly: true },
7
- { value: "B", readOnly: true },
8
- { value: "C", readOnly: true },
9
- { value: "D", readOnly: true },
10
- { value: "D", readOnly: true },
11
- ],
12
- [
13
- { readOnly: true, value: 1 },
14
- { value: 1 },
15
- { value: 3 },
16
- { value: 3 },
17
- { value: 3 },
18
- ],
19
- [
20
- { readOnly: true, value: 2 },
21
- { value: 2 },
22
- { value: 4 },
23
- { value: 4 },
24
- { value: 4 },
25
- ],
26
- [
27
- { readOnly: true, value: 3 },
28
- { value: 1 },
29
- { value: 3 },
30
- { value: 3 },
31
- { value: 3 },
32
- ],
33
- [
34
- { readOnly: true, value: 4 },
35
- { value: 2 },
36
- { value: 4 },
37
- { value: 4 },
38
- { value: 4 },
39
- ],
40
- ];
41
-
42
- generateExcel(data);
43
-
44
- export { generateExcel };
1
+ import { generateExcel, generateExcelWorkbook } from "./generate-excel";
2
+
3
+ enum ICellType {
4
+ string = "s",
5
+ number = "n"
6
+ }
7
+
8
+ interface ICell {
9
+ type: ICellType;
10
+ value: any;
11
+ }
12
+
13
+ interface IRows {
14
+ cells: ICell[];
15
+ }
16
+
17
+ interface ISheet {
18
+ title: string;
19
+ rows: IRows[];
20
+ }
21
+
22
+ interface IWorkbook {
23
+ filename: string;
24
+ sheets: ISheet[];
25
+ strings: string[];
26
+ }
27
+
28
+ interface IPage {
29
+ title: string
30
+ content: (string | number)[][]
31
+ }
32
+
33
+ export { ICell, ISheet, IWorkbook, IRows, ICellType, IPage }
34
+
35
+
36
+ const sampleData = [
37
+ {
38
+ title: 'Maifee1', content: [
39
+ ['meaw', 'grrr'],
40
+ ['woof', 'smack'],
41
+ [1],
42
+ [1, 2],
43
+ [1, 2, 3],
44
+ ]
45
+ },
46
+ { title: 'Maifee2', content: [[1], [1, 2]] },
47
+ { title: 'Maifee3', content: [['meaw', "meaw"], ["woof", 'woof']] }
48
+ ]
49
+
50
+ // generateExcel(sampleData);
51
+
52
+ export { generateExcel, generateExcelWorkbook, sampleData };
@@ -1,29 +0,0 @@
1
- name: Publish to NPM
2
-
3
- on:
4
- release:
5
- types: [published]
6
-
7
- jobs:
8
- build:
9
- runs-on: ubuntu-latest
10
-
11
- strategy:
12
- matrix:
13
- node-version: [16.x]
14
- registry-url: ['https://registry.npmjs.org']
15
-
16
- steps:
17
- - uses: actions/checkout@v2
18
- - uses: actions/setup-node@v2
19
- with:
20
- node-version: ${{ matrix.node-version }}
21
- registry-url: ${{ matrix.registry-url }}
22
- - name: 'Installing deps'
23
- run: npm i
24
- - name: 'Build'
25
- run: npm run build
26
- - name: 'Publish'
27
- run: npm publish
28
- env:
29
- NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
package/DEVELOPMENT.md DELETED
@@ -1,24 +0,0 @@
1
- # Hi
2
- I appreciate, that you want to help us grow. I think we can help you get started.
3
-
4
- # No TS?
5
- If you are only familiar with JS, I, Maifee Ul Asad, personally will help you learn TS. TS is awesome. We don't have to maintain another repo for typing or that `.d.ts` file separately.
6
- We are trying to make the quality of the code higher.
7
-
8
- # Files
9
- ## Current organization
10
- There is a few simple set of rules:
11
- - If the file name is `index.ts` or `generate*ts`, maybe it contains some function that will be exported. And is responsible for generating at least more than one part of the spreadsheet.
12
- - Else, it is already assigned to generate a specific part(to be a more specific file) of that spreadsheet.
13
- ## Why we did do it?
14
- So how/why these parts were defined? We simply took a simple excel(.xlsx) file and extracted it. And kept that file structure. Read more here: https://github.com/maifeeulasad/to-spreadsheet/discussions/1
15
- ## How are we going to maintain it?
16
- -
17
-
18
- # Why it's a long way to go?
19
- - Let's take a look at something, which is working (almost): https://github.com/maifeeulasad/to-spreadsheet/blob/7295c884dfbbc20ac9ec0c456a14535adb63928c/src/xl/worksheets/sheet1.xml.ts#L37-L50. Now this works, this generates sheet seamlessly. But the issue is it doesn't support any style, color, alignment, etc. There are many arguments for that, and we have to implement those.
20
- - The other thing, they are completely missing. Say we have made, this line static: https://github.com/maifeeulasad/to-spreadsheet/blob/7295c884dfbbc20ac9ec0c456a14535adb63928c/src/generate-excel.ts#L21. But there can and will be more than one sheet, so we have to take care of these too.
21
-
22
-
23
- **We are way too noob, in this, but we can achieve something great for sure.**
24
- Thanks.
@@ -1,15 +0,0 @@
1
- const generateContentTypesXml = () =>
2
- `<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
3
- <Types xmlns="http://schemas.openxmlformats.org/package/2006/content-types">
4
- <Default Extension="rels" ContentType="application/vnd.openxmlformats-package.relationships+xml" />
5
- <Default Extension="xml" ContentType="application/xml" />
6
- <Override PartName="/xl/workbook.xml" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml" />
7
- <Override PartName="/xl/worksheets/sheet1.xml" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet+xml" />
8
- <Override PartName="/xl/theme/theme1.xml" ContentType="application/vnd.openxmlformats-officedocument.theme+xml" />
9
- <Override PartName="/xl/styles.xml" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.styles+xml" />
10
- <Override PartName="/docProps/core.xml" ContentType="application/vnd.openxmlformats-package.core-properties+xml" />
11
- <Override PartName="/docProps/app.xml" ContentType="application/vnd.openxmlformats-officedocument.extended-properties+xml" />
12
- </Types>
13
- `;
14
-
15
- export {generateContentTypesXml}
@@ -1,51 +0,0 @@
1
- const generateAppXml = () =>
2
- `<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
3
- <Properties xmlns="http://schemas.openxmlformats.org/officeDocument/2006/extended-properties" xmlns:vt="http://schemas.openxmlformats.org/officeDocument/2006/docPropsVTypes">
4
- <Application>
5
- Microsoft Excel
6
- </Application>
7
- <DocSecurity>
8
- 0
9
- </DocSecurity>
10
- <ScaleCrop>
11
- false
12
- </ScaleCrop>
13
- <HeadingPairs>
14
- <vector size="2" baseType="variant">
15
- <variant>
16
- <lpstr>
17
- Worksheets
18
- </lpstr>
19
- </variant>
20
- <variant>
21
- <i4>
22
- 1
23
- </i4>
24
- </variant>
25
- </vector>
26
- </HeadingPairs>
27
- <TitlesOfParts>
28
- <vector size="1" baseType="lpstr">
29
- <lpstr>
30
- Sheet1
31
- </lpstr>
32
- </vector>
33
- </TitlesOfParts>
34
- <Company>
35
- </Company>
36
- <LinksUpToDate>
37
- false
38
- </LinksUpToDate>
39
- <SharedDoc>
40
- false
41
- </SharedDoc>
42
- <HyperlinksChanged>
43
- false
44
- </HyperlinksChanged>
45
- <AppVersion>
46
- 16.0300
47
- </AppVersion>
48
- </Properties>
49
- `;
50
-
51
- export {generateAppXml}
@@ -1,24 +0,0 @@
1
- import { format } from 'date-fns'
2
-
3
- const generateCoreXml = (username: string = "to-spreadsheet") => {
4
- const date = format(Date.now(), 'yyyy-MM-dd') + 'T' + format(Date.now(), 'HH:mm:ss') + 'Z'
5
- return `<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
6
- <coreProperties xmlns:cp="http://schemas.openxmlformats.org/package/2006/metadata/core-properties" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:dcterms="http://purl.org/dc/terms/" xmlns:dcmitype="http://purl.org/dc/dcmitype/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
7
- <creator>
8
- ${username}
9
- </creator>
10
- <lastModifiedBy>
11
- ${username}
12
- </lastModifiedBy>
13
- <created xsi:type="dcterms:W3CDTF">
14
- ${date}
15
- </created>
16
- <modified xsi:type="dcterms:W3CDTF">
17
- ${date}
18
- </modified>
19
- </coreProperties>
20
- `;
21
- }
22
-
23
-
24
- export {generateCoreXml}
@@ -1,66 +0,0 @@
1
- import {generateRels} from "./_rels/.rels";
2
- import {generateAppXml} from "./docProps/app.xml";
3
- import { generateCoreXml} from "./docProps/core.xml";
4
- import {generateContentTypesXml} from "./content-types.xml";
5
- import {generateWorkBookXmlRels} from "./xl/_rels/workbook.xml.rels";
6
- import {generateTheme1} from "./xl/theme/theme1.xml";
7
- import {generateSheet1Xml, ICellEntry} from "./xl/worksheets/sheet1.xml";
8
- import {generateStyleXml} from "./xl/styles.xml";
9
- import {generateWorkBookXml} from "./xl/workbook.xml";
10
-
11
- const fs = require('fs');
12
- const archiver = require('archiver');
13
-
14
- const generateTree = (data:ICellEntry[][]) => { return{
15
- "[Content_Types].xml": generateContentTypesXml(),
16
- "_rels/.rels" : generateRels(),
17
- "docProps/app.xml": generateAppXml() ,
18
- "docProps/core.xml": generateCoreXml() ,
19
- "xl/_rels/workbook.xml.rels": generateWorkBookXmlRels(),
20
- "xl/theme/theme1.xml": generateTheme1() ,
21
- "xl/worksheets/sheet1.xml": generateSheet1Xml(data) ,
22
- "xl/styles.xml": generateStyleXml(),
23
- "xl/workbook.xml": generateWorkBookXml(),
24
- }
25
- }
26
-
27
-
28
- const generateExcel = (data:ICellEntry[][]) => {
29
- const output = fs.createWriteStream(__dirname + '/example.xlsx');
30
- const archive = archiver('zip', {
31
- zlib: { level: 9 }
32
- });
33
-
34
- output.on('close', function() {
35
- console.debug(archive.pointer() + ' total bytes');
36
- console.debug('archiver has been finalized and the output file descriptor has closed.');
37
- });
38
-
39
- output.on('end', function() {
40
- console.debug('Data has been drained');
41
- });
42
-
43
- archive.on('warning', function(err:any) {
44
- if (err.code === 'ENOENT') {
45
- // log warning
46
- } else {
47
- // throw error
48
- throw err;
49
- }
50
- });
51
-
52
- archive.on('error', function(err:any) {
53
- throw err;
54
- });
55
-
56
- archive.pipe(output);
57
-
58
-
59
- Object.entries(generateTree(data)).map((value)=>{
60
- archive.append(value[1], { name: value[0] });
61
- })
62
-
63
- archive.finalize();
64
- }
65
-
66
- export {generateExcel}
@@ -1,10 +0,0 @@
1
- const generateWorkBookXmlRels = () =>
2
- `<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
3
- <Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">
4
- <Relationship Id="rId3" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles" Target="styles.xml" />
5
- <Relationship Id="rId2" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/theme" Target="theme/theme1.xml" />
6
- <Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet" Target="worksheets/sheet1.xml" />
7
- </Relationships>
8
- `;
9
-
10
- export {generateWorkBookXmlRels}
@@ -1,54 +0,0 @@
1
- interface ICellEntry {
2
- readonly?: boolean;
3
- value: number | string;
4
- }
5
-
6
- // 1->a, 2->b, 26->z, 27->aa
7
- const index = (value: number) => {
8
- const base = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".split("");
9
- value++;
10
- let result = "";
11
- do {
12
- const remainder = value % 26;
13
- result = base[(remainder || 26) - 1] + result;
14
- value = Math.floor(value / 26);
15
- } while (value > 0);
16
- return result;
17
- };
18
-
19
- const row = (row: number, cellValues: ICellEntry[]) =>
20
- `<row r="${row}" spans="1:${cellValues.length}">${cellValues
21
- .map((item: ICellEntry, i: number) => cell(index(i) + row, item))
22
- .join("")}</row>\n`;
23
-
24
- const cell = (row: string, value: ICellEntry) =>
25
- ` <c r="${row}">${cellValue(value)}</c>\n`;
26
-
27
- const cellValue = (value: ICellEntry) => `<v>${value.value}</v>`;
28
-
29
- const generateSheetData = (grid: ICellEntry[][]) =>
30
- `<sheetData>${grid
31
- .map((ro: ICellEntry[], index: number) => row(index, ro))
32
- .join("")}</sheetData>`;
33
-
34
-
35
-
36
-
37
- const generateSheet1Xml = (
38
- grid: ICellEntry[][]
39
- ) => `<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
40
- <worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="x14ac xr xr2 xr3" xmlns:x14ac="http://schemas.microsoft.com/office/spreadsheetml/2009/9/ac" xmlns:xr="http://schemas.microsoft.com/office/spreadsheetml/2014/revision" xmlns:xr2="http://schemas.microsoft.com/office/spreadsheetml/2015/revision2" xmlns:xr3="http://schemas.microsoft.com/office/spreadsheetml/2016/revision3" xr:uid="{6A0CD924-A203-41BB-8331-42E46B7D5F20}">
41
- <dimension ref="B3:I7" />
42
- <sheetViews>
43
- <sheetView tabSelected="1" workbookViewId="0">
44
- <selection activeCell="B3" sqref="B3:D4" />
45
- </sheetView>
46
- </sheetViews>
47
- <sheetFormatPr defaultRowHeight="15" x14ac:dyDescent="0.25" />
48
- ${generateSheetData(grid)}
49
- <pageMargins left="0.7" right="0.7" top="0.75" bottom="0.75" header="0.3" footer="0.3" />
50
- </worksheet>
51
- `;
52
-
53
-
54
- export { ICellEntry, generateSheet1Xml };
package/tsconfig.json DELETED
@@ -1,8 +0,0 @@
1
- {
2
- "extends": "@tsconfig/node16/tsconfig.json",
3
- "compilerOptions": {
4
- "outDir": "dist"
5
- },
6
- "include": ["src"],
7
- "exclude": ["node_modules"]
8
- }