to-spreadsheet 0.1.0 → 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.
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
@@ -1,2 +1,3 @@
1
- declare const generateContentTypesXml: () => string;
1
+ import { IWorkbook } from "./index";
2
+ declare const generateContentTypesXml: (workbook: IWorkbook) => string;
2
3
  export { generateContentTypesXml };
@@ -1,16 +1,20 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.generateContentTypesXml = void 0;
4
- const generateContentTypesXml = () => `<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
4
+ const util_1 = require("./util");
5
+ const generateContentTypesXml = (workbook) => `<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
5
6
  <Types xmlns="http://schemas.openxmlformats.org/package/2006/content-types">
6
- <Default Extension="rels" ContentType="application/vnd.openxmlformats-package.relationships+xml" />
7
- <Default Extension="xml" ContentType="application/xml" />
8
- <Override PartName="/xl/workbook.xml" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml" />
9
- <Override PartName="/xl/worksheets/sheet1.xml" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet+xml" />
10
- <Override PartName="/xl/theme/theme1.xml" ContentType="application/vnd.openxmlformats-officedocument.theme+xml" />
11
- <Override PartName="/xl/styles.xml" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.styles+xml" />
12
- <Override PartName="/docProps/core.xml" ContentType="application/vnd.openxmlformats-package.core-properties+xml" />
13
- <Override PartName="/docProps/app.xml" ContentType="application/vnd.openxmlformats-officedocument.extended-properties+xml" />
14
- </Types>
15
- `;
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>`;
16
20
  exports.generateContentTypesXml = generateContentTypesXml;
@@ -1,2 +1,3 @@
1
- declare const generateAppXml: () => string;
1
+ import { IWorkbook } from '../index';
2
+ declare const generateAppXml: (workbook: IWorkbook) => string;
2
3
  export { generateAppXml };
@@ -1,52 +1,50 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.generateAppXml = void 0;
4
- const generateAppXml = () => `<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
5
- <Properties xmlns="http://schemas.openxmlformats.org/officeDocument/2006/extended-properties" xmlns:vt="http://schemas.openxmlformats.org/officeDocument/2006/docPropsVTypes">
6
- <Application>
7
- Microsoft Excel
8
- </Application>
9
- <DocSecurity>
10
- 0
11
- </DocSecurity>
12
- <ScaleCrop>
13
- false
14
- </ScaleCrop>
15
- <HeadingPairs>
16
- <vector size="2" baseType="variant">
17
- <variant>
18
- <lpstr>
19
- Worksheets
20
- </lpstr>
21
- </variant>
22
- <variant>
23
- <i4>
24
- 1
25
- </i4>
26
- </variant>
27
- </vector>
28
- </HeadingPairs>
29
- <TitlesOfParts>
30
- <vector size="1" baseType="lpstr">
31
- <lpstr>
32
- Sheet1
33
- </lpstr>
34
- </vector>
35
- </TitlesOfParts>
36
- <Company>
37
- </Company>
38
- <LinksUpToDate>
39
- false
40
- </LinksUpToDate>
41
- <SharedDoc>
42
- false
43
- </SharedDoc>
44
- <HyperlinksChanged>
45
- false
46
- </HyperlinksChanged>
47
- <AppVersion>
48
- 16.0300
49
- </AppVersion>
50
- </Properties>
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>
51
49
  `;
52
50
  exports.generateAppXml = generateAppXml;
@@ -1,2 +1,10 @@
1
- declare const generateCoreXml: (username?: string) => string;
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;
2
10
  export { generateCoreXml };
@@ -2,26 +2,31 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.generateCoreXml = void 0;
4
4
  const date_fns_1 = require("date-fns");
5
- const generateCoreXml = (username = "to-spreadsheet") => {
5
+ const generateCoreXml = ({ username = "to-spreadsheet", description = "", language = "en-US", version = "1", subject = "", title = "" }) => {
6
6
  const date = (0, date_fns_1.format)(Date.now(), "yyyy-MM-dd") +
7
7
  "T" +
8
8
  (0, date_fns_1.format)(Date.now(), "HH:mm:ss") +
9
9
  "Z";
10
10
  return `<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
11
- <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">
12
- <creator>
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>
13
19
  ${username}
14
- </creator>
15
- <lastModifiedBy>
16
- ${username}
17
- </lastModifiedBy>
18
- <created xsi:type="dcterms:W3CDTF">
19
- ${date}
20
- </created>
21
- <modified xsi:type="dcterms:W3CDTF">
22
- ${date}
23
- </modified>
24
- </coreProperties>
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>
25
30
  `;
26
31
  };
27
32
  exports.generateCoreXml = generateCoreXml;
@@ -1,3 +1,4 @@
1
- import { ICellEntry } from "./xl/worksheets/sheet1.xml";
2
- declare const generateExcel: (data: ICellEntry[][]) => void;
3
- export { generateExcel };
1
+ import { IPage, IWorkbook } from "./index";
2
+ declare const generateExcel: (dump: IPage[]) => void;
3
+ declare const generateExcelWorkbook: (workbook: IWorkbook) => void;
4
+ export { generateExcel, generateExcelWorkbook };
@@ -1,32 +1,50 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.generateExcel = void 0;
3
+ exports.generateExcelWorkbook = exports.generateExcel = void 0;
4
+ const content_types_xml_1 = require("./content-types.xml");
4
5
  const _rels_1 = require("./_rels/.rels");
5
6
  const app_xml_1 = require("./docProps/app.xml");
6
7
  const core_xml_1 = require("./docProps/core.xml");
7
- const content_types_xml_1 = require("./content-types.xml");
8
8
  const workbook_xml_rels_1 = require("./xl/_rels/workbook.xml.rels");
9
- const theme1_xml_1 = require("./xl/theme/theme1.xml");
10
- const sheet1_xml_1 = require("./xl/worksheets/sheet1.xml");
9
+ const sharedStrings_xml_1 = require("./xl/sharedStrings.xml");
11
10
  const styles_xml_1 = require("./xl/styles.xml");
11
+ const theme1_xml_1 = require("./xl/theme/theme1.xml");
12
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");
13
15
  const fs = require("fs");
14
16
  const archiver = require("archiver");
15
- const generateTree = (data) => {
16
- return {
17
- "[Content_Types].xml": (0, content_types_xml_1.generateContentTypesXml)(),
18
- "_rels/.rels": (0, _rels_1.generateRels)(),
19
- "docProps/app.xml": (0, app_xml_1.generateAppXml)(),
20
- "docProps/core.xml": (0, core_xml_1.generateCoreXml)(),
21
- "xl/_rels/workbook.xml.rels": (0, workbook_xml_rels_1.generateWorkBookXmlRels)(),
22
- "xl/theme/theme1.xml": (0, theme1_xml_1.generateTheme1)(),
23
- "xl/worksheets/sheet1.xml": (0, sheet1_xml_1.generateSheet1Xml)(data),
24
- "xl/styles.xml": (0, styles_xml_1.generateStyleXml)(),
25
- "xl/workbook.xml": (0, workbook_xml_1.generateWorkBookXml)(),
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"
26
42
  };
43
+ generateExcelWorkbook(workbook);
27
44
  };
28
- const generateExcel = (data) => {
29
- const output = fs.createWriteStream(__dirname + "/example.xlsx");
45
+ exports.generateExcel = generateExcel;
46
+ const generateExcelWorkbook = (workbook) => {
47
+ const output = fs.createWriteStream(`${__dirname}/${workbook.filename}.xlsx`);
30
48
  const archive = archiver("zip", {
31
49
  zlib: { level: 9 },
32
50
  });
@@ -48,9 +66,9 @@ const generateExcel = (data) => {
48
66
  throw err;
49
67
  });
50
68
  archive.pipe(output);
51
- Object.entries(generateTree(data)).map(([filename, fileContent]) => {
69
+ Object.entries(generateTree(workbook)).map(([filename, fileContent]) => {
52
70
  archive.append(fileContent, { name: filename });
53
71
  });
54
72
  archive.finalize();
55
73
  };
56
- exports.generateExcel = generateExcel;
74
+ exports.generateExcelWorkbook = generateExcelWorkbook;
package/lib/index.d.ts CHANGED
@@ -1,12 +1,31 @@
1
- import { generateExcel } from "./generate-excel";
2
- declare let sampleData: ({
3
- readOnly: boolean;
4
- value: string;
5
- }[] | ({
6
- readOnly: boolean;
7
- value: number;
8
- } | {
9
- value: number;
10
- readOnly?: undefined;
11
- })[])[];
12
- export { generateExcel, sampleData };
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 CHANGED
@@ -1,44 +1,26 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.sampleData = exports.generateExcel = void 0;
3
+ exports.sampleData = exports.generateExcelWorkbook = 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
- let sampleData = [
7
- [
8
- { readOnly: true, value: "" },
9
- { value: "A", readOnly: true },
10
- { value: "B", readOnly: true },
11
- { value: "C", readOnly: true },
12
- { value: "D", readOnly: true },
13
- { value: "D", readOnly: true },
14
- ],
15
- [
16
- { readOnly: true, value: 1 },
17
- { value: 1 },
18
- { value: 3 },
19
- { value: 3 },
20
- { value: 3 },
21
- ],
22
- [
23
- { readOnly: true, value: 2 },
24
- { value: 2 },
25
- { value: 4 },
26
- { value: 4 },
27
- { value: 4 },
28
- ],
29
- [
30
- { readOnly: true, value: 3 },
31
- { value: 1 },
32
- { value: 3 },
33
- { value: 3 },
34
- { value: 3 },
35
- ],
36
- [
37
- { readOnly: true, value: 4 },
38
- { value: 2 },
39
- { value: 4 },
40
- { value: 4 },
41
- { value: 4 },
42
- ],
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']] }
43
25
  ];
44
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;
@@ -1,2 +1,3 @@
1
- declare const generateWorkBookXmlRels: () => string;
1
+ import { IWorkbook } from "../..";
2
+ declare const generateWorkBookXmlRels: (workbook: IWorkbook) => string;
2
3
  export { generateWorkBookXmlRels };
@@ -1,11 +1,14 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.generateWorkBookXmlRels = void 0;
4
- const generateWorkBookXmlRels = () => `<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
5
- <Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">
6
- <Relationship Id="rId3" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles" Target="styles.xml" />
7
- <Relationship Id="rId2" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/theme" Target="theme/theme1.xml" />
8
- <Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet" Target="worksheets/sheet1.xml" />
9
- </Relationships>
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>
10
13
  `;
11
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;
@@ -1,2 +1,3 @@
1
- declare const generateWorkBookXml: () => string;
1
+ import { IWorkbook } from "..";
2
+ declare const generateWorkBookXml: (workbook: IWorkbook) => string;
2
3
  export { generateWorkBookXml };
@@ -1,22 +1,26 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.generateWorkBookXml = void 0;
4
- const generateWorkBookXml = () => `<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
4
+ const util_1 = require("../util");
5
+ const generateWorkBookXml = (workbook) => `<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
5
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">
6
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>
7
14
  <workbookPr defaultThemeVersion="166925" />
8
15
  <AlternateContent xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006">
9
16
  <Choice Requires="x15">
10
- <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" />
11
18
  </Choice>
12
19
  </AlternateContent>
13
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}" />
14
21
  <bookViews>
15
22
  <workbookView xWindow="825" yWindow="-120" windowWidth="28095" windowHeight="16440" xr2:uid="{BABDBE04-4BCD-4C42-B58D-30BEF5B5054D}" />
16
23
  </bookViews>
17
- <sheets>
18
- <sheet name="Sheet1" sheetId="1" r:id="rId1" />
19
- </sheets>
20
24
  <calcPr calcId="181029" />
21
25
  <extLst>
22
26
  <ext uri="{140A7094-0E35-4892-8432-C4D2E57EDEB5}" xmlns:x15="http://schemas.microsoft.com/office/spreadsheetml/2010/11/main">
@@ -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,7 +1,7 @@
1
1
  {
2
2
  "name": "to-spreadsheet",
3
- "version": "0.1.0",
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
5
  "main": "src/index.ts",
6
6
  "scripts": {
7
7
  "prepare": "npm run build",
package/src/index.ts CHANGED
@@ -1,44 +1,52 @@
1
- import { generateExcel } from "./generate-excel";
2
-
3
- let sampleData = [
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
- ];
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
+ ]
41
49
 
42
50
  // generateExcel(sampleData);
43
51
 
44
- export { generateExcel, sampleData };
52
+ export { generateExcel, generateExcelWorkbook, sampleData };
@@ -1,6 +0,0 @@
1
- interface ICellEntry {
2
- readonly?: boolean;
3
- value: number | string;
4
- }
5
- declare const generateSheet1Xml: (grid: ICellEntry[][]) => string;
6
- export { ICellEntry, generateSheet1Xml };
@@ -1,36 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.generateSheet1Xml = void 0;
4
- const index = (value) => {
5
- const base = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".split("");
6
- value++;
7
- let result = "";
8
- do {
9
- const remainder = value % 26;
10
- result = base[(remainder || 26) - 1] + result;
11
- value = Math.floor(value / 26);
12
- } while (value > 0);
13
- return result;
14
- };
15
- const row = (row, cellValues) => `<row r="${row}" spans="1:${cellValues.length}">${cellValues
16
- .map((item, i) => cell(index(i) + row, item))
17
- .join("")}</row>\n`;
18
- const cell = (row, value) => ` <c r="${row}">${cellValue(value)}</c>\n`;
19
- const cellValue = (value) => `<v>${value.value}</v>`;
20
- const generateSheetData = (grid) => `<sheetData>${grid
21
- .map((ro, index) => row(index, ro))
22
- .join("")}</sheetData>`;
23
- const generateSheet1Xml = (grid) => `<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
24
- <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}">
25
- <dimension ref="B3:I7" />
26
- <sheetViews>
27
- <sheetView tabSelected="1" workbookViewId="0">
28
- <selection activeCell="B3" sqref="B3:D4" />
29
- </sheetView>
30
- </sheetViews>
31
- <sheetFormatPr defaultRowHeight="15" x14ac:dyDescent="0.25" />
32
- ${generateSheetData(grid)}
33
- <pageMargins left="0.7" right="0.7" top="0.75" bottom="0.75" header="0.3" footer="0.3" />
34
- </worksheet>
35
- `;
36
- exports.generateSheet1Xml = generateSheet1Xml;