Browse Source

cvstoarrory , 解析cvs方法

wanggaokun 1 year ago
parent
commit
3d4eee5266
2 changed files with 35 additions and 0 deletions
  1. 1 0
      pdaaphm-ui/package.json
  2. 34 0
      pdaaphm-ui/src/utils/cvstoarrory.js

+ 1 - 0
pdaaphm-ui/package.json

@@ -49,6 +49,7 @@
     "js-cookie": "3.0.1",
     "jsencrypt": "3.0.0-rc.1",
     "nprogress": "0.2.0",
+    "papaparse": "^5.4.1",
     "quill": "1.3.7",
     "screenfull": "5.0.2",
     "sortablejs": "1.10.2",

+ 34 - 0
pdaaphm-ui/src/utils/cvstoarrory.js

@@ -0,0 +1,34 @@
+import Papa from 'papaparse'
+export function parseCSV(url) {
+  return new Promise((resolve, reject) => {
+    Papa.parse(url, {
+      download: true,
+      header: true,
+      // dynamicTyping: true,
+      transform: (value, header) => {
+        // 检查是否是科学记数法
+        const scientificNotationRegex =
+          /^[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?$/
+
+        // 如果是科学记数法,将其转换为普通数字格式
+        if (scientificNotationRegex.test(value)) {
+          //  const numberValue = Number(value);
+          //  return isNaN(numberValue) ? value : numberValue;
+          return parseFloat(value)
+        }
+
+        // 如果不是科学记数法,保持原值
+        return value
+      },
+      complete: results => {
+        // 处理完整的解析结果
+        console.log(results.data)
+        resolve(results.data)
+      },
+      error: (error) => {
+        console.error('Error parsing CSV:', error.message);
+        reject(error.message);
+      },
+    })
+  })
+}