using ExcelDna.Integration.CustomUI;
using System.Reflection;
using System.Runtime.InteropServices;
#if! NET472
using System.Runtime.Versioning;
#endif
using System.Windows.Forms;
namespace RhSolutions.Controllers;
#if !NET472
[SupportedOSPlatform("windows")]
#endif
[ComVisible(true)]
public class RibbonController : ExcelRibbon
{
private static IRibbonUI ribbonUi;
public override string GetCustomUI(string RibbonID)
{
return @"
";
}
public void RibbonLoad(IRibbonUI sender)
{
ribbonUi = sender;
}
public static void RefreshControl(string id)
{
ribbonUi?.InvalidateControl(id);
}
public void OnSetPricePressed(IRibbonControl control)
{
IFileDialog dialog = RhSolutionsAddIn.ServiceProvider.GetService();
string file = dialog.GetFile();
if (!string.IsNullOrEmpty(file))
{
RhSolutionsAddIn.Configuration.SetPriceListPath(file);
RhSolutionsAddIn.Configuration.SaveSettings();
}
}
public void OnToolPressed(IRibbonControl control)
{
try
{
ToolBase tool = control.Id switch
{
"export" => new ExportTool(),
"convert" => new ConvertTool(),
"merge" => new MergeTool(),
_ => throw new Exception("Неизвестный инструмент"),
};
tool.Execute();
}
catch (Exception exception)
{
MessageBox.Show(exception.Message,
"Ошибка",
MessageBoxButtons.OK,
MessageBoxIcon.Information);
RhSolutionsAddIn.Excel.StatusBar = false;
return;
}
}
public bool GetConvertEnabled(IRibbonControl control)
{
if (RhSolutionsAddIn.Excel.ActiveWorkbook == null)
return false;
else
{
Worksheet worksheet = RhSolutionsAddIn.Excel.ActiveWorkbook.ActiveSheet;
return worksheet.IsValidSource();
}
}
public bool GetExportEnabled(IRibbonControl control)
{
if (RhSolutionsAddIn.Excel.ActiveWorkbook == null)
return false;
else
{
Range selection = RhSolutionsAddIn.Excel.Selection;
return selection.Columns.Count == 2;
}
}
public string GetVersionLabel(IRibbonControl control)
{
string version = Assembly.GetExecutingAssembly().GetName().Version.ToString();
return $"v{version}";
}
public string GetPriceListPathLabel(IRibbonControl control)
{
string name = RhSolutionsAddIn.Configuration.GetPriceListFileName();
return string.IsNullOrEmpty(name) ? "Шаблонный файл" : name;
}
}