1
0

Add custom panel and tools

This commit is contained in:
Sergey Chebotar 2023-06-14 15:23:33 +03:00
parent c29849b19d
commit 7905d3af4d
7 changed files with 131 additions and 0 deletions

View File

@ -0,0 +1,30 @@
using ExcelDna.Integration.CustomUI;
using ExcelAddIn.Tools;
namespace ExcelAddIn.Controllers;
public class RibbonController : ExcelRibbon
{
public override string GetCustomUI(string ribbonID)
{
return @"<customUI xmlns='http://schemas.microsoft.com/office/2006/01/customui'>
<ribbon>
<tabs>
<tab id='MyAddinTab' label='My Addin Tab'>
<group id='MyAddinGroup' label='My Addin Group'>
<button id='Button1' label='Button 1' size='large' imageMso='HappyFace' onAction='OnToolPressed'/>
<button id='Button2' label='Button 2' size='large' imageMso='SadFace' onAction='OnToolPressed'/>
<button id='Button3' label='Button 3' size='large' imageMso='Piggy' onAction='OnToolPressed'/>
</group>
</tab>
</tabs>
</ribbon>
</customUI>";
}
public void OnToolPressed(IRibbonControl control)
{
using var tool = ToolFactory.GetTool(control);
tool.Execute();
}
}

View File

@ -3,11 +3,13 @@
<PropertyGroup>
<TargetFramework>net6.0-windows</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<UseWindowsForms>true</UseWindowsForms>
<Nullable>disable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="ExcelDna.AddIn" Version="1.6.0" />
<PackageReference Include="ExcelDna.Interop" Version="15.0.1" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Http" Version="7.0.0" />
</ItemGroup>

14
Tools/Button1Tool.cs Normal file
View File

@ -0,0 +1,14 @@
namespace ExcelAddIn.Tools;
public class Button1Tool : Tool
{
public override void Execute()
{
MessageBox.Show($"Message from {nameof(Button1Tool)}");
}
protected override void Dispose(bool disposing)
{
}
}

30
Tools/Button2Tool.cs Normal file
View File

@ -0,0 +1,30 @@
using Application = Microsoft.Office.Interop.Excel.Application;
namespace ExcelAddIn.Tools;
public class Button2Tool : Tool
{
private readonly Application app;
public Button2Tool()
{
app = (Application)ExcelDnaUtil.Application;
}
public override void Execute()
{
if (app.ActiveCell == null)
{
return;
}
double? cellValue = app.ActiveCell.Cells.Value2;
if (cellValue != null)
{
app.ActiveCell.Cells.Value2 = ++cellValue;
}
}
protected override void Dispose(bool disposing)
{
}
}

24
Tools/Button3Tool.cs Normal file
View File

@ -0,0 +1,24 @@
using Application = Microsoft.Office.Interop.Excel.Application;
namespace ExcelAddIn.Tools;
public class Button3Tool : Tool
{
private readonly Application app;
public Button3Tool()
{
app = (Application)ExcelDnaUtil.Application;
}
public override void Execute()
{
for (int i = 0; i < 10;)
{
Thread.Sleep(400);
app.StatusBar = $"Выполнено {++i * 10}%...";
}
}
protected override void Dispose(bool disposing)
{
app.StatusBar = false;
}
}

14
Tools/Tool.cs Normal file
View File

@ -0,0 +1,14 @@
namespace ExcelAddIn.Tools;
public abstract class Tool : IDisposable
{
public abstract void Execute();
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected abstract void Dispose(bool disposing);
}

17
Tools/ToolFactory.cs Normal file
View File

@ -0,0 +1,17 @@
using ExcelDna.Integration.CustomUI;
namespace ExcelAddIn.Tools;
public static class ToolFactory
{
public static Tool GetTool(IRibbonControl control)
{
return control.Id switch
{
"Button1" => new Button1Tool(),
"Button2" => new Button2Tool(),
"Button3" => new Button3Tool(),
_ => throw new NotImplementedException(control.Id)
};
}
}