Skip to content

Commit 32e02ef

Browse files
Linted with ts-standard and updated node version to v16
1 parent 74f1dcd commit 32e02ef

File tree

11 files changed

+30212
-6526
lines changed

11 files changed

+30212
-6526
lines changed

.github/workflows/publish.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ on:
88
jobs:
99
deploy:
1010
runs-on: ubuntu-latest
11-
if: github.repository == 'LuisEnMarroquin/json-as-xlsx'
1211
steps:
1312
- uses: LuisEnMarroquin/[email protected]
1413
with:

CONTRIBUTING.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ Here are the **node** and **npm** versions that we use to develop this project
44

55
```shell
66
$ node --version && npm --version
7-
v14.17.4
8-
6.14.14
7+
v16.13.0
8+
8.1.0
99
```
1010

1111
After cloning the repository, run `npm install` to install the dependencies

README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,23 @@ let settings = {
4848
xlsx(data, settings) // Will download the excel file
4949
```
5050

51+
### TypeScript
52+
53+
```ts
54+
const jsonSheets: IJsonSheet[] = [{
55+
sheet: "Friends",
56+
columns: [
57+
{ label: "Name", value: "name" },
58+
{ label: "Username", value: "username" }
59+
],
60+
content: [
61+
{ name: "Andreas", username: "andr34s" }
62+
]
63+
}]
64+
65+
xlsx(jsonSheets)
66+
```
67+
5168
## Examples
5269

5370
This are my files used for development, remember to change:

index.ts

Lines changed: 20 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,30 @@
1-
import {utils, WorkBook, WorkSheet, write, writeFile} from 'xlsx'
2-
import {IColumn, IContent, IJsonSheet, IJsonSheetRow, ISettings, IWorksheetColumnWidth} from './types'
3-
4-
function getContentProperty(content: IContent, property: string): string | number | boolean | Date | IContent {
5-
6-
function accessContentProperties(content: IContent, properties: string[]): string | number | boolean | Date | IContent {
1+
import { utils, WorkBook, WorkSheet, write, writeFile } from 'xlsx'
2+
import { IColumn, IContent, IJsonSheet, IJsonSheetRow, ISettings, IWorksheetColumnWidth } from './types'
73

4+
function getContentProperty (content: IContent, property: string): string | number | boolean | Date | IContent {
5+
function accessContentProperties (content: IContent, properties: string[]): string | number | boolean | Date | IContent {
86
const value = content[properties[0]]
97

108
if (properties.length === 1) {
11-
return value ?? ""
9+
return value ?? ''
1210
}
1311

1412
if (value === undefined || typeof value === 'string' || typeof value === 'boolean' ||
15-
typeof value === 'number'|| value instanceof Date) {
16-
return ""
13+
typeof value === 'number' || value instanceof Date) {
14+
return ''
1715
}
1816

1917
return accessContentProperties(value, properties.slice(1))
2018
}
2119

22-
const properties = property.split(".")
20+
const properties = property.split('.')
2321
return accessContentProperties(content, properties)
2422
}
2523

26-
function getJsonSheetRow(content: IContent, columns: IColumn[]): IJsonSheetRow {
27-
28-
let jsonSheetRow: IJsonSheetRow = {}
24+
function getJsonSheetRow (content: IContent, columns: IColumn[]): IJsonSheetRow {
25+
const jsonSheetRow: IJsonSheetRow = {}
2926
columns.forEach((column) => {
30-
if (typeof column.value === "function") {
27+
if (typeof column.value === 'function') {
3128
jsonSheetRow[column.label] = column.value(content)
3229
} else {
3330
jsonSheetRow[column.label] = getContentProperty(content, column.value)
@@ -36,21 +33,19 @@ function getJsonSheetRow(content: IContent, columns: IColumn[]): IJsonSheetRow {
3633
return jsonSheetRow
3734
}
3835

39-
function getWorksheetColumnWidths(worksheet: WorkSheet, extraLength: number = 1): IWorksheetColumnWidth[] {
40-
36+
function getWorksheetColumnWidths (worksheet: WorkSheet, extraLength: number = 1): IWorksheetColumnWidth[] {
4137
const columnRange = utils.decode_range(worksheet['!ref'] ?? '')
4238

4339
// Column letters present in the workbook, e.g. A, B, C
44-
let columnLetters: string[] = []
40+
const columnLetters: string[] = []
4541
for (let C = columnRange.s.c; C <= columnRange.e.c; C++) {
4642
const address = utils.encode_col(C)
4743
columnLetters.push(address)
4844
}
4945

5046
return columnLetters.map((column) => {
51-
5247
// Cells that belong to this column
53-
let columnCells: string[] = Object.keys(worksheet).filter((cell) => {
48+
const columnCells: string[] = Object.keys(worksheet).filter((cell) => {
5449
return cell.charAt(0) === column
5550
})
5651

@@ -59,12 +54,11 @@ function getWorksheetColumnWidths(worksheet: WorkSheet, extraLength: number = 1)
5954
? previousCell : currentCell
6055
})
6156

62-
return {width: worksheet[maxWidthCell].v.length + extraLength}
57+
return { width: worksheet[maxWidthCell].v.length + extraLength }
6358
})
6459
}
6560

66-
function getWorksheet(jsonSheet: IJsonSheet, settings: ISettings): WorkSheet {
67-
61+
function getWorksheet (jsonSheet: IJsonSheet, settings: ISettings): WorkSheet {
6862
const jsonSheetRows = jsonSheet.content.map((contentItem) => {
6963
return getJsonSheetRow(contentItem, jsonSheet.columns)
7064
})
@@ -75,18 +69,16 @@ function getWorksheet(jsonSheet: IJsonSheet, settings: ISettings): WorkSheet {
7569
return worksheet
7670
}
7771

78-
function writeWorkbook(workbook: WorkBook, settings: ISettings = {}): Buffer | undefined {
79-
72+
function writeWorkbook (workbook: WorkBook, settings: ISettings = {}): Buffer | undefined {
8073
const filename = `${settings.fileName ?? 'Spreadsheet'}.xlsx`
8174
const writeOptions = settings.writeOptions ?? {}
8275

8376
return writeOptions.type === 'buffer' ? write(workbook, writeOptions)
8477
: writeFile(workbook, filename, writeOptions)
8578
}
8679

87-
function xlsx(jsonSheets: IJsonSheet[], settings: ISettings = {}): Buffer | undefined {
88-
89-
if (!jsonSheets.length) {
80+
function xlsx (jsonSheets: IJsonSheet[], settings: ISettings = {}): Buffer | undefined {
81+
if (jsonSheets.length === 0) {
9082
return
9183
}
9284

@@ -102,7 +94,7 @@ function xlsx(jsonSheets: IJsonSheet[], settings: ISettings = {}): Buffer | unde
10294
}
10395

10496
export default xlsx
105-
export {getContentProperty, getJsonSheetRow, getWorksheetColumnWidths}
97+
export { getContentProperty, getJsonSheetRow, getWorksheetColumnWidths }
10698
module.exports = xlsx
10799
module.exports.getContentProperty = getContentProperty
108100
module.exports.getJsonSheetRow = getJsonSheetRow

jest.config.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/** @type {import('ts-jest/dist/types').InitialOptionsTsJest} */
22
module.exports = {
33
preset: 'ts-jest',
4-
testEnvironment: 'node',
5-
};
4+
testEnvironment: 'node'
5+
}

0 commit comments

Comments
 (0)