diff --git a/excel-editor.js b/excel-editor.js index 9bc2d2a..ce47050 100644 --- a/excel-editor.js +++ b/excel-editor.js @@ -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 += ''; 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 += '' + escapeHtml(value) + ''; + 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 += '' + escapeHtml(newValue !== null && newValue !== undefined ? newValue : '') + ''; }); bodyHtml += '' + (row.matched ? '✓' : '✗') + ''; }); @@ -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 файл