合并单元格

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) { function addRowIdToData(content) {
console.log('每行data数据1', content) console.log('每行data数据1', content)
var data = JSON.parse(JSON.stringify(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++) { for (let i = 0; i < data.length; i++) {
data[i].rowId = `row-${idCounter}`; if (usedRows.has(i)) continue;
idCounter++;
for (let j = 0; j < data[i].length; j++) { const rowId = `row-${idCounter++}`;
const cell = data[i][j]; rowIdMap[i] = rowId;
if (cell && cell.text && cell.rowspan && cell.rowspan > 1) { usedRows.add(i);
for (let k = 0; k < cell.rowspan; k++) {
if (data[i + k]) { const row = data[i];
for (let j = 0; j < row.length; j++) {
data[i + k][j] = { ...data[i + k][j], rowId: `row-${idCounter - 1}` }; 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);
} }
}
} }
}
} }
console.log('每行data数据:', JSON.parse(JSON.stringify(data)))
// 把 rowId 实际写进每个单元格
for (let i = 0; i < data.length; i++) {
const rowId = rowIdMap[i];
for (let j = 0; j < data[i].length; j++) {
if (!data[i][j]) data[i][j] = {};
data[i][j].rowId = rowId;
}
}
console.log('每行data数据111:', JSON.parse(JSON.stringify(data)))
const seenIds = []; const seenIds = [];
@@ -8658,25 +8675,35 @@ ${header
`; `;
}) })
.join('')} .join('')}
${rowData ${rowData
.map((row, i) => { .map((row, i) => {
return ` return `
<tr class="table-content-row ${row.rowId && rowIds.includes(row.rowId) ? 'oddColor' : ''}" > <tr class="table-content-row ${row.rowId && rowIds.includes(row.rowId) ? 'oddColor' : ''}">
${row ${row
.map((cell, cellIndex) => { .map((cell) => {
if (cell && cell.cellId) { if (!cell || !cell.cellId) return '';
return `
<td colspan="${cell.colspan || 1}" rowspan="${cell.rowspan || 1}" >
<span style="font-family: 'Charis SIL';">${cell.text || ''}</span>
</td>
`;
} else {
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';">${finalContent}</span>
</td>`;
}) })
.join('')} .join('')}
</tr> </tr>
`; `;
}) })
.join('')} .join('')}