RhSolutions-AddIn/RhSolutions.AddIn/Controllers/RibbonController.cs
2023-03-28 09:58:43 +03:00

133 lines
4.6 KiB
C#

using ExcelDna.Integration.CustomUI;
using Microsoft.Office.Interop.Excel;
using RhSolutions.AddIn;
using RhSolutions.Services;
using System;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using Range = Microsoft.Office.Interop.Excel.Range;
namespace RhSolutions.Controllers
{
[ComVisible(true)]
public class RibbonController : ExcelRibbon
{
private static IRibbonUI ribbonUi;
public override string GetCustomUI(string RibbonID)
{
return @"
<customUI onLoad='RibbonLoad' xmlns='http://schemas.microsoft.com/office/2006/01/customui'>
<ribbon>
<tabs>
<tab id='rau' label='RhSolutions'>
<group id='priceList' label='Прайс-лист'>
<button id='export' getEnabled='GetExportEnabled' label='Экспорт в новый файл' size='normal' imageMso='PivotExportToExcel' onAction='OnToolPressed'/>
<button id='convert' getEnabled='GetConvertEnabled' label='Актуализировать' size='normal' imageMso='FileUpdate' onAction='OnToolPressed'/>
<menu id='conjoinMenu' label='Объединить' imageMso='Copy'>
<button id='merge' label='Сложить' onAction='OnToolPressed'/>
<button id='combine' label='По колонкам' onAction='OnToolPressed'/>
</menu>
</group>
<group id='rausettings' getLabel='GetVersionLabel'>
<button id='setPriceList' getLabel='GetPriceListPathLabel' size='large' imageMso='TableExcelSpreadsheetInsert' onAction='OnSetPricePressed'/>
</group>
</tab>
</tabs>
</ribbon>
</customUI>";
}
public void RibbonLoad(IRibbonUI sender)
{
ribbonUi = sender;
}
public static void RefreshControl(string id)
{
if (ribbonUi != null)
{
ribbonUi.InvalidateControl(id);
}
}
public void OnSetPricePressed(IRibbonControl control)
{
var dialog = RhSolutionsAddIn.Excel
.FileDialog[Microsoft.Office.Core.MsoFileDialogType.msoFileDialogFilePicker];
dialog.AllowMultiSelect = false;
dialog.Filters.Add("Файлы Excel", "*.xls; *.xlsx; *.xlsm");
if (dialog.Show() < 0)
{
RhSolutionsAddIn.Configuration.SetPriceListPath(dialog.SelectedItems.Item(1));
RhSolutionsAddIn.Configuration.SaveSettings();
}
}
public void OnToolPressed(IRibbonControl control)
{
try
{
ToolBase tool = control.Id switch
{
"export" => new ExportTool(),
"convert" => new ConvertTool(),
"merge" => new MergeTool(),
"combine" => new CombineTool(),
_ => throw new Exception("Неизвестный инструмент"),
};
tool.OpenNewPrice();
tool.FillTarget();
}
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.IsRehauSource();
}
}
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;
}
}
}