Files
radzen-blazor/Radzen.Blazor/Spreadsheet/HyperlinkCommand.cs
2026-06-15 18:45:06 +03:00

52 lines
1.4 KiB
C#

using Radzen.Documents.Spreadsheet;
namespace Radzen.Blazor.Spreadsheet;
#nullable enable
/// <summary>
/// Command to set or remove a hyperlink on a cell.
/// </summary>
public class HyperlinkCommand(Worksheet sheet, CellRef address, Hyperlink? hyperlink) : ICommand
{
private readonly Worksheet sheet = sheet;
private readonly CellRef address = address;
private readonly Hyperlink? hyperlink = hyperlink;
private Hyperlink? previousHyperlink;
private string? previousDisplayText;
/// <inheritdoc/>
public bool Execute()
{
var cell = sheet.Cells[address.Row, address.Column];
previousHyperlink = cell.Hyperlink?.Clone();
previousDisplayText = cell.Value?.ToString();
cell.Hyperlink = hyperlink?.Clone();
if (hyperlink is not null && !string.IsNullOrEmpty(hyperlink.Text))
{
cell.Value = hyperlink.Text;
}
else if (hyperlink is not null && (cell.Value is null || string.IsNullOrEmpty(cell.Value.ToString())))
{
cell.Value = hyperlink.Url;
}
return true;
}
/// <inheritdoc/>
public void Unexecute()
{
if (sheet.Cells.TryGet(address.Row, address.Column, out var cell))
{
cell.Hyperlink = previousHyperlink?.Clone();
if (previousDisplayText is not null)
{
cell.Value = previousDisplayText;
}
}
}
}