handlerInsertData.js 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. import { read as readXlsx, utils as utilsXlsx } from 'xlsx';
  2. import Papa from 'papaparse';
  3. import { parseJSON } from '@/utils/helper';
  4. import getFile, { readFileAsBase64 } from '@/utils/getFile';
  5. import renderString from '../templating/renderString';
  6. async function insertData({ id, data }, { refData }) {
  7. const replacedValueList = {};
  8. for (const item of data.dataList) {
  9. let value = '';
  10. if (item.isFile) {
  11. const replacedPath = await renderString(
  12. item.filePath || '',
  13. refData,
  14. this.engine.isPopup
  15. );
  16. const path = replacedPath.value;
  17. const isExcel = /.xlsx?$/.test(path);
  18. const isJSON = path.endsWith('.json');
  19. const action = item.action || item.csvAction || 'default';
  20. let responseType = 'text';
  21. if (isJSON) responseType = 'json';
  22. else if (action === 'base64' || (isExcel && action !== 'default'))
  23. responseType = 'blob';
  24. let result = await getFile(path, {
  25. responseType,
  26. returnValue: true,
  27. });
  28. const readAsJson = action.includes('json');
  29. if (action === 'base64') {
  30. result = await readFileAsBase64(result);
  31. } else if (result && path.endsWith('.csv') && readAsJson) {
  32. const parsedCSV = Papa.parse(result, {
  33. header: action.includes('header'),
  34. });
  35. result = parsedCSV.data || [];
  36. } else if (isExcel && readAsJson) {
  37. const base64Xls = await readFileAsBase64(result);
  38. const wb = readXlsx(base64Xls.slice(base64Xls.indexOf(',')), {
  39. type: 'base64',
  40. });
  41. const inputtedSheet = (item.xlsSheet || '').trim();
  42. const sheetName = wb.SheetNames.includes(inputtedSheet)
  43. ? inputtedSheet
  44. : wb.SheetNames[0];
  45. const options = {};
  46. if (item.xlsRange) options.range = item.xlsRange;
  47. if (!action.includes('header')) options.header = 1;
  48. const sheetData = utilsXlsx.sheet_to_json(
  49. wb.Sheets[sheetName],
  50. options
  51. );
  52. result = sheetData;
  53. }
  54. value = result;
  55. Object.assign(replacedValueList, replacedPath.list);
  56. } else {
  57. const replacedValue = await renderString(
  58. item.value,
  59. refData,
  60. this.engine.isPopup
  61. );
  62. value = parseJSON(replacedValue.value, replacedValue.value);
  63. Object.assign(replacedValueList, replacedValue.list);
  64. }
  65. if (item.type === 'table') {
  66. const values = typeof value === 'string' ? value.split('||') : [value];
  67. values.forEach((tableValue) => {
  68. this.addDataToColumn(item.name, tableValue);
  69. });
  70. } else {
  71. this.setVariable(item.name, value);
  72. }
  73. }
  74. return {
  75. data: '',
  76. replacedValue: replacedValueList,
  77. nextBlockId: this.getBlockConnections(id),
  78. };
  79. }
  80. export default insertData;