import { transDecimalPlace, getDecimalPlaces } from '../common/customTable'; export default { methods: { // 判断时间 isDateInRange(dateStr) { const today = new Date(); const tomorrow = new Date(today); tomorrow.setDate(today.getDate() + 1); const sevenDaysAgo = new Date(today); // 当前日期的副本 sevenDaysAgo.setDate(sevenDaysAgo.getDate() - 7); // 减去6天 const date = new Date(dateStr); const startOfWeek = new Date( today.getFullYear(), today.getMonth(), today.getDate() - today.getDay() + 1 ); const lastWeekfristDay = new Date( today.getFullYear(), today.getMonth(), today.getDate() - today.getDay() + 1 ); lastWeekfristDay.setDate(lastWeekfristDay.getDate() - 7); const endOfWeek = new Date( today.getFullYear(), today.getMonth(), today.getDate() + (7 - today.getDay()) ); const nextWeekLastDay = new Date( today.getFullYear(), today.getMonth(), today.getDate() + (7 - today.getDay()) ); nextWeekLastDay.setDate(nextWeekLastDay.getDate() + 7); const startOfMonth = new Date(today.getFullYear(), today.getMonth(), 1); const startOfLastMonth = new Date( today.getFullYear(), today.getMonth() - 1, 1 ); const endOfMonth = new Date(today.getFullYear(), today.getMonth() + 1, 0); const endOfNextMonth = new Date( today.getFullYear(), today.getMonth() + 2, 0 ); return { today: date.toDateString() === today.toDateString(), tomorrow: date.toDateString() === tomorrow.toDateString(), thisweek: date >= startOfWeek && date <= endOfWeek, lastweek: date < startOfWeek && date >= lastWeekfristDay, nextweek: date > endOfWeek && date <= nextWeekLastDay, thismonth: date >= startOfMonth && date <= endOfMonth, lastmonth: date < startOfMonth && date >= endOfNextMonth, nextmonth: date > endOfMonth && date <= endOfNextMonth, last7days: date <= today && date > sevenDaysAgo, }; }, // 删除规则 deleteExcelRule(item) { // console.log(item); this.resetTableData() this.getExcelRule(); }, // 修改规则 editExcelRule(item) { // console.log(item); this.resetTableData() this.getExcelRule(); }, // 清空全部单元格样式 resetTableData() { this.config.data.forEach((el) => { el.forEach((item) => { const styleCss = item.ShowStyle ? JSON.parse( item.ShowStyle ) : {}; item.ShowStyle = JSON.stringify({ ...styleCss, glObj: null, }) }) }) }, commonInitCell(initType = "change", rulist = this.excelRuleList) { if (initType == 'edit') { this.resetTableData() } // 获取表格 const table = document.getElementById("myTable"); rulist.forEach((el) => { let indexArr = []; if (el.ScopeShow.indexOf(":") > -1) { let rowCols = this.getRowsAndColumns(el.ScopeShow); // console.log(rowCols); if (rowCols.length === 2) { indexArr = this.getCellIndices( table, rowCols[0].row, rowCols[1].row, rowCols[0].column, rowCols[1].column ); } } else { indexArr = this.getRowsAndColumns(el.ScopeShow); } // console.log(indexArr); indexArr.forEach((item) => { if (this.config.data[item.row - 1] && this.config.data[item.row - 1][item.column - 1]) { // 先判断类型然后取值,再去进行比较,符合再去设置样式 判断两个值都是数字 const keyObj = { 1: "ShowValue", 3: "Value", 5: "ShowValue", }; const types = this.config.data[item.row - 1][item.column - 1].DataType; const cellValue = this.config.data[item.row - 1][item.column - 1]; const val = cellValue[types == 3 ? "Value" : "ShowValue"]; const leftVal = el.RuleType == 5 ? el.LeftValue : Number.isFinite(+el.LeftValue) ? el.LeftValue : this.config.data[ this.getRowsAndColumns(el.LeftValue)[0].row - 1 ][this.getRowsAndColumns(el.LeftValue)[0].column - 1][ this.config.data[ this.getRowsAndColumns(el.LeftValue)[0].row - 1 ][this.getRowsAndColumns(el.LeftValue)[0].column - 1] .DataType == 3 ? "Value" : "ShowValue" ]; // console.log(leftVal) const rightVal = Number.isFinite(+el.RightValue) ? el.RightValue : this.config.data[ this.getRowsAndColumns(el.RightValue)[0].row - 1 ][this.getRowsAndColumns(el.RightValue)[0].column - 1][ this.config.data[ this.getRowsAndColumns(el.RightValue)[0].row - 1 ][this.getRowsAndColumns(el.RightValue)[0].column - 1] .DataType == 3 ? "Value" : "ShowValue" ]; let tag = false; const regex = /^\d{4}-\d{2}-\d{2}$/; if ( (val != '' && Number.isFinite(+val) && leftVal != '' && Number.isFinite(+leftVal) && (el.RuleType == 3 && rightVal ? Number.isFinite(+rightVal) : true) && el.RuleType != 5) || (el.RuleType == 5 && cellValue.ShowValue && regex.test(cellValue.ShowValue)) ) { switch (+el.RuleType) { case 1: if (+val > +leftVal) { tag = true; } break; case 2: if (+val < +leftVal) { // console.log(+val < +leftVal); tag = true; } break; case 3: // console.log(el.RuleType, 3333); if (+val >= +leftVal && +val <= +rightVal) { tag = true; } break; case 4: if (+val == +leftVal) { tag = true; } break; case 5: const dateRanges = this.isDateInRange(cellValue.ShowValue); if (dateRanges[el.LeftValue]) { tag = true; } break; default: break; } // console.log(tag); const showStyle = this.config.data[item.row - 1][item.column - 1].ShowStyle; const styleCss = showStyle ? JSON.parse( showStyle ) : {}; let glObj = tag ? { color: el.FontColor ? el.FontColor : "#606266", "background-color": el.BackgroundColor ? el.BackgroundColor : "#fff", } : styleCss.glObj; this.config.data[item.row - 1][item.column - 1].ShowStyle = JSON.stringify({ ...styleCss, glObj, }); } } }); }); }, //获取选区及值的下标数组 getRowsAndColumns(input) { const matches = input.match(/\$([A-Z])\$(\d+)/g) || []; const result = []; // console.log(matches); matches.forEach((match) => { const [, column, row] = match.match(/\$([A-Z])\$(\d+)/); result.push({ row: Number(row), column: Number(this.getColumnHeaderIndex(column) + 1), }); }); return result; }, //获取字母对应下标 getColumnHeaderIndex(code) { return code.charCodeAt() - 65; }, // 获取区间所有坐标 getCellIndices(table, startRow, endRow, startCol, endCol) { // console.log(startRow, endRow, startCol, endCol); const indices = []; for (let i = startRow; i <= endRow; i++) { for (let j = startCol; j <= endCol; j++) { indices.push({ row: i, column: j }); } } return indices; }, // 展示小数 showDecimalValue(cell) { const styleCss = cell.ShowStyle ? JSON.parse(cell.ShowStyle) : {}; let Value = '' if (styleCss.nt == 'percent' && cell.ShowFormatValue.indexOf('%') > -1) { if (styleCss.last == 'nt') { Value = this.commonDecimalValue(cell.ShowValue * 100, styleCss.decimal) + '%' } else { Value = (parseFloat(this.commonDecimalValue(cell.ShowValue, styleCss.decimal) * 100)) + '%' } } else { Value = parseFloat(this.commonDecimalValue(cell.ShowValue, styleCss.decimal)).toFixed(styleCss.decimal) } if (getDecimalPlaces(cell.ShowValue) < styleCss.decimal || styleCss.nt == 'percent') { Value = transDecimalPlace(cell.ShowValue + '', { ...styleCss, pn: styleCss.decimal - getDecimalPlaces(cell.ShowValue) + (styleCss.nt == 'percent'&&getDecimalPlaces(cell.ShowValue)!=0 ? 2 : 0) }) } // console.log(cell) this.$set(this.config.data[cell.rIndex][cell.cIndex],'ShowFormatValue',Value) return Value }, commonDecimalValue(values, decimal) { const multiplier = Math.pow(10, decimal); return decimal == 0 ? Math.round(values) : Math.round(values * multiplier) / multiplier; }, // 是否展示小数 isShowDecimal(cell) { // console.log(cell,111) const styleCss = cell.ShowStyle ? JSON.parse(cell.ShowStyle) : {}; let tag = !isNaN(parseFloat(cell.ShowValue)) && isFinite(cell.ShowValue) return tag ? !isNaN(parseFloat(styleCss.decimal)) && isFinite(styleCss.decimal) : false }, // 由于在showStyle做了背景色及字体颜色处理,解决判断冲突 isShowFormat(style, type = "css") { const styleCss = style ? JSON.parse(style) : {}; let tag = type === "css" ? styleCss.pn > 0 || styleCss.pn < 0 || ["percent"].includes(styleCss.nt) : style.EdbInfoId; return tag; }, // 初始化单元格横纵坐标 initIndex(rindex, cindex, row, col) { this.config.data[rindex][cindex]["cIndex"] = cindex; this.config.data[rindex][cindex]["rIndex"] = rindex; this.config.data[rindex][cindex]["colIndex"] = col; this.config.data[rindex][cindex]["rowIndex"] = row; }, // 设置背景色及字体颜色 getShowCss(style) { const styleCss = JSON.parse(style); let color = styleCss.glObj ? styleCss.glObj["color"] : styleCss["color"]; let BackgroundColor = styleCss.glObj ? styleCss.glObj["background-color"] : styleCss["background-color"]; return { color: color, "background-color": BackgroundColor, }; }, }, };