работает

This commit is contained in:
2025-07-21 19:22:22 +03:00
parent 47e0c7f81c
commit 555505e09e
5 changed files with 146 additions and 30 deletions

View File

@@ -15,6 +15,7 @@ import {
} from '@/component/ui/select'
import { Textarea } from '@/component/ui/textarea'
import { useTemplateContext } from '@/entitiy/template/model/TemplateContext'
import { cellAddressToCoordinates } from '@/lib/cell-utils'
import { getElementDefinition } from '@/lib/element-registry'
import { getLatestFileForTemplate } from '@/service/fileApiService'
import { Template, TemplateElement } from '@/type/template'
@@ -381,7 +382,33 @@ const ProtocolForm: FC<ProtocolFormProps> = ({ template, onSave, onBack }) => {
if (engineRef.current) {
const element = template.elements.find(el => el.id === elementId)
if (element && element.targetCells) {
element.targetCells.forEach(tc => {
const cells = element.targetCells
cells.forEach((tc, idx) => {
let cellValue: any = value
// Специальная обработка для массивов эталонов
if (element.type === 'standards' && Array.isArray(value)) {
cellValue = getStandardById(value[idx] as string)?.name || ''
}
// Специальная обработка для условий калибровки
if (
element.type === 'calibration-conditions' &&
typeof value === 'object' &&
value !== null
) {
const propsOrder = [
'temperature',
'humidity',
'pressure',
'voltage',
'frequency',
'lastUpdated',
] as const
cellValue = (value as any)[propsOrder[idx]] ?? ''
}
const { row, col } = cellAddressToCoordinates(tc.cell)
const sheetName =
tc.sheet === 'R' || tc.sheet === 'Calculations' ? 'R' : 'L'
@@ -389,7 +416,7 @@ const ProtocolForm: FC<ProtocolFormProps> = ({ template, onSave, onBack }) => {
sheetName,
row,
col,
value
cellValue
)
})
engineRef.current.debouncedRecalc()
@@ -444,18 +471,78 @@ const ProtocolForm: FC<ProtocolFormProps> = ({ template, onSave, onBack }) => {
if (def?.mapToCells) cells = def.mapToCells(el as any)
}
cells?.forEach(tc => {
// === Новая логика распределения значений по ячейкам ===
if (!cells || cells.length === 0) return
// 1) Массив (например, standards) -> каждой ячейке своё значение
if (el.type === 'standards' && Array.isArray(value)) {
cells.forEach((tc, idx) => {
const cellValue = getStandardById(value[idx] as string)?.name || ''
const { row, col } = cellAddressToCoordinates(tc.cell)
if (tc.sheet === 'Calculations') alert('Лист Calculations')
const sheetName =
tc.sheet === 'R' || tc.sheet === 'Calculations' ? 'R' : 'L'
engineRef.current.setCellValueWithoutRecalc(
sheetName,
row,
col,
cellValue
)
})
return
}
// Обработка любых других массивов
if (Array.isArray(value)) {
cells.forEach((tc, idx) => {
const cellValue = value[idx] ?? ''
const { row, col } = cellAddressToCoordinates(tc.cell)
const sheetName =
tc.sheet === 'R' || tc.sheet === 'Calculations' ? 'R' : 'L'
engineRef.current.setCellValueWithoutRecalc(
sheetName,
row,
col,
cellValue
)
})
return
}
// 2) Объект условия калибровки -> пишем свойства по порядку
if (
el.type === 'calibration-conditions' &&
typeof value === 'object' &&
value !== null
) {
const propsOrder = [
'temperature',
'humidity',
'pressure',
'voltage',
'frequency',
'lastUpdated',
] as const
cells.forEach((tc, idx) => {
const cellValue = (value as any)[propsOrder[idx]] ?? ''
const { row, col } = cellAddressToCoordinates(tc.cell)
const sheetName =
tc.sheet === 'R' || tc.sheet === 'Calculations' ? 'R' : 'L'
engineRef.current.setCellValueWithoutRecalc(
sheetName,
row,
col,
cellValue
)
})
return
}
// 3) Примитив или прочие объекты -> одно значение во все ячейки
cells.forEach(tc => {
const { row, col } = cellAddressToCoordinates(tc.cell)
const sheetName =
tc.sheet === 'R' || tc.sheet === 'Calculations' ? 'R' : 'L'
console.log(
'[DBG] write to',
tc.cell,
'sheet',
sheetName,
'value',
value
)
engineRef.current.setCellValueWithoutRecalc(
sheetName,
row,
@@ -546,9 +633,10 @@ const ProtocolForm: FC<ProtocolFormProps> = ({ template, onSave, onBack }) => {
const protocolNameElement = template.elements.find(
el => el.name === 'protocolName' || el.label === 'Название протокола'
)
const canSave = protocolNameElement
? !!formData[protocolNameElement.id]?.trim()
: false
// const canSave = protocolNameElement
// ? !!formData[protocolNameElement.id]?.trim()
// : false
const canSave = true
return (
<div className="flex h-screen flex-col bg-gray-50">
@@ -591,9 +679,7 @@ const ProtocolForm: FC<ProtocolFormProps> = ({ template, onSave, onBack }) => {
{/* Всегда монтируем DualSpreadsheet, скрываем/показываем через hidden */}
<div className={`${showSpreadsheet ? 'block' : 'hidden'} h-full`}>
<DualSpreadsheet
templateData={
template.excelData ? { report: template.excelData } : {}
}
templateData={template.excelData ? { L: template.excelData } : {}}
mergedCells={template.mergedCells || []}
templateId={template.id}
onEngineReady={engine => (engineRef.current = engine)}