|
|
|
@ -1286,12 +1286,25 @@ nextTick(()=>{ |
|
|
|
let tby=getLastColonAfterString(element.tby) |
|
|
|
/* console.log(tbx) |
|
|
|
console.log(tby) */ |
|
|
|
|
|
|
|
//console.log(element.name) |
|
|
|
let rowValue = model.value[tbx] |
|
|
|
//console.log(rowValue) |
|
|
|
let columnValue = model.value[tby] |
|
|
|
let cellValue = getTableCellValueV2(element.tableData, rowValue, columnValue) |
|
|
|
//console.log(cellValue); |
|
|
|
//console.log(element.name) |
|
|
|
//console.log(columnValue) |
|
|
|
if(tbx!=""&&tby!=""){ |
|
|
|
|
|
|
|
let cellValue = getTableCellValue(element.tableData, rowValue, columnValue) |
|
|
|
model.value[element.name] = cellValue*1 |
|
|
|
}else if(tbx!=""&&tby==""){ |
|
|
|
//console.log("仅设置了标题行") |
|
|
|
let cellValue = getTableCellValue(element.tableData, rowValue, 'default') |
|
|
|
//console.log(cellValue) |
|
|
|
model.value[element.name] = cellValue*1 |
|
|
|
}else if(tbx==""&&tby!=""){ |
|
|
|
//console.log("仅设置了索引列") |
|
|
|
let cellValue = getTableCellValue(element.tableData, 'any', columnValue) |
|
|
|
//console.log(cellValue) |
|
|
|
model.value[element.name] = cellValue*1 |
|
|
|
} |
|
|
|
|
|
|
|
@ -1313,7 +1326,7 @@ nextTick(()=>{ |
|
|
|
function getLastColonAfterString(str) { |
|
|
|
// 1. 输入校验:确保输入是字符串类型(处理非字符串入参) |
|
|
|
if (typeof str !== 'string') { |
|
|
|
console.warn('输入必须为字符串类型'); |
|
|
|
//console.warn('输入必须为字符串类型'); |
|
|
|
return ''; |
|
|
|
} |
|
|
|
|
|
|
|
@ -1330,8 +1343,20 @@ function getLastColonAfterString(str) { |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// 根据自动填充表格行列数据确定单元格值 |
|
|
|
function getTableCellValueV2(tableData, rowValue, columnValue) { |
|
|
|
|
|
|
|
/** |
|
|
|
* 获取表格单元格值,兼容多种tableData结构 |
|
|
|
* 支持三种结构: |
|
|
|
* 1. 标准行列结构:{列号: {行号: 值, ...}, ...} |
|
|
|
* 2. 仅一行结构:{default: {列号: 值, ...}} |
|
|
|
* 3. 仅一列结构:{列号: {default: 值}, ...} |
|
|
|
* |
|
|
|
* @param {Object} tableData - 表格数据 |
|
|
|
* @param {String|Number} rowValue - 行标识 |
|
|
|
* @param {String|Number} columnValue - 列标识 |
|
|
|
* @returns {String|undefined} - 返回对应的单元格值,如果未找到则返回undefined |
|
|
|
*/ |
|
|
|
function getTableCellValueV3(tableData, rowValue, columnValue) { |
|
|
|
try { |
|
|
|
// 检查输入参数 |
|
|
|
if (!tableData || typeof tableData !== 'object' || Array.isArray(tableData)) { |
|
|
|
@ -1347,8 +1372,59 @@ function getTableCellValueV2(tableData, rowValue, columnValue) { |
|
|
|
const rowStr = String(rowValue); |
|
|
|
const columnStr = String(columnValue); |
|
|
|
|
|
|
|
// 使用可选链操作符安全地访问嵌套属性 |
|
|
|
return tableData?.[columnStr]?.[rowStr]; |
|
|
|
// 1. 首先尝试检测是否为仅一行结构(有default行) |
|
|
|
if (tableData.default && typeof tableData.default === 'object') { |
|
|
|
// 这是仅一行结构:{default: {列号: 值, ...}} |
|
|
|
// 忽略行值,直接使用列值 |
|
|
|
return tableData.default?.[columnStr]; |
|
|
|
} |
|
|
|
|
|
|
|
// 2. 尝试标准结构或仅一列结构 |
|
|
|
const columnObj = tableData[columnStr]; |
|
|
|
if (columnObj && typeof columnObj === 'object') { |
|
|
|
// 2.1 尝试从列对象中获取行值 |
|
|
|
const cellValue = columnObj[rowStr]; |
|
|
|
if (cellValue !== undefined) { |
|
|
|
return cellValue; // 找到标准结构的值 |
|
|
|
} |
|
|
|
|
|
|
|
// 2.2 如果没找到标准结构的行值,检查是否是仅一列结构(有default值) |
|
|
|
if (columnObj.default !== undefined) { |
|
|
|
// 这是仅一列结构:{列号: {default: 值}, ...} |
|
|
|
// 忽略行值,返回该列的default值 |
|
|
|
return columnObj.default; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
// 3. 如果没有匹配任何结构,尝试其他可能的查找方式 |
|
|
|
|
|
|
|
// 3.1 如果列不存在,检查所有列是否有default值(用于仅一列结构) |
|
|
|
if (!columnObj) { |
|
|
|
// 遍历所有列,看是否有匹配的列号 |
|
|
|
for (const colKey in tableData) { |
|
|
|
if (String(colKey) === columnStr) { |
|
|
|
const colData = tableData[colKey]; |
|
|
|
if (colData && typeof colData === 'object' && colData.default !== undefined) { |
|
|
|
return colData.default; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
// 3.2 检查是否有default行(可能列名不是字符串"default") |
|
|
|
for (const key in tableData) { |
|
|
|
const data = tableData[key]; |
|
|
|
if (data && typeof data === 'object') { |
|
|
|
// 检查这个对象是否是一个行数据(包含列号作为键) |
|
|
|
if (data[columnStr] !== undefined) { |
|
|
|
// 这可能是行数据,返回对应的列值 |
|
|
|
return data[columnStr]; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
// 4. 所有尝试都失败,返回undefined |
|
|
|
return undefined; |
|
|
|
} catch (error) { |
|
|
|
// 如果发生任何错误,返回undefined |
|
|
|
console.error('获取表格单元格值时发生错误:', error); |
|
|
|
@ -1356,6 +1432,220 @@ function getTableCellValueV2(tableData, rowValue, columnValue) { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 增强版:自动检测tableData结构并获取值 |
|
|
|
* 支持动态结构检测 |
|
|
|
*/ |
|
|
|
function getTableCellValueEnhanced(tableData, rowValue, columnValue) { |
|
|
|
try { |
|
|
|
if (!tableData || typeof tableData !== 'object') { |
|
|
|
return undefined; |
|
|
|
} |
|
|
|
|
|
|
|
if (rowValue == null || columnValue == null) { |
|
|
|
return undefined; |
|
|
|
} |
|
|
|
|
|
|
|
const rowStr = String(rowValue); |
|
|
|
const columnStr = String(columnValue); |
|
|
|
|
|
|
|
// 先尝试直接获取(标准结构) |
|
|
|
const directValue = tableData?.[columnStr]?.[rowStr]; |
|
|
|
if (directValue !== undefined) { |
|
|
|
return directValue; |
|
|
|
} |
|
|
|
|
|
|
|
// 检测表格结构 |
|
|
|
const tableStructure = detectTableStructure(tableData); |
|
|
|
|
|
|
|
switch (tableStructure) { |
|
|
|
case 'standard': |
|
|
|
// 标准结构,已经尝试过直接获取 |
|
|
|
break; |
|
|
|
|
|
|
|
case 'singleRow': |
|
|
|
// 仅一行结构 |
|
|
|
return getFromSingleRowStructure(tableData, columnStr); |
|
|
|
|
|
|
|
case 'singleColumn': |
|
|
|
// 仅一列结构 |
|
|
|
return getFromSingleColumnStructure(tableData, columnStr); |
|
|
|
|
|
|
|
case 'mixed': |
|
|
|
// 混合结构,尝试多种方式 |
|
|
|
return getFromMixedStructure(tableData, rowStr, columnStr); |
|
|
|
|
|
|
|
default: |
|
|
|
// 未知结构,返回undefined |
|
|
|
return undefined; |
|
|
|
} |
|
|
|
|
|
|
|
return undefined; |
|
|
|
} catch (error) { |
|
|
|
console.error('获取表格单元格值时发生错误:', error); |
|
|
|
return undefined; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 检测tableData的结构 |
|
|
|
* @returns {string} - 结构类型:'standard', 'singleRow', 'singleColumn', 'mixed', 'unknown' |
|
|
|
*/ |
|
|
|
function detectTableStructure(tableData) { |
|
|
|
if (!tableData || typeof tableData !== 'object') { |
|
|
|
return 'unknown'; |
|
|
|
} |
|
|
|
|
|
|
|
const keys = Object.keys(tableData); |
|
|
|
if (keys.length === 0) { |
|
|
|
return 'empty'; |
|
|
|
} |
|
|
|
|
|
|
|
// 检查是否为仅一行结构(有default键) |
|
|
|
if (keys.length === 1 && keys[0] === 'default') { |
|
|
|
const defaultData = tableData.default; |
|
|
|
if (defaultData && typeof defaultData === 'object') { |
|
|
|
// 检查default对象的值是否都是简单值(不是对象) |
|
|
|
const hasNestedObjects = Object.values(defaultData).some( |
|
|
|
value => value && typeof value === 'object' |
|
|
|
); |
|
|
|
if (!hasNestedObjects) { |
|
|
|
return 'singleRow'; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
// 检查是否为仅一列结构 |
|
|
|
let isSingleColumn = true; |
|
|
|
for (const key of keys) { |
|
|
|
const colData = tableData[key]; |
|
|
|
if (!colData || typeof colData !== 'object') { |
|
|
|
isSingleColumn = false; |
|
|
|
break; |
|
|
|
} |
|
|
|
|
|
|
|
// 检查是否只有default键或只有一个键 |
|
|
|
const colKeys = Object.keys(colData); |
|
|
|
if (colKeys.length !== 1 || (colKeys.length === 1 && colKeys[0] !== 'default')) { |
|
|
|
// 如果有多个键,或者唯一的键不是default,则不是仅一列结构 |
|
|
|
if (colKeys.length > 1 || (colKeys.length === 1 && !colKeys[0].match(/^\d+$/))) { |
|
|
|
isSingleColumn = false; |
|
|
|
break; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
if (isSingleColumn) { |
|
|
|
return 'singleColumn'; |
|
|
|
} |
|
|
|
|
|
|
|
// 检查是否为标准结构(所有值都是对象,且对象键是行号) |
|
|
|
let isStandard = true; |
|
|
|
for (const key of keys) { |
|
|
|
const colData = tableData[key]; |
|
|
|
if (!colData || typeof colData !== 'object') { |
|
|
|
isStandard = false; |
|
|
|
break; |
|
|
|
} |
|
|
|
|
|
|
|
// 检查对象键是否都是数字字符串(表示行号) |
|
|
|
const colKeys = Object.keys(colData); |
|
|
|
if (colKeys.length === 0) { |
|
|
|
isStandard = false; |
|
|
|
break; |
|
|
|
} |
|
|
|
|
|
|
|
// 允许有default键和其他键混合 |
|
|
|
const hasNonNumericKeys = colKeys.some(k => k !== 'default' && !k.match(/^\d+$/)); |
|
|
|
if (hasNonNumericKeys) { |
|
|
|
isStandard = false; |
|
|
|
break; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
if (isStandard) { |
|
|
|
return 'standard'; |
|
|
|
} |
|
|
|
|
|
|
|
// 混合结构 |
|
|
|
return 'mixed'; |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 从仅一行结构中获取值 |
|
|
|
*/ |
|
|
|
function getFromSingleRowStructure(tableData, columnStr) { |
|
|
|
return tableData.default?.[columnStr]; |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 从仅一列结构中获取值 |
|
|
|
*/ |
|
|
|
function getFromSingleColumnStructure(tableData, columnStr) { |
|
|
|
const colData = tableData[columnStr]; |
|
|
|
if (colData && typeof colData === 'object') { |
|
|
|
// 优先返回default值,如果没有则返回第一个值 |
|
|
|
return colData.default !== undefined ? colData.default : Object.values(colData)[0]; |
|
|
|
} |
|
|
|
return undefined; |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 从混合结构中获取值 |
|
|
|
*/ |
|
|
|
function getFromMixedStructure(tableData, rowStr, columnStr) { |
|
|
|
// 尝试多种方式 |
|
|
|
|
|
|
|
// 1. 尝试标准方式 |
|
|
|
const standardValue = tableData?.[columnStr]?.[rowStr]; |
|
|
|
if (standardValue !== undefined) { |
|
|
|
return standardValue; |
|
|
|
} |
|
|
|
|
|
|
|
// 2. 检查该列是否有default值 |
|
|
|
const columnData = tableData[columnStr]; |
|
|
|
if (columnData && typeof columnData === 'object') { |
|
|
|
if (columnData.default !== undefined) { |
|
|
|
return columnData.default; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
// 3. 检查是否有default行 |
|
|
|
if (tableData.default && typeof tableData.default === 'object') { |
|
|
|
return tableData.default[columnStr]; |
|
|
|
} |
|
|
|
|
|
|
|
// 4. 遍历查找 |
|
|
|
for (const key in tableData) { |
|
|
|
const data = tableData[key]; |
|
|
|
if (data && typeof data === 'object') { |
|
|
|
// 如果这个对象有我们要找的列键 |
|
|
|
if (data[columnStr] !== undefined) { |
|
|
|
return data[columnStr]; |
|
|
|
} |
|
|
|
// 如果键名匹配列,并且有我们要找的行键 |
|
|
|
if (key === columnStr && data[rowStr] !== undefined) { |
|
|
|
return data[rowStr]; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
return undefined; |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 统一方法,推荐使用这个 |
|
|
|
*/ |
|
|
|
function getTableCellValue(tableData, rowValue, columnValue) { |
|
|
|
return getTableCellValueEnhanced(tableData, rowValue, columnValue); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function addStringIfNotExists(arr: string[], str: string) { |
|
|
|
if (!arr.includes(str)) { |
|
|
|
arr.push(str); |
|
|
|
|