const is_numeric = (value) => { if (typeof(value) === 'object') { return false; } else { return !Number.isNaN(Number(value)) && value.toString().trim() !== ''; } }; function getPasteHint (str, row = '') { let returnObj = str; if (row) { returnObj.msg = '指数清单第' + (row+1) + '行' + str.msg; } return returnObj; } $(document).ready(() => { autoFlashHeight(); const materialExponentSpread = SpreadJsObj.createNewSpread($('#material-exponent-spread')[0]); const materialExponentSpreadSetting = { cols: [ {title: '类型', colSpan: '1', rowSpan: '2', field: 'type', hAlign: 1, width: 60, formatter: '@', readOnly: true,cellType: 'customizeCombo', comboItems: materialType.ex_type, cellTypeKey: 1}, {title: '符号', colSpan: '1', rowSpan: '2', field: 'symbol', hAlign: 1, width: 60, formatter: '@', readOnly: 'readOnly.isEdit'}, {title: '符号说明', colSpan: '1', rowSpan: '2', field: 'symbol_desc', hAlign: 0, width: 180, formatter: '@', readOnly: 'readOnly.isEdit'}, {title: '编号', colSpan: '1', rowSpan: '2', field: 'code', hAlign: 1, width: 60, formatter: '@', readOnly: 'readOnly.isEdit'}, {title: '基本价格指数(F0)', colSpan: '1', rowSpan: '2', field: 'basic_price', hAlign: 2, width: 120, readOnly: 'readOnly.isEdit'}, {title: '基准时间', colSpan: '1', rowSpan: '2', field: 'basic_times', hAlign: 0, width: 80, formatter: '@', readOnly: 'readOnly.isEdit'}, {title: '现行价格指数(Ft)', colSpan: '1', rowSpan: '2', field: 'm_price', hAlign: 2, width: 120, type: 'Number', readOnly: 'readOnly.remark'}, {title: '加权系数(B)', colSpan: '1', rowSpan: '2', field: 'weight_num', hAlign: 2, width: 120, formatter: '@', readOnly: 'readOnly.isConstant'}, {title: '计算值(Ft/F0*B)', colSpan: '1', rowSpan: '2', field: 'calc_num', hAlign: 2, width: 100, formatter: '@', readOnly: true, getValue: 'getValue.calc_num'}, {title: '备注', colSpan: '1', rowSpan: '2', field: 'remark', hAlign: 0, width: 60, formatter: '@', readOnly: 'readOnly.remark'}, {title: '是否汇总', colSpan: '1', rowSpan: '2', field: 'is_summary', hAlign: 1, width: 60, cellType: 'checkbox', readOnly: 'readOnly.isEdit'}, ], emptyRows: 0, headRows: 2, headRowHeight: [25, 25], defaultRowHeight: 21, headerFont: '12px 微软雅黑', font: '12px 微软雅黑', readOnly: readOnly, }; const materialExponentBase = { isUsed: function (data) { if (data.type === 2) { return data.mid === materialID || data.basic_price === null || data.basic_price === 0; } else { return false; } }, isEdit: function (data) { return data.mid === materialID && data.type === 2; }, isConstant: function (data) { return (materialOrder === 1 && data.type === 1) || (data.mid === materialID && data.type === 2); } }; const materialExponentCol = { getValue: { calc_num : function (data) { if (data.type === 1) return null; const calc_num = data.basic_price > 0 ? ZhCalc.mul(data.weight_num, ZhCalc.div(data.m_price, data.basic_price)) : 0; return calc_num > 0 ? ZhCalc.round(calc_num, materialExponentDecimal.qty) : 0; }, }, readOnly: { isEdit: function (data) { return !(!readOnly && materialExponentBase.isEdit(data)); }, isUsed: function (data) { return !(!readOnly && materialExponentBase.isUsed(data)); }, remark: function (data) { return !(!readOnly && data.type === 2); }, isConstant: function (data) { // return !(!readOnly && materialExponentBase.isConstant(data)); return readOnly; } }, }; const stageTjSpread = SpreadJsObj.createNewSpread($('#stage-tj-spread')[0]); const getTipText = function (data) { if (!data || !data.tip) return ''; return data.tip; }; const stageTjSpreadSetting = { cols: [ {title: '计量期数', colSpan: '1', rowSpan: '2', field: 'order', hAlign: 0, width: 250, formatter: '@', cellType: 'delayTip', getTip: getTipText}, {title: '本期调差金额', colSpan: '1', rowSpan: '2', field: 'ex_tp', hAlign: 1, width: 200, type: 'Number'}, {title: '本期调差金额(建筑税)', colSpan: '1', rowSpan: '2', field: 'ex_tax_tp', hAlign: 1, width: 200, type: 'Number'}, ], emptyRows: 0, headRows: 1, headRowHeight: [32], defaultRowHeight: 21, headerFont: '12px 微软雅黑', font: '12px 微软雅黑', readOnly: true, }; SpreadJsObj.initSheet(stageTjSpread.getActiveSheet(), stageTjSpreadSetting); const needUpdateArray = ['weight_num', 'm_price', 'remark']; const materialExponentSpreadObj = { getMaterialExponentData: function (msid = null, mnid = null) { const ms_id = msid || $('#myTab').find('.active').attr('data-msid') || null; const mn_id = mnid || $('#myTab2').find('.active').attr('data-mnid') || null; if (ms_id || mn_id) { const condition = {}; if (ms_id) condition.ms_id = parseInt(ms_id); if (mn_id) condition.mn_id = parseInt(mn_id); const mseList = _.filter(materialExponentShardData, condition); console.log(mseList); for (const me of materialExponentData) { me.ms_id = ms_id ? parseInt(ms_id) : null; me.mn_id = mn_id ? parseInt(mn_id) : null; const mseInfo = _.find(mseList, { me_id: me.me_id ? me.me_id : me.id }); console.log(mseInfo); if (mseInfo) { for (const nu of needUpdateArray) { me[nu] = mseInfo[nu]; } } } } console.log(materialExponentData, materialExponentShardData); return materialExponentData; }, updateMaterialNodeTab: function () { const nodeList = $('#myTab2').find('li'); const nowli = $('#myTab2').find('.active'); const materialExponentNodeSort = exponent_nodes ? ['-1'].concat(exponent_nodes.split(',')) : []; const map = Object.fromEntries(materialExponentNodeSort.map((v, i) => [v, i])); materialExponentNodeData = materialExponentNodeData.sort((a, b) => map[a.node] - map[b.node]); const newNodeList = _.filter(materialExponentNodeData, { ms_id: parseInt($('#myTab').find('.active').attr('data-msid')) }); for (const n of nodeList) { const node = $(n).children('a').attr('data-node'); const nl = _.find(newNodeList, { node }); $(n).children('a').attr('data-mnid', nl.id); } const newMnid = parseInt(nowli.attr('data-mnid')); materialExponentSpreadObj.getMaterialExponentData($('#myTab').find('.active').attr('data-msid'), newMnid); materialExponentSpreadObj.materialSheetReset(true); setExCalc($('#myTab').find('.active').attr('data-msid'), newMnid); }, updateOneMaterialExponent: function (data) { const ms_id = $('#myTab').find('.active').attr('data-msid') || null; const mn_id = $('#myTab2').find('.active').attr('data-mnid') || null; const condition = { me_id: data.id }; if (ms_id) condition.ms_id = parseInt(ms_id); if (mn_id) condition.mn_id = parseInt(mn_id); const mseInfo = _.find(materialExponentShardData, condition); console.log(mseInfo); data.ms_id = ms_id ? parseInt(ms_id) : null; data.mn_id = mn_id ? parseInt(mn_id) : null; for (const nu of needUpdateArray) { data[nu] = mseInfo[nu]; } return data; }, updateMaterialData: function (datas) { if (datas.stageData && datas.stageData.length > 0) { if (datas.stageData.length === materialStageData.length) { // 全体替换 materialStageData = datas.stageData; } else { // 替换对应的stage数据 for (const s of datas.stageData) { const index = _.findIndex(materialStageData, { id: s.id }); materialStageData.splice(index, 1, s); } } } if (datas.nodeData && datas.nodeData.length > 0) { if (datas.nodeData.length === materialExponentNodeData.length) { // 全体替换 materialExponentNodeData = datas.nodeData; } else { // 替换对应的stage数据 for (const s of datas.nodeData) { const index = _.findIndex(materialExponentNodeData, { id: s.id }); materialExponentNodeData.splice(index, 1, s); } } } if (datas.pushExponentShardData && datas.pushExponentShardData.length > 0) { materialExponentShardData = _.concat(materialExponentShardData, datas.pushExponentShardData); } if (datas.removeExponentShardData && datas.removeExponentShardData.length > 0) { materialExponentShardData = _.remove(materialExponentShardData, function (item) { return _.findIndex(datas.removeExponentShardData, { id: item.me_id }) === -1 }) } if (datas.exponentShardData && datas.exponentShardData.length > 0) { if (datas.exponentShardData.length === materialExponentShardData.length) { // 全体替换 materialExponentShardData = datas.exponentShardData; } else { // 替换对应的stage数据 for (const s of datas.exponentShardData) { const index = _.findIndex(materialExponentShardData, { id: s.id }); materialExponentShardData.splice(index, 1, s); } } } if (datas.exponentData && datas.exponentData.length > 0) { if (datas.exponentData.length === materialExponentData.length) { // 全体替换 materialExponentData = datas.exponentData; materialExponentSpreadObj.getMaterialExponentData(); } else { for (const b of datas.exponentData) { const index = _.findIndex(materialExponentData, {id: b.id}); materialExponentData.splice(index, 1, b); materialExponentSpreadObj.updateOneMaterialExponent(materialExponentData[index]); } } } }, materialSheetReset: function (redo = false) { let newMaterialExponentData = _.cloneDeep(materialExponentData); if (redo) { materialExponentSpread.getActiveSheet().reset(); SpreadJsObj.initSpreadSettingEvents(materialExponentSpreadSetting, materialExponentCol); SpreadJsObj.initSheet(materialExponentSpread.getActiveSheet(), materialExponentSpreadSetting); } SpreadJsObj.loadSheetData(materialExponentSpread.getActiveSheet(), SpreadJsObj.DataType.Data, newMaterialExponentData); materialExponentSpreadObj.refreshActn(); }, refreshActn: function (rowCount = 1) { const setObjEnable = function (obj, enable) { if (enable) { obj.removeClass('disabled'); } else { obj.addClass('disabled'); } }; const sheet = materialExponentSpread.getActiveSheet(); const select = SpreadJsObj.getSelectObject(sheet); // 还需判断是否已被调差清单调用 setObjEnable($('#del'), !readOnly && select && materialExponentBase.isUsed(select) && rowCount === 1); }, add: function () { const sheet = materialExponentSpread.getActiveSheet(); postData(window.location.pathname + '/save', {type: 'add'}, function (result) { if (result) { materialExponentData.push(result.info); if (showStageExponent || exponent_nodes) { materialExponentSpreadObj.updateMaterialData(result); materialExponentSpreadObj.getMaterialExponentData(); } SpreadJsObj.loadSheetData(materialExponentSpread.getActiveSheet(), SpreadJsObj.DataType.Data, materialExponentData); sheet.setSelection(materialExponentData.length - 1, 0, 1, 1); materialExponentSpreadObj.refreshActn(); } }); }, del: function () { const sheet = materialExponentSpread.getActiveSheet(); const select = SpreadJsObj.getSelectObject(sheet); const sel = sheet.getSelections(); postData(window.location.pathname + '/save', {type: 'del', id: select.id}, function (result) { ex_tp = result.ex_tp; ex_tax_tp = result.ex_tax_tp; ex_expr = result.ex_expr || null; const index = _.findIndex(materialExponentData, { id: select.id }); materialExponentData.splice(index, 1); if (showStageExponent || exponent_nodes) { result.removeExponentShardData = [{ id: select.id }]; materialExponentSpreadObj.updateMaterialData(result); materialExponentSpreadObj.getMaterialExponentData(); } materialExponentSpreadObj.materialSheetReset(true); resetExTpTable(); sheet.setSelection(index > 0 ? index - 1 : 0, sel.length > 0 ? sel[0].col : 0, 1, 1); materialExponentSpreadObj.refreshActn(); }); }, selectionChanged: function (e, info) { const sel = info.sheet.getSelections()[0]; const col = info.sheet.zh_setting.cols[sel.col]; materialExponentSpreadObj.refreshActn(sel.rowCount); const data = SpreadJsObj.getSelectObject(info.sheet); materialExponentSpreadObj.setReadOnly(true); }, editEnded: function (e, info) { if (info.sheet.zh_setting) { const select = SpreadJsObj.getSelectObject(info.sheet); const col = info.sheet.zh_setting.cols[info.col]; if (col.field === 'is_summary') { return; } // 未改变值则不提交 let validText = is_numeric(info.editingText) ? parseFloat(info.editingText) : (info.editingText ? trimInvalidChar(info.editingText) : null); const orgValue = select[col.field]; if (orgValue == validText || ((!orgValue || orgValue === '') && (validText === '' || validText === null))) { SpreadJsObj.reLoadRowData(info.sheet, info.row); return; } // 判断部分值是否输入的是数字判断和数据计算 if (col.field === 'basic_price' || col.field === 'm_price') { if (isNaN(validText)) { toastr.error('不能输入其它非数字类型字符'); SpreadJsObj.reLoadRowData(info.sheet, info.row); return; } const num = parseFloat(validText.toPrecision(10)); if (validText !== null && num < 0) { toastr.error('请输入10位以内有效数字并且小于3位小数的浮点数'); SpreadJsObj.reLoadRowData(info.sheet, info.row); return; } else if (validText !== null) { if(!Number.isInteger(num) && !/^(\d{1,10}\.\d{1,3})?$/.test(num)) { toastr.warning('已保留3位小数'); } else if (validText.toString().length > 11) { toastr.warning('已保留10位有效数字'); } // SpreadJsObj.reLoadRowData(info.sheet, info.row); // return; validText = ZhCalc.round(num, 3); } } if (col.field === 'weight_num') { if (isNaN(validText)) { toastr.error('不能输入其它非数字类型字符'); SpreadJsObj.reLoadRowData(info.sheet, info.row); return; } let num = parseFloat(validText); if (validText !== null && (num < 0 || num >= 1)) { toastr.error('请输入0~1范围内并且小于3位小数的浮点数'); SpreadJsObj.reLoadRowData(info.sheet, info.row); return; } else if (validText !== null && num > 0 && num < 1 && !/^\d+(\.\d{1,3})?$/.test(num)) { toastr.warning('已保留3位小数'); validText = ZhCalc.round(num, 3); num = ZhCalc.round(num, 3); } const weight_datas = showStageExponent || exponent_nodes ? _.filter(materialExponentShardData, { ms_id: parseInt($('#myTab').find('.active').attr('data-msid')) || null, mn_id: parseInt($('#myTab2').find('.active').attr('data-mnid')) || null }) : materialExponentData; const total_weight = ZhCalc.add(ZhCalc.sub(ZhCalc.sum(_.map(weight_datas, 'weight_num')), parseFloat(orgValue)), num); if (total_weight > 1) { toastr.error('加权系数总和不能大于1'); SpreadJsObj.reLoadRowData(info.sheet, info.row); return; } } select[col.field] = validText; select.calc_num = materialExponentCol.getValue.calc_num(select); delete select.waitingLoading; // console.log(select); // 更新至服务器 postData(window.location.pathname + '/save', { type:'update', updateData: select, ms_id: $('#myTab').find('.active').attr('data-msid') || null, mn_id: $('#myTab2').find('.active').attr('data-mnid') || null }, function (result) { ex_tp = result.ex_tp; ex_tax_tp = result.ex_tax_tp; ex_expr = result.ex_expr || null; if (showStageExponent || exponent_nodes) { materialExponentSpreadObj.updateMaterialData(result); } resetExTpTable(); SpreadJsObj.reLoadRowData(info.sheet, info.row); // materialExponentData.splice(info.row, 1, select); }, function () { select[col.field] = orgValue; SpreadJsObj.reLoadRowData(info.sheet, info.row); }); } }, buttonClicked: function (e, info) { if (info.sheet.zh_setting) { const select = SpreadJsObj.getSelectObject(info.sheet); const col = info.sheet.zh_setting.cols[info.col]; if (col.field === 'weight_num') { if(materialExponentCol.readOnly.isConstant(select)) { return; } } else { if(materialExponentCol.readOnly.isEdit(select)) { return } } if (col.field === 'is_summary') { if (info.sheet.isEditing()) { info.sheet.endEdit(true); } select.is_summary = info.sheet.getValue(info.row, info.col) ? 1 : 0; delete select.waitingLoading; // 更新至服务器 postData(window.location.pathname + '/save', { type:'update', updateData: select, ms_id: $('#myTab').find('.active').attr('data-msid') || null, mn_id: $('#myTab2').find('.active').attr('data-mnid') || null }, function (result) { ex_tp = result.ex_tp; ex_tax_tp = result.ex_tax_tp; ex_expr = result.ex_expr || null; if (showStageExponent || exponent_nodes) { materialExponentSpreadObj.updateMaterialData(result); } resetExTpTable(); SpreadJsObj.reLoadRowData(info.sheet, info.row); }, function () { select.is_summary = info.sheet.getValue(info.row, info.col) ? 0 : 1; SpreadJsObj.reLoadRowData(info.sheet, info.row); }); } } }, deletePress: function (sheet) { if (!sheet.zh_setting || readOnly || sheet.zh_setting.readOnly) return; // materialExponentBase.isUsed(select) if (sheet.zh_setting && sheet.zh_data) { const sel = sheet.getSelections()[0]; if (!sel) return; const sortData = sheet.zh_data || []; const data = []; for (let iRow = sel.row; iRow < sel.row + sel.rowCount; iRow++) { // let bPaste = true; const exData = sortData[iRow]; const materialExData = { id: sortData[iRow].id }; for (let iCol = 0; iCol < sel.colCount; iCol++) { const curCol = sel.col + iCol; const colSetting = sheet.zh_setting.cols[curCol]; if(colSetting.readOnly(exData)) continue; if (!colSetting) continue; const orgValue = sortData[iRow][colSetting.field]; if(orgValue === null) { // bPaste = false; continue; } if (exData.type === materialType.ex_type[0].value && colSetting.field !== 'weight_num') { // bPaste = false; continue; } if (colSetting.field === 'type' || colSetting.field === 'calc_num' || colSetting.field === 'is_summary'){ // bPaste = false; continue; } materialExData[colSetting.field] = null; sortData[iRow][colSetting.field] = null; } if (exData.type !== materialType.ex_type[0].value) materialExData.calc_num = materialExponentCol.getValue.calc_num(sortData[iRow]); const arr = Object.keys(materialExData); if (arr.length !== 1) { data.push(materialExData); } } if (data.length === 0) { SpreadJsObj.reLoadSheetData(sheet); return; } // 更新至服务器 postData(window.location.pathname + '/save', { type:'paste', updateData: data, ms_id: $('#myTab').find('.active').attr('data-msid') || null, mn_id: $('#myTab2').find('.active').attr('data-mnid') || null }, function (result) { ex_tp = result.ex_tp; ex_tax_tp = result.ex_tax_tp; ex_expr = result.ex_expr || null; materialExponentSpreadObj.updateMaterialData(result); if (showStageExponent || exponent_nodes) { materialExponentSpreadObj.getMaterialExponentData(); } SpreadJsObj.loadSheetData(materialExponentSpread.getActiveSheet(), SpreadJsObj.DataType.Data, materialExponentData); resetExTpTable(); }, function () { SpreadJsObj.reLoadSheetData(sheet); return; }); } }, clipboardPasted(e, info) { const hint = { cellError: {type: 'error', msg: '粘贴内容超出了表格范围'}, numberExpr: {type: 'error', msg: '不能粘贴其它非数字类型字符'}, numberCan: {type: 'error', msg: '请粘贴10位以内有效数字并且小于3位小数的浮点数'}, numberCan2: {type: 'error', msg: '请粘贴0~1范围内并且小于3位小数的浮点数'}, numberCan3: {type: 'warning', msg: '已保留3位小数'}, numberCan4: {type: 'warning', msg: '已保留10位有效数字'}, weightNumberCan: {type: 'error', msg: '粘贴的加权系数总和不能大于1'}, }; const range = info.cellRange; const sortData = info.sheet.zh_data || []; if (info.cellRange.row + info.cellRange.rowCount > sortData.length) { toastMessageUniq(hint.cellError); // SpreadJsObj.loadSheetData(materialSpread.getActiveSheet(), SpreadJsObj.DataType.Data, materialBillsData); SpreadJsObj.reLoadSheetHeader(materialExponentSpread.getActiveSheet()); SpreadJsObj.reLoadSheetData(materialExponentSpread.getActiveSheet()); return; } if (sortData.length > 0 && range.col + range.colCount > 10) { toastMessageUniq(hint.cellError); SpreadJsObj.reLoadSheetHeader(materialExponentSpread.getActiveSheet()); SpreadJsObj.reLoadSheetData(materialExponentSpread.getActiveSheet()); return; } const data = []; // const rowData = []; for (let iRow = 0; iRow < range.rowCount; iRow++) { let bPaste = true; const curRow = range.row + iRow; // const materialData = JSON.parse(JSON.stringify(sortData[curRow])); const materialExData = { id: sortData[curRow].id }; const hintRow = range.rowCount > 1 ? curRow : ''; let sameCol = 0; for (let iCol = 0; iCol < range.colCount; iCol++) { const curCol = range.col + iCol; const colSetting = info.sheet.zh_setting.cols[curCol]; if (!colSetting) continue; let validText = info.sheet.getText(curRow, curCol); validText = is_numeric(validText) ? parseFloat(validText) : (validText ? trimInvalidChar(validText) : null); const orgValue = sortData[curRow][colSetting.field]; if (orgValue == validText || ((!orgValue || orgValue === '') && (validText === ''))) { sameCol++; if (range.colCount === sameCol) { bPaste = false; } continue; } if (colSetting.field === 'basic_price' || colSetting.field === 'm_price') { if (isNaN(validText)) { toastMessageUniq(getPasteHint(hint.numberExpr, hintRow)); bPaste = false; continue; } const num = parseFloat(validText.toPrecision(10)); if (validText !== null && num < 0) { toastMessageUniq(getPasteHint(hint.numberCan, hintRow)); bPaste = false; continue; } else if (validText !== null) { if(!Number.isInteger(num) && !/^(\d{1,10}\.\d{1,3})?$/.test(num)) { toastMessageUniq(getPasteHint(hint.numberCan3, hintRow)); } else if (validText.toString().length > 11) { toastMessageUniq(getPasteHint(hint.numberCan4, hintRow)); } // SpreadJsObj.reLoadRowData(info.sheet, info.row); // return; validText = ZhCalc.round(num, 3); } } if (colSetting.field === 'weight_num') { if (isNaN(validText)) { toastMessageUniq(getPasteHint(hint.numberExpr, hintRow)); bPaste = false; continue; } let num = parseFloat(validText); if (validText !== null && (num < 0 || num >= 1)) { toastMessageUniq(getPasteHint(hint.numberCan2, hintRow)); bPaste = false; continue; } else if (validText !== null && num > 0 && num < 1 && !/^\d+(\.\d{1,3})?$/.test(num)) { toastMessageUniq(getPasteHint(hint.numberCan3, hintRow)); validText = ZhCalc.round(num, 3); num = ZhCalc.round(num, 3); } const weight_datas = showStageExponent || exponent_nodes ? _.filter(materialExponentShardData, { ms_id: parseInt($('#myTab').find('.active').attr('data-msid')) || null, mn_id: parseInt($('#myTab2').find('.active').attr('data-mnid')) || null }) : materialExponentData; const total_weight = ZhCalc.add(ZhCalc.sub(ZhCalc.sum(_.map(weight_datas, 'weight_num')), parseFloat(orgValue)), num); if (total_weight > 1) { toastMessageUniq(getPasteHint(hint.weightNumberCan, hintRow)); bPaste = false; continue; } } materialExData[colSetting.field] = validText; sortData[curRow][colSetting.field] = validText; } if (bPaste) { materialExData.calc_num = materialExponentCol.getValue.calc_num(sortData[curRow]); data.push(materialExData); // rowData.push(curRow); } else { SpreadJsObj.reLoadRowData(info.sheet, curRow); } } if (data.length === 0) { SpreadJsObj.reLoadRowData(info.sheet, info.cellRange.row, info.cellRange.rowCount); return; } // console.log(data); // 更新至服务器 postData(window.location.pathname + '/save', { type:'paste', updateData: data, ms_id: $('#myTab').find('.active').attr('data-msid') || null, mn_id: $('#myTab2').find('.active').attr('data-mnid') || null }, function (result) { ex_tp = result.ex_tp; ex_tax_tp = result.ex_tax_tp; ex_expr = result.ex_expr || null; materialExponentSpreadObj.updateMaterialData(result); if (showStageExponent || exponent_nodes) { materialExponentSpreadObj.getMaterialExponentData(); } SpreadJsObj.loadSheetData(materialExponentSpread.getActiveSheet(), SpreadJsObj.DataType.Data, materialExponentData); resetExTpTable(); }, function () { SpreadJsObj.reLoadRowData(info.sheet, info.cellRange.row, info.cellRange.rowCount); return; }); }, setReadOnly: function(readOnly) { // SpreadJsObj.resetFieldReadOnly(materialSpread.getActiveSheet(), 'msg_spread', 'm_spread', 'm_tp', 'pre_tp', readOnly); }, _checkExprValid(expr) { if (!expr) return [true, null]; const param = []; let num = '', base = ''; for (let i = 0, iLen = expr.length; i < iLen; i++) { if (/^[\d\.%]+/.test(expr[i])) { if (base !== '') { param.push({type: 'base', value: base}); base = ''; } num = num + expr[i]; } else if (expr[i] === '(') { if (num !== '') { param.push({type: 'num', value: num}); num = ''; } if (base !== '') { param.push({type: 'base', value: base}); base = ''; } param.push({type: 'left', value: '('}); } else if (expr[i] === ')') { if (num !== '') { param.push({type: 'num', value: num}); num = ''; } if (base !== '') { param.push({type: 'base', value: base}); base = ''; } param.push({type: 'right', value: ')'}); } else if (/^[\+\-*\/]/.test(expr[i])) { if (num !== '') { param.push({type: 'num', value: num}); num = ''; } if (base !== '') { param.push({type: 'base', value: base}); base = ''; } param.push({type: 'calc', value: expr[i]}); } else { return [false, '输入的表达式含有非法字符: ' + expr[i]]; } } if (num !== '') { param.push({type: 'num', value: num}); num = ''; } if (base !== '') { param.push({type: 'base', value: base}); base = ''; } if (param.length === 0) return true; if (param.length > 1) { if (param[0].value === '-' && param[1].type === 'num') { param[1].value = '-' + param[1].value; param.shift(); } } const iLen = param.length; let iLeftCount = 0, iRightCount = 0; for (const [i, p] of param.entries()) { if (p.type === 'calc') { if (i === 0 || i === iLen - 1) return [false, '输入的表达式非法:计算符号' + p.value + '前后应有数字']; } if (p.type === 'num') { num = p.value.replace('%', ''); if (p.value.length - num.length > 1) return [false, '输入的表达式非法:' + p.value + '不是一个有效的数字']; num = _.toNumber(num); if (num === undefined || num === null || _.isNaN(num)) return [false, '输入的表达式非法:' + p.value + '不是一个有效的数字']; if (i > 0) { if (param[i - 1].type !== 'calc' && param[i - 1].type !== 'left') { return [false, '输入的表达式非法:' + p.value + '前应有运算符']; } else if (param[i - 1].value === '/' && num === 0) { return [false, '输入的表达式非法:请勿除0']; } } } if (p.type === 'base') { if (i > 0 && (param[i - 1].type === 'num' || param[i - 1].type === 'right')) return [false, '输入的表达式非法:' + p.value + '前应有运算符']; } if (p.type === 'left') { iLeftCount += 1; if (i !== 0 && param[i-1].type !== 'calc') return [false, '输入的表达式非法:(前应有运算符']; } if (p.type === 'right') { iRightCount += 1; if (i !== iLen - 1 && param[i+1].type !== 'calc') return [false, '输入的表达式非法:)后应有运算符']; if (iRightCount > iLeftCount) return [false, '输入的表达式非法:")"前无对应的"("']; } } if (iLeftCount > iRightCount) return [false, '输入的表达式非法:"("后无对应的")"']; return [true, '']; }, _checkExpr: function (text, data) { if (text) { const num = _.toNumber(text); if (num) { data.quantity = num; data.expr = ''; } else { const expr = $.trim(text).replace('\t', '').replace('=', '').toLowerCase(); const [valid, msg] = this._checkExprValid(expr); if (!valid) return [valid, msg]; data.expr = expr; data.quantity = ZhCalc.calcExpr.calcExprStrRpn(expr.replace(new RegExp('%', 'gm'), '/100')); // const ce = new CalcEvalMin(); // data.quantity = ce.eval(expr); // console.log(data.quantity); } } else { data.quantity = 0; data.expr = ''; } return [true, '']; }, }; SpreadJsObj.initSpreadSettingEvents(materialExponentSpreadSetting, materialExponentCol); SpreadJsObj.initSheet(materialExponentSpread.getActiveSheet(), materialExponentSpreadSetting); materialExponentSpreadObj.getMaterialExponentData(); SpreadJsObj.loadSheetData(materialExponentSpread.getActiveSheet(), SpreadJsObj.DataType.Data, materialExponentData); if (showStageExponent || exponent_nodes) setExCalc(); if (exponent_nodes) resetExTpTable(); materialExponentSpreadObj.refreshActn(); // 期切换 $('#myTab a[data-toggle="tab"]').on('shown.bs.tab', function (e) { e.preventDefault(); showWaitingView(); setTimeout(function () { if (exponent_nodes) { materialExponentSpreadObj.updateMaterialNodeTab(); } else { materialExponentSpreadObj.getMaterialExponentData(); materialExponentSpreadObj.materialSheetReset(true); setExCalc(); } closeWaitingView(); }, 1000); }); // 分项切换 $('#myTab2 a[data-toggle="tab"]').on('shown.bs.tab', function (e) { e.preventDefault(); showWaitingView(); setTimeout(function () { materialExponentSpreadObj.getMaterialExponentData(); materialExponentSpreadObj.materialSheetReset(true); setExCalc(); closeWaitingView(); }, 1000); }); function setExCalc(msid = null, mnid = null) { let html = ''; const mn_id = mnid || $('#myTab2').find('.active').attr('data-mnid') || null; const ms_id = msid || $('#myTab').find('.active').attr('data-msid') || null; const condition = {}; if (mn_id) condition.mn_id = parseInt(mn_id); if (ms_id) condition.ms_id = parseInt(ms_id); ex_calc = _.find(ex_calcList, condition).ex_calc; for (const bq of ex_calc) { html += `