Text() debug
All checks were successful
Publish NuGet packages / publish (push) Successful in 25s

This commit is contained in:
melekhin
2026-06-16 15:52:08 +07:00
parent 804fc84563
commit eccb12b83c
2 changed files with 28 additions and 6 deletions

View File

@@ -426,15 +426,34 @@ internal sealed class ExcelCell : ICell
}
/// <inheritdoc />
public ICellText? Text()
public ICellText Text()
{
_writer.ThrowIfDisposed();
lock (_writer._syncLock)
{
var cell = GetCellElement();
if (cell == null || cell.DataType?.Value != CellValues.InlineString || cell.InlineString == null)
return null;
// 1. Получаем или создаём элемент ячейки
var cell = GetOrCreateCellElement();
// 2. Убедимся, что у ячейки правильный тип и есть InlineString
if (cell.DataType == null || cell.DataType.Value != CellValues.InlineString)
{
// Если был другой тип (например, число) — меняем на InlineString
cell.DataType = CellValues.InlineString;
// Удаляем старый InlineString, если был
cell.InlineString?.Remove();
// Создаём новый пустой InlineString
cell.InlineString = new InlineString();
// Добавляем пустой Text (обязательно для Open XML)
cell.InlineString.AppendChild(new Text());
}
else if (cell.InlineString == null)
{
// Если тип InlineString, но сам элемент отсутствует
cell.InlineString = new InlineString();
cell.InlineString.AppendChild(new Text());
}
// 3. Строим объект ICellText на основе содержимого InlineString
var textObj = new ExcelCellText();
foreach (var run in cell.InlineString.Elements<Run>())
{
@@ -442,11 +461,14 @@ internal sealed class ExcelCell : ICell
RunFormat? format = null;
if (run.RunProperties != null)
{
// Преобразуем RunProperties в RunFormat (можно вынести в отдельный метод)
format = RunFormat.FromRunProperties(run.RunProperties);
}
textObj.Run(text, format);
}
// Если не было ни одного Run, текст всё равно должен быть доступен (пустой)
// Можно оставить textObj пустым, а можно сразу добавить пустой Run
// (но это не обязательно пользователь может добавить run позже)
return textObj;
}
}