Browse Source

EXCEL模块提交

lzj500 1 year ago
parent
commit
8b724be698

+ 22 - 0
excel/.gitignore

@@ -0,0 +1,22 @@
+# Logs
+logs
+*.log
+npm-debug.log*
+yarn-debug.log*
+yarn-error.log*
+pnpm-debug.log*
+lerna-debug.log*
+*.lock
+
+node_modules
+.DS_Store
+dist
+*.local
+
+# Editor directories and files
+.idea
+*.suo
+*.ntvs*
+*.njsproj
+*.sln
+*.sw?

+ 6 - 0
excel/.npmignore

@@ -0,0 +1,6 @@
+node_modules/
+test/
+*.lock
+发布教程.md
+test2.ts
+src/

BIN
excel/assets/2023-09-18-20-47-40-image.png


+ 85 - 0
excel/package-lock.json

@@ -0,0 +1,85 @@
+{
+  "name": "@ycxxkj/uniapp",
+  "version": "1.0.2",
+  "lockfileVersion": 2,
+  "requires": true,
+  "packages": {
+    "": {
+      "name": "@ycxxkj/uniapp",
+      "version": "1.0.2",
+      "license": "ISC",
+      "dependencies": {
+        "await-to-js": "^3.0.0"
+      },
+      "devDependencies": {
+        "@dcloudio/types": "^3.3.3",
+        "typescript": "^5.1.6"
+      }
+    },
+    "C:/Programs/nvm/v16.17.1/node_modules/@ycxxkj/uniapp": {
+      "version": "1.0.0",
+      "extraneous": true,
+      "license": "ISC",
+      "dependencies": {
+        "await-to-js": "^3.0.0"
+      },
+      "devDependencies": {
+        "@dcloudio/types": "^3.3.3",
+        "@types/chai": "^4.3.5",
+        "@types/mocha": "^10.0.1",
+        "chai": "^4.3.7",
+        "mocha": "^10.2.0",
+        "typescript": "^5.1.6"
+      }
+    },
+    "node_modules/@dcloudio/types": {
+      "version": "3.3.3",
+      "resolved": "https://registry.npmjs.org/@dcloudio/types/-/types-3.3.3.tgz",
+      "integrity": "sha512-xfp88QOJ2fgCzv49HhEGrX0L+3xDsCyyvcoApL7z0J1Lr7tqPUkxqAVBe9zBlKsDX/mO9mNj7NzKIisHfp+fNQ==",
+      "dev": true,
+      "license": "Apache-2.0"
+    },
+    "node_modules/await-to-js": {
+      "version": "3.0.0",
+      "resolved": "https://registry.npmjs.org/await-to-js/-/await-to-js-3.0.0.tgz",
+      "integrity": "sha512-zJAaP9zxTcvTHRlejau3ZOY4V7SRpiByf3/dxx2uyKxxor19tpmpV2QRsTKikckwhaPmr2dVpxxMr7jOCYVp5g==",
+      "license": "MIT",
+      "engines": {
+        "node": ">=6.0.0"
+      }
+    },
+    "node_modules/typescript": {
+      "version": "5.1.6",
+      "resolved": "https://registry.npmjs.org/typescript/-/typescript-5.1.6.tgz",
+      "integrity": "sha512-zaWCozRZ6DLEWAWFrVDz1H6FVXzUSfTy5FUMWsQlU8Ym5JP9eO4xkTIROFCQvhQf61z6O/G6ugw3SgAnvvm+HA==",
+      "dev": true,
+      "license": "Apache-2.0",
+      "bin": {
+        "tsc": "bin/tsc",
+        "tsserver": "bin/tsserver"
+      },
+      "engines": {
+        "node": ">=14.17"
+      }
+    }
+  },
+  "dependencies": {
+    "@dcloudio/types": {
+      "version": "3.3.3",
+      "resolved": "https://registry.npmjs.org/@dcloudio/types/-/types-3.3.3.tgz",
+      "integrity": "sha512-xfp88QOJ2fgCzv49HhEGrX0L+3xDsCyyvcoApL7z0J1Lr7tqPUkxqAVBe9zBlKsDX/mO9mNj7NzKIisHfp+fNQ==",
+      "dev": true
+    },
+    "await-to-js": {
+      "version": "3.0.0",
+      "resolved": "https://registry.npmjs.org/await-to-js/-/await-to-js-3.0.0.tgz",
+      "integrity": "sha512-zJAaP9zxTcvTHRlejau3ZOY4V7SRpiByf3/dxx2uyKxxor19tpmpV2QRsTKikckwhaPmr2dVpxxMr7jOCYVp5g=="
+    },
+    "typescript": {
+      "version": "5.1.6",
+      "resolved": "https://registry.npmjs.org/typescript/-/typescript-5.1.6.tgz",
+      "integrity": "sha512-zaWCozRZ6DLEWAWFrVDz1H6FVXzUSfTy5FUMWsQlU8Ym5JP9eO4xkTIROFCQvhQf61z6O/G6ugw3SgAnvvm+HA==",
+      "dev": true
+    }
+  }
+}

+ 25 - 0
excel/package.json

@@ -0,0 +1,25 @@
+{
+  "name": "@ycxxkj/excel",
+  "version": "1.0.2",
+  "description": "ycxxkj excel libs",
+  "main": "./dist/index.js",
+  "types": "./dist/index.d.ts",
+  "scripts": {
+    "prepublish": "npm run build",
+    "test": "mocha --reporter spec",
+    "build": "npm version patch && tsc"
+  },
+  "keywords": [
+    "excel"
+  ],
+  "type": "module",
+  "author": "ycxxkj",
+  "license": "ISC",
+  "devDependencies": {
+    "typescript": "^5.1.6"
+  },
+  "dependencies": {
+    "await-to-js": "^3.0.0",
+    "write-excel-file": "^1.4.27"
+  }
+}

+ 10 - 0
excel/readme.md

@@ -0,0 +1,10 @@
+# excel组件
+
+## 安装
+
+```
+# npm 安装 
+npm i @ycxxkj/excel
+# yarn 安装
+yarn add @ycxxkj/excel
+```

+ 4 - 0
excel/src/index.ts

@@ -0,0 +1,4 @@
+import ExcelWriter from "./libs/ExcelWriter";
+
+import Yc from "./libs/Yc";
+export { ExcelWriter, Yc };

+ 12 - 0
excel/src/libs/ExcelInterface.ts

@@ -0,0 +1,12 @@
+
+/**
+ * 导出的excel类型
+ */
+interface Options {
+    type: typeof String;
+    border: boolean;
+    borderColor: string;
+    fileName: string;
+    columns: Array<any>;   
+}
+export { Options };

+ 236 - 0
excel/src/libs/ExcelWriter.ts

@@ -0,0 +1,236 @@
+
+import writeXlsxFile from "write-excel-file";
+import { Options } from "./ExcelInterface";
+
+const columnNames = getColumnNames();
+
+function getColumnNames() {
+    let wordList = Array.from(new Array(26), (ele, index) => {
+        return String.fromCharCode(65 + index);
+    });
+    let columnNames = [];
+    for (let i = 0; i < 26; i++) {
+        columnNames.push(wordList[i]);
+    }
+    for (let i = 0; i < 26; i++) {
+        for (let j = 0; j < 26; j++) {
+            columnNames.push(wordList[i] + wordList[j]);
+        }
+    }
+    return columnNames;
+}
+console.log("#debug#🚀 ~ file: ExcelWriter.js:6 ~ columnNames ~ columnNames:", columnNames)
+function convertType(value:any, type:any) {
+    switch (type) {
+        case String:
+            return String(value);
+        case Number:
+            let output = Number(value);
+            if (isNaN(output)) {
+                output = 0;
+            }
+            return output;
+        case Boolean:
+            return Boolean(value);
+        case Array:
+            return Array.isArray(value) ? value : [value];
+        case Object:
+            return Object(value);
+        case null:
+            return null;
+        case undefined:
+            return undefined;
+        default:
+            return String(value);
+    }
+}
+const ExcelWriter = {
+
+    async outputExcel(header:Array<any>, data:Array<any>, options:Options) {
+        console.log("#debug#🚀 ~ file: ExcelWriter.js:24 ~ outputExcel ~ options:", options)
+        console.log("#debug#🚀 ~ file: ExcelWriter.js:24 ~ outputExcel ~ header:", header)
+        console.log("#debug#🚀 ~ file: ExcelWriter.js:24 ~ outputExcel ~ data:", data)
+        let excelData = [];
+
+        let excelDataHeader = [] as any;
+        header.forEach((headerItem, headerIndex) => {
+            let column = this._dealCellData(headerItem.title, headerItem, options);
+            column.type = String;
+            column.value = headerItem.title;
+            excelDataHeader.push(column);
+        });
+        let excelDataBody = [] as any;
+        data.forEach((row, rowIndex) => {
+            let rowObject = [] as any;
+            header.forEach((headerItem, headerIndex) => {
+                let column = this._dealCellData(row[headerItem.key], headerItem, options);
+
+                rowObject.push(column);
+            })
+            excelDataBody.push(rowObject);
+        });
+        excelData = [excelDataHeader, ...excelDataBody];
+        console.log("#debug#🚀 ~ file: ExcelWriter.js:45 ~ outputExcel ~ excelData:", excelData)
+        console.log("#debug#🚀 ~ file: ExcelWriter.js:55 ~ outputExcel ~ options:", options)
+        await this._output(excelData, options);
+
+    },
+    //==================私有方法============================
+
+    /**
+     * 生成单元格数据
+     * @param {*} columnCellValue 
+     * @param {*} header 
+     * @param {*} options 
+     * @returns 
+     */
+    _dealCellData(columnCellValue:any, header:any, options:Options) {
+        let cell = {
+            value: convertType(columnCellValue, header?.type ? header?.type : String)// columnCellValue.toString().trim()
+        } as any;
+        if (header?.width) {
+            cell.width = header.width;
+        }
+        if (header?.type) {
+            cell.type = header.type;
+        }
+        if (options.border) {
+            //如果有边框
+            cell.borderColor = options?.borderColor ?? "#000000";
+        }
+        return cell;
+    },
+    /**
+     * 导出excel表格
+     * @param {*} data 
+     * @param {*} options 
+     */
+    async _output(data:Array<any>, options:Options) {
+
+        console.log("#debug#🚀 ~ file: ExcelWriter.js:6 ~ outputExcel ~ outputExcel:导出开始");
+        let excelRows = [];
+        data.forEach((row:Array<any>, rowIndex) => {
+            row.forEach((cell, cellIndex) => {
+                let column = {
+                    value: cell
+                };
+                excelRows.push(column);
+            })
+        });
+        let fileName = options?.fileName ? options.fileName : '导出.xlsx';
+        console.log("#debug#🚀 ~ file: ExcelWriter.js:73 ~ _output ~ options:", options)
+        let columns = options?.columns ? options.columns : [];
+        console.log("#debug#🚀 ~ file: ExcelWriter.js:74 ~ _output ~ columns:", columns)
+        let optionsOutput = {
+            columns, // (optional) column widths, etc.
+            fileName,
+        }
+        console.log("#debug#🚀 ~ file: ExcelWriter.js:60 ~ _output ~ data:", data)
+        console.log("#debug#🚀 ~ file: ExcelWriter.js:76 ~ _output ~ optionsOutput:", optionsOutput)
+        await writeXlsxFile(data, optionsOutput)
+
+        console.log("#debug#🚀 ~ file: ExcelWriter.js:6 ~ outputExcel ~ outputExcel:导出结束");
+    },
+    /**
+     * 导出示例
+     */
+    async outputTest() {
+        console.log("#debug#🚀 ~ file: ExcelWriter.js:6 ~ outputTest ~ outputTest:测试导出开始", columnNames);
+
+        const HEADER_ROW = [
+            {
+                value: 'Name',
+                fontWeight: 'bold'
+            },
+            {
+                value: 'Date of Birth',
+                fontWeight: 'bold'
+            },
+            {
+                value: 'Cost',
+                fontWeight: 'bold'
+            },
+            {
+                value: 'Paid',
+                fontWeight: 'bold'
+            }
+        ]
+
+        const DATA_ROW_1 = [
+            [
+                // "Name"
+                {
+                    type: String,
+                    value: 'John Smith',
+                    borderColor: "#000000"
+                },
+
+                // "Date of Birth"
+                {
+                    type: Date,
+                    value: new Date(),
+                    format: 'mm/dd/yyyy'
+                },
+
+                // "Cost"
+                {
+                    type: Number,
+                    value: 1800
+                },
+
+                // "Paid"
+                {
+                    type: Boolean,
+                    value: true
+                }
+            ],
+            [
+                // "Name"
+                {
+                    type: String,
+                    value: '第2行',
+                    borderColor: "#000000"
+                },
+
+                // "Date of Birth"
+                {
+                    type: Date,
+                    value: new Date(),
+                    format: 'yyyy-mm-dd'
+                },
+
+                // "Cost"
+                {
+                    type: Number,
+                    value: 1800,
+                    span: 3,
+                    borderColor: "#ff0000"
+                },
+
+                // "Paid"
+                null,
+                null,
+                {
+                    type: String,
+                    value: '第2行00',
+                    borderColor: "#000000",
+                    fontSize: 18
+                },
+            ]
+        ];
+
+        const data = [
+            HEADER_ROW,
+            ...DATA_ROW_1,
+
+        ] as any;
+        let options = {
+            // columns, // (optional) column widths, etc.
+            fileName: 'file.xlsx'
+        };
+        await writeXlsxFile(data, options);
+
+    }
+}
+
+export default ExcelWriter;

+ 21 - 0
excel/src/libs/Yc.ts

@@ -0,0 +1,21 @@
+
+const Yc = {
+    getVersion() {
+        return "@ycxxkj/uniapp v1.0.7";
+        
+    },
+    /**
+     * X毫秒后执行
+     * @param milliseconds 
+     * @returns 
+     */
+    delay (milliseconds: number): Promise<boolean> {
+		return new Promise((resolve, reject) => {
+			setTimeout(function (e: any) {
+				e = true;
+				resolve(e);
+			}, milliseconds);
+		});
+	},
+};
+export default Yc;

+ 17 - 0
excel/tsconfig.json

@@ -0,0 +1,17 @@
+{
+  "compilerOptions": {
+    /* Visit https://aka.ms/tsconfig to read more about this file */
+    "target": "es2016", /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */
+    /* Modules */
+    "module": "commonjs", /* Specify what module code is generated. */
+    "esModuleInterop": true, /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type compatibility. */
+    // "preserveSymlinks": true,                         /* Disable resolving symlinks to their realpath. This correlates to the same flag in node. */
+    "forceConsistentCasingInFileNames": true, /* Ensure that casing is correct in imports. */
+    /* Type Checking */
+    "strict": true, /* Enable all strict type-checking options. */
+    "declaration": true, // 生成 `.d.ts` 文件
+    "outDir": "./dist", // 编译后生成的文件目录
+    "skipLibCheck": true, /* Skip type checking all .d.ts files. */
+    "types": []
+  }
+}

+ 149 - 0
excel/发布教程.md

@@ -0,0 +1,149 @@
+# 发布教程
+
+Step 1 
+
+```
+npm init  # 或者使用, npm init -y 跳过所有提问
+```
+
+Step 2  根据提示,填写相应信息,得到 package.json文件
+
+```
+# package.json
+
+{
+  "name": "ts-hi",
+  "version": "0.0.1",
+  "description": "create npm package with typescript",
+  "main": "index.js",
+  "scripts": {
+    "test": "echo \"Error: no test specified\" && exit 1"
+  },
+  "repository": {
+    "type": "git",
+    "url": "git+https://github.com/youthcity/ts-hi.git"
+  },
+  "keywords": [
+    "typescript"
+  ],
+  "author": "youthcity",
+  "license": "ISC",
+  "bugs": {
+    "url": "https://github.com/youthcity/ts-hi/issues"
+  },
+  "homepage": "https://github.com/youthcity/ts-hi#readme"
+}
+```
+
+Step 3 安装依赖
+安装 Typescript
+
+```
+# 使用 npm 安装
+npm i typescript -D
+
+# 或使用 yarn 进行安装
+yarn add typescript -D
+```
+
+配置 tsconfig.json 文件
+手动创建配置文件,文件如下
+
+```
+{
+  "compilerOptions": {
+    "target": "es5",
+    "module": "commonjs",
+    "declaration": true,
+    "outDir": "./dist",
+    "strict": true
+  }
+}
+```
+
+使用命令行创建。
+
+```
+# 需要全局安装 typescript包
+yarn add i typescript -D
+npm i typescript -D
+tsc --init 
+
+# 使用当前项目中的 typescript
+./node_modules/.bin/tsc --init
+```
+
+Step 6 编写测试
+1)安装测试框架和断言库
+
+```
+npm i mocha -D
+npm i chai -D
+yarn add  mocha -D
+yarn add chai -D
+```
+
+2)创建测试文件目录和文件
+
+根目录下
+
+```
+mkdir test && touch test/test.js
+test.js
+```
+
+```
+'use strict';
+const expect = require('chai').expect;
+const add = require('../dist/index').add;
+
+describe('ts-hi function test', () => {
+  it('should return 2', () => {
+    const result = add(1, 1);
+    expect(result).to.equal(2);
+  });
+});
+```
+
+Step 7 运行测试
+添加测试脚本
+
+```
+  "scripts": {
+    "build": "tsc",
+    "test": "mocha --reporter spec"
+  },
+```
+
+运行测试脚本
+
+```
+npm run test
+```
+
+# 版本管理
+
+## 版本升级
+
+```
+# From 1.0.0 -> 1.0.1
+npm version patch 
+
+# From 1.0.1 -> 1.1.0
+npm version minor
+
+# From 1.1.0 -> 2.0.0
+npm version major
+```
+
+## 版本发布
+
+```
+npm publish --access public 
+```
+
+## 版本删除
+
+```
+npm unpublish @ycxxkj/uniapp@1.0.0 --force
+```

+ 5 - 0
excel/测试文档.md

@@ -0,0 +1,5 @@
+# 标题1
+
+## 标题2
+
+![](assets/2023-09-18-20-47-40-image.png)

+ 11 - 0
readme.md

@@ -0,0 +1,11 @@
+# 目录
+
+# [excel](./excel/readme.md )
+
+excel组件
+
+# uniapp
+
+uniapp 通用代码
+
+