1 Commits

Author SHA1 Message Date
melekhin
eccb12b83c Text() debug
All checks were successful
Publish NuGet packages / publish (push) Successful in 25s
2026-06-16 15:52:08 +07:00
2 changed files with 28 additions and 6 deletions

View File

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

View File

@@ -706,7 +706,7 @@ public interface ICell
bool TryText(out ICellText cellText); bool TryText(out ICellText cellText);
/// <summary>Возвращает объект для редактирования богатого текста ячейки (если ячейка содержит InlineString).</summary> /// <summary>Возвращает объект для редактирования богатого текста ячейки (если ячейка содержит InlineString).</summary>
ICellText? Text(); ICellText Text();
/// <summary>Устанавливает простое текстовое значение (без форматирования).</summary> /// <summary>Устанавливает простое текстовое значение (без форматирования).</summary>
ICell Set(string value); ICell Set(string value);