|
| 1 | +import Spreadsheet from 'x-data-spreadsheet' |
| 2 | + |
| 3 | +/** |
| 4 | + * |
| 5 | + declare module ExcelRows { |
| 6 | + export interface cell { |
| 7 | + text: string; |
| 8 | + } |
| 9 | + export interface Cells { |
| 10 | + 0: cell; |
| 11 | + 1: cell; |
| 12 | + 2: cell; |
| 13 | + } |
| 14 | + export interface ExcelRows { |
| 15 | + cells: Cells; |
| 16 | + } |
| 17 | +} |
| 18 | + */ |
| 19 | + |
| 20 | +/** |
| 21 | + * |
| 22 | + BinaryMatrix = [ |
| 23 | + [any, any, any, ...], |
| 24 | + [any, any, any, ...], |
| 25 | + [any, any, any, ...], |
| 26 | + ] |
| 27 | +
|
| 28 | + ExcelDataType = [ |
| 29 | + { |
| 30 | + cells: { |
| 31 | + 0: { text: any }, |
| 32 | + 1: { text: any }, |
| 33 | + 2: { text: any } |
| 34 | + } |
| 35 | + }, |
| 36 | + { |
| 37 | + cells: { |
| 38 | + 0: { text: any }, |
| 39 | + 1: { text: any }, |
| 40 | + 2: { text: any } |
| 41 | + } |
| 42 | + }, |
| 43 | + ] |
| 44 | + */ |
| 45 | + |
| 46 | +class Parser { |
| 47 | + /** |
| 48 | + * |
| 49 | + * @param {*} dataset ExcelDataType |
| 50 | + */ |
| 51 | + static dataset2excel (dataset) { |
| 52 | + return dataset.map(item => ({ |
| 53 | + cells: { |
| 54 | + 0: { text: item.x }, |
| 55 | + 1: { text: item.y }, |
| 56 | + 2: { text: item.s } |
| 57 | + } |
| 58 | + })) |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * |
| 63 | + [ |
| 64 | + [1,2,3,4], |
| 65 | + [5,6,7,8], |
| 66 | + [9,10,11,12] |
| 67 | + ] |
| 68 | + * @param {Object} BinaryMatrix |
| 69 | + * @returns {Object} ExcelDataType |
| 70 | + */ |
| 71 | + static binaryMatrix2excel (binaryMatrix) { |
| 72 | + const excelData = binaryMatrix.map((row, rowIndex) => { |
| 73 | + // cells: { |
| 74 | + // 0: { text: item.x }, |
| 75 | + // 1: { text: item.y }, |
| 76 | + // 2: { text: item.s } |
| 77 | + // } |
| 78 | + const cells = {} |
| 79 | + row.forEach((cellValue, cellIndex) => { |
| 80 | + cells[cellIndex] = { text: cellValue } |
| 81 | + }) |
| 82 | + return { cells } |
| 83 | + }) |
| 84 | + return excelData |
| 85 | + } |
| 86 | + |
| 87 | + static excel2chartDataSet (excelData) { |
| 88 | + const rowsArray = Object.values(excelData.rows).filter(item => typeof item === 'object') |
| 89 | + const dataset = rowsArray.map(row => { |
| 90 | + const [x, y, s] = Object.values(row.cells).map(item => item.text) |
| 91 | + return { |
| 92 | + x: x, |
| 93 | + y: y, |
| 94 | + s: s |
| 95 | + } |
| 96 | + }) |
| 97 | + return dataset |
| 98 | + } |
| 99 | + |
| 100 | + static excel2BinaryMatrix (excelData) { |
| 101 | + const rowsArray = Object.values(excelData.rows).filter(item => typeof item === 'object') |
| 102 | + const dataset = rowsArray.map(row => { |
| 103 | + // [1,2,3,4] |
| 104 | + const cells = Object.values(row.cells).map(item => item.text) |
| 105 | + return cells |
| 106 | + }) |
| 107 | + console.log('dataset', dataset) |
| 108 | + return dataset |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +// const getDefaultTableMatrix = () => [ |
| 113 | +// [1, 2, 3, 4], |
| 114 | +// [5, 6, 7, 8], |
| 115 | +// [9, 10, 11, 12] |
| 116 | +// ] |
| 117 | + |
| 118 | +export default { |
| 119 | + name: 'lbs-excel-editor', |
| 120 | + props: { |
| 121 | + value: { |
| 122 | + type: Array, |
| 123 | + // default: () => getDefaultTableMatrix() |
| 124 | + default: () => [] |
| 125 | + }, |
| 126 | + formatter: { |
| 127 | + type: Function, |
| 128 | + default: Parser.excel2BinaryMatrix |
| 129 | + } |
| 130 | + }, |
| 131 | + computed: { |
| 132 | + innerItems: { |
| 133 | + get () { |
| 134 | + return Parser.binaryMatrix2excel(this.value) |
| 135 | + }, |
| 136 | + set (val) { |
| 137 | + this.$emit('input', val) |
| 138 | + } |
| 139 | + } |
| 140 | + }, |
| 141 | + render () { |
| 142 | + return <div id="excel-wrapper" ref="excel" style="margin-right: 12px;width: 100%;overflow: scroll"></div> |
| 143 | + }, |
| 144 | + mounted () { |
| 145 | + const ele = this.$refs.excel |
| 146 | + const options = { |
| 147 | + showToolbar: false, |
| 148 | + showGrid: true, |
| 149 | + showContextmenu: true |
| 150 | + // view: { |
| 151 | + // height: () => 400, |
| 152 | + // width: () => ele.getBoundingClientRect().width |
| 153 | + // } |
| 154 | + } |
| 155 | + new Spreadsheet(ele, options) |
| 156 | + .loadData({ |
| 157 | + rows: this.innerItems |
| 158 | + }) // load data |
| 159 | + .change(excelData => { |
| 160 | + // console.log('----------') |
| 161 | + // console.log(excelData) |
| 162 | + // console.log(this.formatter(excelData)) |
| 163 | + // console.log('----------') |
| 164 | + this.$emit('change', this.formatter(excelData) /** BinaryMatrix */) |
| 165 | + // save data to db |
| 166 | + }) |
| 167 | + } |
| 168 | +} |
0 commit comments