Исправлена мутация данных - теперь создаются копии строк

This commit is contained in:
Nurs 2026-09-17 03:42:55 +00:00
parent 3cd599f56b
commit c1a2b091f2

View File

@ -202,22 +202,25 @@ function processMapping() {
} }
}); });
// Сопоставляем данные // Сопоставляем данные (без мутации!)
let matched = 0; let matched = 0;
let notMatched = 0; let notMatched = 0;
let filled = 0; let filled = 0;
const previewRows = templateData.slice(0, 10).map((templateRow, index) => { const previewRows = templateData.slice(0, 10).map((templateRow, index) => {
// Создаём КОПИЮ строки, чтобы не мутировать исходные данные
const rowCopy = { ...templateRow };
const keyValue = normalizeValue(templateRow[templateKeyField]); const keyValue = normalizeValue(templateRow[templateKeyField]);
const mainRow = mainMap.get(keyValue); const mainRow = mainMap.get(keyValue);
if (mainRow) { if (mainRow) {
matched++; matched++;
// Заполняем пустые поля // Заполняем пустые поля в копии
fieldsToFill.forEach(field => { fieldsToFill.forEach(field => {
if (!templateRow[field] && mainRow[field]) { const isEmpty = rowCopy[field] === null || rowCopy[field] === undefined || rowCopy[field] === '';
if (isEmpty && mainRow[field] !== null && mainRow[field] !== undefined) {
filled++; filled++;
templateRow[field] = mainRow[field]; rowCopy[field] = mainRow[field];
} }
}); });
} else { } else {
@ -225,7 +228,8 @@ function processMapping() {
} }
return { return {
templateRow, templateRow: rowCopy,
originalRow: templateRow,
mainRow, mainRow,
matched: !!mainRow matched: !!mainRow
}; };
@ -268,10 +272,12 @@ function processMapping() {
previewRows.forEach(row => { previewRows.forEach(row => {
bodyHtml += '<tr class="' + (row.matched ? 'matched' : 'no-match') + '">'; bodyHtml += '<tr class="' + (row.matched ? 'matched' : 'no-match') + '">';
allFields.forEach(field => { allFields.forEach(field => {
const value = row.templateRow[field] !== undefined ? row.templateRow[field] : ''; const originalValue = row.originalRow[field];
const fromMain = row.mainRow && row.mainRow[field] && !row.templateRow[field]; const newValue = row.templateRow[field];
const cellStyle = fromMain ? 'background: var(--kt-ai-success-bg); font-weight: 600;' : ''; const wasEmpty = originalValue === null || originalValue === undefined || String(originalValue).trim() === '';
bodyHtml += '<td style="' + cellStyle + '">' + escapeHtml(value) + '</td>'; const filled = wasEmpty && newValue !== null && newValue !== undefined;
const cellStyle = filled ? 'background: var(--kt-ai-success-bg); font-weight: 600;' : '';
bodyHtml += '<td style="' + cellStyle + '">' + escapeHtml(newValue !== null && newValue !== undefined ? newValue : '') + '</td>';
}); });
bodyHtml += '<td>' + (row.matched ? '✓' : '✗') + '</td></tr>'; bodyHtml += '<td>' + (row.matched ? '✓' : '✗') + '</td></tr>';
}); });
@ -290,21 +296,27 @@ function processFiles() {
const { templateData, fieldsToFill, mainMap, templateKeyField } = mappedResult; const { templateData, fieldsToFill, mainMap, templateKeyField } = mappedResult;
// Полная обработка всех данных // Полная обработка всех данных (без мутации!)
let totalFilled = 0; let totalFilled = 0;
const resultData = templateData.map(row => { const resultData = templateData.map(row => {
// Создаём КОПИЮ строки
const newRow = { ...row };
const keyValue = normalizeValue(row[templateKeyField]); const keyValue = normalizeValue(row[templateKeyField]);
const mainRow = mainMap.get(keyValue); const mainRow = mainMap.get(keyValue);
if (mainRow) { if (mainRow) {
fieldsToFill.forEach(field => { fieldsToFill.forEach(field => {
if (!row[field] && mainRow[field]) { // Проверяем на пустое значение (null, undefined, пустая строка)
row[field] = mainRow[field]; const isEmpty = newRow[field] === null || newRow[field] === undefined || String(newRow[field]).trim() === '';
const hasValueInMain = mainRow[field] !== null && mainRow[field] !== undefined;
if (isEmpty && hasValueInMain) {
newRow[field] = mainRow[field];
totalFilled++; totalFilled++;
} }
}); });
} }
return row; return newRow;
}); });
// Создаём Excel файл // Создаём Excel файл