luckySheet.vue 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. <template>
  2. <div>
  3. <!-- <input style="font-size: 16px" type="file" @change="uploadExcel" /> -->
  4. <el-row type="flex" class="row-bg" justify="space-between">
  5. <el-col>
  6. <el-button
  7. type="text"
  8. style="font-size: 20px"
  9. icon="el-icon-arrow-left"
  10. @click="() => $router.push('/home/performance/customAnalysis')"
  11. >
  12. 返回
  13. </el-button>
  14. </el-col>
  15. <el-col style="display: flex; justify-content: end">
  16. <el-button type="primary" @click="saveData" size="small">
  17. 保存数据
  18. </el-button>
  19. </el-col>
  20. </el-row>
  21. <div id="luckysheet" style="width: 100%; height: 88vh"></div>
  22. <!-- 显示获取的数据 -->
  23. </div>
  24. </template>
  25. <script>
  26. import luckysheet from "luckysheet";
  27. import {
  28. getDataFromIndexedDB,
  29. storeSetData,
  30. initDatabase,
  31. } from "@/utils/indexedDb";
  32. import { format } from "date-fns";
  33. export default {
  34. name: "LuckySheetDemo",
  35. data() {
  36. return {
  37. sheetData: null, // 用于存储表格数据
  38. sheetName: "",
  39. };
  40. },
  41. mounted() {
  42. this.initSheetData();
  43. },
  44. methods: {
  45. async initSheetData() {
  46. const jsonData = await getDataFromIndexedDB();
  47. console.log(jsonData, "jsonData");
  48. if (jsonData && jsonData.length > 0) {
  49. this.sheetName = [...jsonData].filter(
  50. (item) => item.fileId == this.$route.query.id
  51. )[0]?.fileOldName;
  52. // 1. 动态提取表头
  53. const headers = Object.keys(
  54. [...jsonData].filter((item) => item.fileId == this.$route.query.id)[0]
  55. .fileData[0]
  56. );
  57. // 2. 将 JSON 数据转换为二维数组格式
  58. const formattedData = [...jsonData]
  59. .filter((item) => item.fileId == this.$route.query.id)[0]
  60. .fileData.map((item) => headers.map((header) => item[header]));
  61. // 3. 将表头插入到数据的第一行
  62. formattedData.unshift(headers);
  63. //4. 将 JSON 数据转换为 Luckysheet 格式
  64. this.initLuckySheet(formattedData);
  65. }
  66. },
  67. initLuckySheet(formattedData) {
  68. const options = {
  69. container: "luckysheet",
  70. showReturnIcon: false, // 假设有这个选项
  71. data: [
  72. {
  73. name: this.sheetName, //工作表名称
  74. index: 0, //工作表索引
  75. status: 1, //激活状态
  76. order: 0, //工作表的下标
  77. hide: 0, //是否隐藏
  78. row: 36, //行数
  79. column: 18, //列数
  80. defaultRowHeight: 19, //自定义行高
  81. defaultColWidth: 73, //自定义列宽
  82. celldata: formattedData
  83. .map((row, rowIndex) =>
  84. row.map((cell, colIndex) => ({
  85. r: rowIndex,
  86. c: colIndex,
  87. v: {
  88. m: cell, // 显示的内容
  89. ct: {
  90. fa: "General", // 格式(通用)
  91. t: "g", // 类型(general)
  92. },
  93. v: cell, // 实际值
  94. },
  95. }))
  96. )
  97. .flat(),
  98. //初始化使用的单元格数据
  99. config: {
  100. merge: {}, //合并单元格
  101. rowlen: {}, //表格行高
  102. columnlen: {}, //表格列宽
  103. rowhidden: {}, //隐藏行
  104. colhidden: {}, //隐藏列
  105. borderInfo: {}, //边框
  106. authority: {}, //工作表保护
  107. },
  108. scrollLeft: 0, //左右滚动条位置
  109. scrollTop: 315, //上下滚动条位置
  110. luckysheet_select_save: [], //选中的区域
  111. calcChain: [], //公式链
  112. isPivotTable: false, //是否数据透视表
  113. pivotTable: {}, //数据透视表设置
  114. filter_select: {}, //筛选范围
  115. filter: null, //筛选配置
  116. luckysheet_alternateformat_save: [], //交替颜色
  117. luckysheet_alternateformat_save_modelCustom: [], //自定义交替颜色
  118. luckysheet_conditionformat_save: {}, //条件格式
  119. frozen: {}, //冻结行列配置
  120. // chart: [], //图表配置
  121. zoomRatio: 1, // 缩放比例
  122. image: [], //图片
  123. showGridLines: 1, //是否显示网格线
  124. dataVerification: {}, //数据验证配置
  125. },
  126. ],
  127. title: "LuckySheet示例", // 表格标题
  128. lang: "zh", // 语言设置
  129. showtoolbar: true, // 是否显示工具栏
  130. chart: {
  131. // 配置图表
  132. enable: false, // 启用图表功能
  133. },
  134. showtoolbarConfig: {
  135. chart: false, // 隐藏图表工具按钮
  136. },
  137. };
  138. luckysheet.create(options);
  139. },
  140. async saveData() {
  141. // 获取当前表格的数据
  142. const data = luckysheet.getAllSheets()[0].data; // 目前只需处理第一个工作表的数据
  143. const formattedData = [];
  144. // 获取表头 (假设表头在第一行,行索引为0)
  145. const headers = data[0].map((cell) => (cell && cell.v ? cell.v : "")); // 提取表头内容
  146. // 遍历数据行,从第二行开始 (行索引为1)
  147. for (let i = 1; i < data.length; i++) {
  148. const row = data[i];
  149. const rowData = {};
  150. // 遍历每一列,并根据表头动态生成键值对
  151. for (let j = 0; j < headers.length; j++) {
  152. if (row[j] && (row[j].v || row[j].v == 0)) {
  153. rowData[headers[j]] = row[j].v || 0;
  154. }
  155. }
  156. // 如果行数据不为空,加入到结果数组中
  157. if (Object.keys(rowData).length) {
  158. formattedData.push(rowData);
  159. }
  160. }
  161. this.sheetData = JSON.stringify(formattedData, null, 2);
  162. await initDatabase()
  163. .then((database) => {
  164. // 调用 storeSetData 方法
  165. let fileData = {
  166. filename: format(new Date(), "yyyyMMdd-HH:mm:ss") + this.sheetName,
  167. fileData: JSON.parse(this.sheetData),
  168. fileOldName: this.sheetName,
  169. fileId: new Date().getTime(),
  170. };
  171. storeSetData(database, "files", "fileDataArray", fileData, () => {
  172. this.$router.push("/home/performance/customAnalysis");
  173. });
  174. })
  175. .catch((error) => {
  176. console.error("数据库初始化失败,无法继续存储数据。", error);
  177. });
  178. },
  179. },
  180. };
  181. </script>
  182. <style scoped lang="scss">
  183. /* 可以添加一些自定义样式 */
  184. ::v-deep .luckysheet_info_detail div.luckysheet_info_detail_back {
  185. display: none;
  186. }
  187. </style>