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

127 lines
4.6 KiB
C#
Raw Normal View History

using ExcelDna.Integration.CustomUI;
using System.Reflection;
using System.Runtime.InteropServices;
2023-04-06 08:29:39 +03:00
#if! NET472
using System.Runtime.Versioning;
#endif
using System.Windows.Forms;
2021-12-05 16:55:36 +03:00
2023-03-28 10:36:36 +03:00
namespace RhSolutions.Controllers;
2023-04-06 08:29:39 +03:00
#if !NET472
[SupportedOSPlatform("windows")]
#endif
2023-03-28 10:36:36 +03:00
[ComVisible(true)]
public class RibbonController : ExcelRibbon
2021-12-05 16:55:36 +03:00
{
2023-03-28 10:36:36 +03:00
private static IRibbonUI ribbonUi;
2023-05-27 08:50:30 +03:00
private static bool _workbookIsValid;
2023-03-28 10:36: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'/>
2023-04-06 08:29:39 +03:00
<button id='merge' label='Объединить' size='normal' imageMso='Copy' onAction='OnToolPressed'/>
2023-06-20 09:50:03 +03:00
<button id='guess' getEnabled='GetGuessEnabled' label='Найти и экспортировать' size='normal' imageMso='ControlWizards' onAction='OnToolPressed'/>
2023-06-21 06:39:15 +03:00
<button id='fillsleeves' getEnabled='GetSleevesEnabled' label='Подобрать гильзы' size='normal' imageMso='CreateQueryFromWizard' onAction='OnToolPressed'/>
2023-04-07 08:10:51 +03:00
<button id='dxfexport' getEnabled='GetDxfEnabled' label='Экспортировать в DXF' size='normal' imageMso='ExportExcel' onAction='OnToolPressed'/>
</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>";
2023-03-28 10:36:36 +03:00
}
2023-03-28 10:36:36 +03:00
public void RibbonLoad(IRibbonUI sender)
{
ribbonUi = sender;
}
2023-03-28 10:36:36 +03:00
public static void RefreshControl(string id)
{
2023-04-06 08:29:39 +03:00
ribbonUi?.InvalidateControl(id);
2023-03-28 10:36:36 +03:00
}
2023-03-28 07:25:10 +03:00
2023-03-28 10:36:36 +03:00
public void OnSetPricePressed(IRibbonControl control)
{
2023-04-06 08:29:39 +03:00
IFileDialog dialog = RhSolutionsAddIn.ServiceProvider.GetService<IFileDialog>();
string file = dialog.GetFile();
2023-04-06 08:29:39 +03:00
if (!string.IsNullOrEmpty(file))
{
2023-04-06 08:29:39 +03:00
RhSolutionsAddIn.Configuration.SetPriceListPath(file);
2023-03-28 10:36:36 +03:00
RhSolutionsAddIn.Configuration.SaveSettings();
2021-12-24 16:22:03 +03:00
}
2023-03-28 10:36:36 +03:00
}
2021-12-24 16:22:03 +03:00
2023-03-28 10:36:36 +03:00
public void OnToolPressed(IRibbonControl control)
{
try
{
2023-04-14 08:08:46 +03:00
var toolFactory = RhSolutionsAddIn.ServiceProvider.GetService<ToolFactory>();
using Tool tool = toolFactory.GetTool(control.Id);
2023-04-06 08:29:39 +03:00
tool.Execute();
}
2023-03-28 10:36:36 +03:00
catch (Exception exception)
{
2023-03-28 10:36:36 +03:00
MessageBox.Show(exception.Message,
"Ошибка",
MessageBoxButtons.OK,
MessageBoxIcon.Information);
RhSolutionsAddIn.Excel.StatusBar = false;
return;
}
2023-03-28 10:36:36 +03:00
}
2023-05-27 08:50:30 +03:00
public bool GetConvertEnabled(IRibbonControl control) => _workbookIsValid;
public bool GetDxfEnabled(IRibbonControl control) => _workbookIsValid;
2023-06-21 06:39:15 +03:00
public bool GetSleevesEnabled(IRibbonControl control) => _workbookIsValid;
2023-05-27 08:58:23 +03:00
public bool GetGuessEnabled(IRibbonControl control) => RhSolutionsAddIn.Excel.ActiveWorkbook != null && !_workbookIsValid;
2023-03-28 10:36:36 +03:00
public bool GetExportEnabled(IRibbonControl control)
{
if (RhSolutionsAddIn.Excel.ActiveWorkbook == null)
return false;
2023-03-28 10:36:36 +03:00
else
{
2023-03-28 10:36:36 +03:00
Range selection = RhSolutionsAddIn.Excel.Selection;
return selection.Columns.Count == 2;
}
2021-12-05 16:55:36 +03:00
}
2023-03-28 10:36:36 +03:00
2023-05-27 08:50:30 +03:00
public string GetVersionLabel(IRibbonControl control)
2023-04-07 08:10:51 +03:00
{
2023-05-27 08:50:30 +03:00
string version = Assembly.GetExecutingAssembly().GetName().Version.ToString();
return $"v{version}";
}
2023-04-07 08:10:51 +03:00
2023-05-27 08:50:30 +03:00
public string GetPriceListPathLabel(IRibbonControl control)
{
string name = RhSolutionsAddIn.Configuration.GetPriceListFileName();
return string.IsNullOrEmpty(name) ? "Шаблонный файл" : name;
2023-04-07 08:10:51 +03:00
}
2023-05-27 08:50:30 +03:00
public static void UpdateWorkbookValidation()
{
if (RhSolutionsAddIn.Excel.ActiveWorkbook == null)
2023-05-27 08:50:30 +03:00
_workbookIsValid = false;
else
{
Worksheet worksheet = RhSolutionsAddIn.Excel.ActiveWorkbook.ActiveSheet;
_workbookIsValid = worksheet.IsValidSource();
}
}
2021-12-05 16:55:36 +03:00
}