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

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