1.0.1 • Published 2 years ago

gh-excel v1.0.1

Weekly downloads
-
License
ISC
Repository
-
Last release
2 years ago

基于 xlsx.js, 目前支持 ES6

开始

npm

$ install gh-excel

interface Sheet2JSONOpts extends DateNFOption {
  /** Output format */
  header?: 'A' | number | string[];

  /** Override worksheet range */
  range?: any;

  /** Include or omit blank lines in the output */
  blankrows?: boolean;

  /** Default value for null/undefined values */
  defval?: any;

  /** if true, return raw data; if false, return formatted text */
  raw?: boolean;

  /** if true, return raw numbers; if false, return formatted numbers */
  rawNumbers?: boolean;
}

demo

import myExcel from 'gh-excel';

// xlsx 配置 Sheet2JSONOpts
const defaultOption = {
  raw: true,
  blankrows: true,
  header: 1,
  defval: ''
};

// 时间转换格式 default 'YYYY-MM-DD'
const dateFormat = 'YYYY-MM-DD';

// 日期字段需要格式化字段key
const dateKeys = [];

// Array 字段重复key 转换 如
//  [{
//    tableName: 'demo',
//    if: 'demo',
//    to: 'demo1'
//  },...]
const repeactChangeKeys = {};

// 根据sheet
// [{
//     key: 'DEMO', //sheetkey
//     name: '测试', //sheet名
//     reg: /测试/, //正则匹配
//     h: 2 // 从 h+1 行读取表格数据
// }]
const ExcelTypeKeys = [];

// 正则匹配 转换成程序需要的key 如下 如果表头字段正则匹配成功 转换为 结果字段替换为 'test'
//{
//    key: 'test',
//    reg: /测试/
//}
const keys = [];

// excel 表格读取模糊匹配存在重复,重写此方法
/**
 * @param obj {msg,data}
 * @param str ['x','x'].join() excel 表第一行数据
 * @returns {key:String,reg:RegRex,h:Number} obj
 */
const checkKey = function(obj, str) {
  return obj;
};
myExcel.init({
  defaultOption,
  dateFormat,
  dateKeys,
  repeactChangeKeys,
  ExcelTypeKeys,
  keys,
  checkKey
})
myExcel.readExcel(File || remote URL ).then(res=>{
  console.log(res)
})
![avatar](./result.png)
1.0.1

2 years ago

1.0.0

2 years ago