RhSolutions-AddIn/RhSolutions.AddIn/Controllers/RibbonController.cs

140 lines
4.6 KiB
C#
Raw Normal View History

using ExcelDna.Integration.CustomUI;
using Microsoft.Office.Interop.Excel;
2022-12-20 12:41:46 +03:00
using RhSolutions.AddIn;
2022-12-20 12:27:47 +03:00
using RhSolutions.Services;
2022-01-09 10:37:25 +03:00
using System;
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Windows.Forms;
2023-02-08 15:59:30 +03:00
using Range = Microsoft.Office.Interop.Excel.Range;
2021-12-05 16:55:36 +03:00
2022-12-20 12:27:47 +03:00
namespace RhSolutions.Controllers
2021-12-05 16:55:36 +03:00
{
[ComVisible(true)]
public class RibbonController : ExcelRibbon
{
private static IRibbonUI ribbonUi;
2021-12-05 16:55:36 +03:00
public override string GetCustomUI(string RibbonID)
{
return @"
<customUI onLoad='RibbonLoad' xmlns='http://schemas.microsoft.com/office/2006/01/customui'>
2021-12-05 16:55:36 +03:00
<ribbon>
<tabs>
2022-12-19 08:44:32 +03:00
<tab id='rau' label='RhSolutions'>
<group id='priceList' label='Прайс-лист'>
2022-02-12 16:57:29 +03:00
<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'>
2022-02-12 16:57:29 +03:00
<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>
2021-12-05 16:55:36 +03:00
</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)
2021-12-24 16:22:03 +03:00
{
2022-12-20 12:27:47 +03:00
string path = Models.Dialog.GetFilePath();
2022-01-27 17:34:03 +03:00
if (!string.IsNullOrEmpty(path))
2021-12-24 16:22:03 +03:00
{
RegistryUtil.PriceListPath = path;
}
}
public void OnToolPressed(IRibbonControl control)
{
try
{
2022-12-20 11:53:55 +03:00
ToolBase tool;
switch (control.Id)
{
2022-02-12 16:57:29 +03:00
case "export":
tool = new ExportTool();
break;
2022-02-12 16:57:29 +03:00
case "convert":
tool = new ConvertTool();
break;
2022-02-12 16:57:29 +03:00
case "merge":
tool = new MergeTool();
break;
2022-02-12 16:57:29 +03:00
case "combine":
tool = new CombineTool();
break;
default:
throw new Exception("Неизвестный инструмент");
}
tool.OpenNewPrice();
tool.FillTarget();
}
2022-01-27 17:34:03 +03:00
catch (Exception exception)
{
MessageBox.Show(exception.Message,
"Ошибка",
MessageBoxButtons.OK,
MessageBoxIcon.Information);
2022-12-20 12:27:47 +03:00
RhSolutionsAddIn.Excel.StatusBar = false;
return;
2021-12-24 16:22:03 +03:00
}
}
public bool GetConvertEnabled(IRibbonControl control)
{
2022-12-20 12:27:47 +03:00
if (RhSolutionsAddIn.Excel.ActiveWorkbook == null)
return false;
else
{
2022-12-20 12:27:47 +03:00
Worksheet worksheet = RhSolutionsAddIn.Excel.ActiveWorkbook.ActiveSheet;
return worksheet.IsRehauSource();
}
}
public bool GetExportEnabled(IRibbonControl control)
{
2022-12-20 12:27:47 +03:00
if (RhSolutionsAddIn.Excel.ActiveWorkbook == null)
return false;
else
{
2022-12-20 12:27:47 +03:00
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 = RegistryUtil.GetPriceListName();
return string.IsNullOrEmpty(name) ? "Нет файла шаблона!" : name;
}
2021-12-05 16:55:36 +03:00
}
}