合并单元格

This commit is contained in:
2025-06-11 10:32:15 +08:00
parent 83e9cbcf14
commit 7dedfc1ba2

View File

@@ -8544,24 +8544,41 @@ function shouldColor(rowIndex, cellIndex, row) {
function addRowIdToData(content) {
console.log('每行data数据1', content)
var data = JSON.parse(JSON.stringify(content))
let idCounter = 0; // Initialize an ID counter
const rowIdMap = {};
const usedRows = new Set();
let idCounter = 0;
for (let i = 0; i < data.length; i++) {
data[i].rowId = `row-${idCounter}`;
idCounter++;
if (usedRows.has(i)) continue;
const rowId = `row-${idCounter++}`;
rowIdMap[i] = rowId;
usedRows.add(i);
const row = data[i];
for (let j = 0; j < row.length; j++) {
const cell = row[j];
if (cell?.rowspan && cell.rowspan > 1) {
for (let k = 1; k < cell.rowspan; k++) {
const nextRowIndex = i + k;
if (nextRowIndex < data.length && !rowIdMap[nextRowIndex]) {
rowIdMap[nextRowIndex] = rowId;
usedRows.add(nextRowIndex);
}
}
}
}
}
// 把 rowId 实际写进每个单元格
for (let i = 0; i < data.length; i++) {
const rowId = rowIdMap[i];
for (let j = 0; j < data[i].length; j++) {
const cell = data[i][j];
if (cell && cell.text && cell.rowspan && cell.rowspan > 1) {
for (let k = 0; k < cell.rowspan; k++) {
if (data[i + k]) {
data[i + k][j] = { ...data[i + k][j], rowId: `row-${idCounter - 1}` };
if (!data[i][j]) data[i][j] = {};
data[i][j].rowId = rowId;
}
}
}
}
}
console.log('每行data数据:', JSON.parse(JSON.stringify(data)))
console.log('每行data数据111:', JSON.parse(JSON.stringify(data)))
const seenIds = [];
@@ -8663,17 +8680,27 @@ ${rowData
return `
<tr class="table-content-row ${row.rowId && rowIds.includes(row.rowId) ? 'oddColor' : ''}">
${row
.map((cell, cellIndex) => {
if (cell && cell.cellId) {
.map((cell) => {
if (!cell || !cell.cellId) return '';
const content = cell.text || '';
const isBase64Image = /^<img\s+[^>]*src=["']data:image\//i.test(content);
const finalContent = isBase64Image
? content.replace(
/<img\s+([^>]*?)src=["'](data:image\/[^"']+)["']([^>]*)>/gi,
(match, preAttrs, src, postAttrs) => {
const safeSrc = src.replace(/'/g, "\\'");
return `<img ${preAttrs}src="${src}"${postAttrs} style="cursor:pointer;" onclick="picPreview('${safeSrc}')" />`;
}
)
: content;
return `
<td colspan="${cell.colspan || 1}" rowspan="${cell.rowspan || 1}">
<span style="font-family: 'Charis SIL';">${cell.text || ''}</span>
</td>
`;
} else {
return ``
}
<span style="font-family: 'Charis SIL';">${finalContent}</span>
</td>`;
})
.join('')}
</tr>