Explorar o código

导入导出Excel材料预算价

chenshilong %!s(int64=3) %!d(string=hai) anos
pai
achega
efab0417e9
Modificáronse 1 ficheiros con 82 adicións e 79 borrados
  1. 82 79
      web/building_saas/main/js/views/project_glj_view.js

+ 82 - 79
web/building_saas/main/js/views/project_glj_view.js

@@ -72,6 +72,8 @@ projectGljObject = {
     /*{ID:'MAIN_MATERIAL',text:'主材'},
         {ID:'EQUIPMENT',text:'设备'}*/
   ],
+  importJson: null,
+
   initSpreads: function () {
     if (this.projectGljSpread == null) this.initProjectGljSpread();
     //if(materialAdjustObj.spread == null) materialAdjustObj.initSpread();
@@ -1281,7 +1283,7 @@ projectGljObject = {
             return compilationName === "四川养护(2013)" || compilationName === "部颁2018计价标准";
           },
           callback: function (key, opt) {
-            me.ExportExcelFile();
+            me.exportToExcelFile();
           },
         },
         importPriceFromExcel: {
@@ -1438,9 +1440,10 @@ projectGljObject = {
     }
     return null;
   },
-  ExportExcelFile: function () {
+
+  exportToExcelFile: function () {
     const excelIO = new GC.Spread.Excel.IO();
-    console.log(projectGljObject.projectGljSheetData);
+    console.log(this.projectGljSheetData);
     let workbook = new GC.Spread.Sheets.Workbook(document.getElementById("#project_glj_sheet_Temp"));
     let sheet = workbook.sheets[0];
     workbook.commandManager().execute({ cmd: "renameSheet", sheetName: "Sheet1", name: "材料预算价" });
@@ -1467,14 +1470,17 @@ projectGljObject = {
     sheet.getRange(-1, 5, -1, 1).hAlign(GC.Spread.Sheets.HorizontalAlign.right);
     sheet.getRange(0, -1, 1, -1).hAlign(GC.Spread.Sheets.HorizontalAlign.center); // 标题行居中对齐
 
-    for (let i = 0; i < projectGljObject.projectGljSheetData.length; i++) {
-      const e = projectGljObject.projectGljSheetData[i];
-      sheet.getCell(i + 1, 0).value(e.code);
-      sheet.getCell(i + 1, 1).value(e.name);
-      sheet.getCell(i + 1, 2).value(e.specs);
-      sheet.getCell(i + 1, 3).value(e.unit);
-      sheet.getCell(i + 1, 4).value(e.type);
-      sheet.getCell(i + 1, 5).value(e.unit_price.market_price);
+    let j = 0;
+    for (let i = 0; i < this.projectGljSheetData.length; i++) {
+      const e = this.projectGljSheetData[i];
+      if (!e.code || e.type !== 201) continue; // 过滤非材料、无编号。
+      j += 1;
+      sheet.getCell(j, 0).value(e.code);
+      sheet.getCell(j, 1).value(e.name);
+      sheet.getCell(j, 2).value(e.specs);
+      sheet.getCell(j, 3).value(e.unit);
+      sheet.getCell(j, 4).value(e.type);
+      sheet.getCell(j, 5).value(e.unit_price.market_price);
     }
 
     let json = JSON.stringify(workbook.toJSON());
@@ -1491,6 +1497,69 @@ projectGljObject = {
       }
     );
   },
+  readExcelFileToJson: function (excelFile) {
+    projectGljObject.importJson = null;
+    let excelIo = new GC.Spread.Excel.IO();
+    let sDate = +new Date();
+    excelIo.open(
+      excelFile,
+      function (json) {
+        projectGljObject.importJson = json;
+        console.log(`解析Excel文件时间:${+new Date() - sDate}`);
+        $.bootstrapLoading.end();
+      },
+      function (e) {
+        $.bootstrapLoading.end();
+        alert(e.errorMessage);
+      }
+    );
+  },
+  readJsonSheets: function () {
+    const err = "当前Excel文件格式不正确,请参考导出Excel材料预算价文件!";
+    const sheet = projectGljObject.importJson.sheets["材料预算价"];
+    if (!sheet) throw err;
+    const sheetData = sheet.data.dataTable;
+    if (!sheetData) throw err;
+    const isRightHead =
+      sheetData[0][0].value === "编号" &&
+      sheetData[0][1].value === "名称" &&
+      sheetData[0][2].value === "规格" &&
+      sheetData[0][3].value === "单位" &&
+      sheetData[0][4].value === "类型" &&
+      sheetData[0][5].value === "预算价";
+    if (!isRightHead) throw err;
+
+    let rst = [];
+    for (let row = 1; row < sheet.rows.length - 1; row++) {
+      const rowData = {
+        code: sheetData[row][0] ? sheetData[row][0].value : undefined,
+        name: sheetData[row][1] ? sheetData[row][1].value : undefined,
+        specs: sheetData[row][2] ? sheetData[row][2].value : undefined,
+        unit: sheetData[row][3] ? sheetData[row][3].value : undefined,
+        type: sheetData[row][4] ? sheetData[row][4].value : undefined,
+        unitPrice: sheetData[row][5] ? sheetData[row][5].value : undefined,
+      };
+      rst.push(rowData);
+    }
+    return rst;
+  },
+  importExcelPrice: function () {
+    const priceData = this.readJsonSheets();
+    if (priceData.length === 0) {
+      throw "当前Excel文件没有材料预算价!";
+    }
+
+    const priceCol = this.getColIndex("预算价");
+    const changeInfo = [];
+    for (const pd of priceData) {
+      const idx = gljUtil.getIndex(pd, gljKeyArray);
+      const pGlj = this.getSheetDataRowAndPrice(idx);
+      if (pGlj && pGlj.price !== pd.unitPrice) {
+        changeInfo.push({ row: pGlj.row, col: priceCol, value: pd.unitPrice });
+      }
+    }
+    if (changeInfo.length > 0) this.batchUpdateProjectGLJ(changeInfo, "projectGljSheet");
+  },
 };
 
 function getProjectResizeEles() {
@@ -1834,8 +1903,6 @@ $(function () {
     projectGljObject.showRelatedRationDatas();
   });
 
-  let importJson = null;
-
   function showAlert(msg, isHint) {
     $("#showAlert").removeClass("alert-success");
     if (isHint) $("#showAlert").addClass("alert-info");
@@ -1844,37 +1911,6 @@ $(function () {
     $("#showAlert").show();
   }
 
-  function getExcelData(sheets) {
-    debugger;
-    const err = "当前Excel文件格式不正确,请参考导出Excel材料预算价文件!";
-    const sheet = sheets["材料预算价"];
-    if (!sheet) throw err;
-    const sheetData = sheet.data.dataTable;
-    if (!sheetData) throw err;
-    const isRightHead =
-      sheetData[0][0].value === "编号" &&
-      sheetData[0][1].value === "名称" &&
-      sheetData[0][2].value === "规格" &&
-      sheetData[0][3].value === "单位" &&
-      sheetData[0][4].value === "类型" &&
-      sheetData[0][5].value === "预算价";
-    if (!isRightHead) throw err;
-
-    let rst = [];
-    for (let row = 1; row < sheet.rows.length; row++) {
-      const rowData = {
-        code: sheetData[row][0] ? sheetData[row][0].value : undefined,
-        name: sheetData[row][1] ? sheetData[row][1].value : undefined,
-        specs: sheetData[row][2] ? sheetData[row][2].value : undefined,
-        unit: sheetData[row][3] ? sheetData[row][3].value : undefined,
-        type: sheetData[row][4] ? sheetData[row][4].value : undefined,
-        unitPrice: sheetData[row][5] ? sheetData[row][5].value : undefined,
-      };
-      rst.push(rowData);
-    }
-    return rst;
-  }
-
   //选择导入的excel文件
   $("#customPriceFile").change(function () {
     let file = $(this)[0];
@@ -1891,25 +1927,7 @@ $(function () {
       $("#showAlert").hide();
       $.bootstrapLoading.start();
       $("#loadingPage").css("z-index", "2000");
-      //前端解析excel数据
-      if (importJson) {
-        importJson = null;
-      }
-      let excelIo = new GC.Spread.Excel.IO();
-      let sDate = +new Date();
-      excelIo.open(
-        excelFile,
-        function (json) {
-          importJson = json;
-          console.log(json);
-          console.log(`解析Excel文件时间:${+new Date() - sDate}`);
-          $.bootstrapLoading.end();
-        },
-        function (e) {
-          $.bootstrapLoading.end();
-          alert(e.errorMessage);
-        }
-      );
+      projectGljObject.readExcelFileToJson(excelFile);
     }
   });
 
@@ -1927,23 +1945,8 @@ $(function () {
         throw "项目数据出错";
       }
 
-      const priceData = getExcelData(importJson.sheets);
-      if (priceData.length === 0) {
-        throw "当前Excel文件没有材料预算价!";
-      }
-
       showAlert("正在导入预算价...", true);
-      const priceCol = projectGljObject.getColIndex("预算价");
-      console.log(projectGljObject.projectGljSheetData);
-      const changeInfo = [];
-      for (const pd of priceData) {
-        const idx = gljUtil.getIndex(pd, gljKeyArray);
-        const pGlj = projectGljObject.getSheetDataRowAndPrice(idx);
-        if (pGlj && pGlj.price !== pd.unitPrice) {
-          changeInfo.push({ row: pGlj.row, col: priceCol, value: pd.unitPrice });
-        }
-      }
-      if (changeInfo.length > 0) projectGljObject.batchUpdateProjectGLJ(changeInfo, "projectGljSheet");
+      projectGljObject.importExcelPrice();
       $("#showAlert").hide();
       $(me).removeClass("disabled");
       $("#importPriceForm").modal("hide");