index.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. import { queryRecordSheet } from '@/services/boom';
  2. import LuckySheet from '../Detail/LuckySheet';
  3. import react, { useRef } from 'react';
  4. import LuckyExcel from 'luckyexcel';
  5. import { Button, message } from 'antd';
  6. import { getToken } from '@/utils/utils';
  7. import moment from 'moment';
  8. const TEMPLATE_URL =
  9. 'https://water-service-test.oss-cn-hangzhou.aliyuncs.com/bom/635/%E5%90%88%E5%90%8C%E6%96%87%E4%BB%B6/%E6%8A%95%E6%A0%87%E6%A8%A1%E6%9D%BF.xlsx';
  10. const TEMPLATE_URL2 = 'https://water-service-test.oss-cn-hangzhou.aliyuncs.com/public/bom/psr.xlsx';
  11. function Index(props) {
  12. const { versionId = 2376 } = props;
  13. const sheetRef = useRef();
  14. const luckysheetRef = useRef();
  15. const uploadExcelByUrl = type => {
  16. LuckyExcel.transformExcelToLuckyByUrl(
  17. type == 1 ? TEMPLATE_URL : TEMPLATE_URL2,
  18. '模板.xlsx',
  19. async (exportJson, luckysheetfile) => {
  20. // if (type == 2) initData(exportJson.sheets);
  21. let [record] = await getExcel(versionId);
  22. let len = exportJson.sheets.length;
  23. record.order = len - 1;
  24. record.index = len;
  25. record.status = '0';
  26. const data = [...exportJson.sheets, record];
  27. console.log(data);
  28. luckysheetRef.current.destroy();
  29. luckysheetRef.current.create({
  30. data,
  31. lang: 'zh',
  32. showinfobar: false,
  33. showstatisticBar: false,
  34. hook: {
  35. cellMousedown: (cell, position, sheet) => {
  36. console.log(cell, position, sheet);
  37. },
  38. cellUpdated: () => {
  39. luckysheetRef.current.refreshFormula();
  40. },
  41. },
  42. });
  43. }
  44. );
  45. };
  46. const handleLoad = () => {
  47. let contentWindow = sheetRef.current.contentWindow;
  48. luckysheetRef.current = contentWindow.luckysheet;
  49. };
  50. const initData = sheets => {
  51. let r = 5,
  52. c = 12;
  53. // let dateCell = sheets[3].celldata.find(item => item.r == 1 && item.c == 3);
  54. let timer = sheets[3].celldata.filter(item => item.r == r && item.c >= c);
  55. timer.forEach((item, index) => {
  56. const cell = item.v;
  57. cell.f = `=EDATE(D2,${index})`;
  58. });
  59. console.log(timer);
  60. };
  61. return (
  62. <div>
  63. <Button type="primary" style={{ marginRight: 20 }} onClick={() => uploadExcelByUrl(1)}>
  64. 导入投标投标
  65. </Button>
  66. <Button type="primary" onClick={() => uploadExcelByUrl(2)}>
  67. 导入合同模板
  68. </Button>
  69. <iframe
  70. style={{
  71. width: '100%',
  72. height: '80vh',
  73. }}
  74. ref={sheetRef}
  75. onLoad={handleLoad}
  76. src="/luckysheet.html"
  77. ></iframe>
  78. </div>
  79. );
  80. }
  81. async function getExcel(gridKey) {
  82. var formData = new FormData();
  83. formData.append('gridKey', gridKey);
  84. let res = await fetch(
  85. `/api/v1/purchase/record/sheet?gridKey=${gridKey}&JWT-TOKEN=${getToken()}`,
  86. {
  87. method: 'POST',
  88. body: formData,
  89. }
  90. ).then(response => response.text());
  91. return JSON.parse(JSON.parse(res));
  92. }
  93. export default Index;