89 lines
2.8 KiB
C#
89 lines
2.8 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using System.Reflection;
|
|
|
|
using System.IO;
|
|
using System.Security.Policy;
|
|
using System.Diagnostics;
|
|
|
|
using ExcelDna.Integration;
|
|
using ExcelDna.IntelliSense;
|
|
using ExcelDna.Logging;
|
|
|
|
namespace bts.excel
|
|
{
|
|
public class TestMethod
|
|
{
|
|
[ExcelFunction]
|
|
static public string bts_excel_test_string(string x) { return "Hello " + x; }
|
|
[ExcelFunction]
|
|
static public object bts_excel_test_int(int x) { return x; }
|
|
[ExcelFunction]
|
|
static public object bts_excel_test_long(long x) { return x; }
|
|
}
|
|
|
|
[System.Runtime.InteropServices.ComVisible(true)]
|
|
public class ContextTeardown : ExcelComAddIn
|
|
{
|
|
private api.core.ServiceContext _context;
|
|
public ContextTeardown(api.core.ServiceContext context)
|
|
{
|
|
_context = context;
|
|
}
|
|
//This is called on application shutdown
|
|
public override void OnBeginShutdown(ref Array custom)
|
|
{
|
|
_context.Dispose();
|
|
_context = null;
|
|
}
|
|
}
|
|
|
|
[System.Runtime.InteropServices.ComVisible(true)]
|
|
public class ExcelAddin : IExcelAddIn
|
|
{
|
|
public void AutoClose()
|
|
{
|
|
//don't do anything here, it's only called when the addin is removed, not on shutdown
|
|
IntelliSenseServer.Uninstall();
|
|
}
|
|
public void AutoOpen() //This is called on EXCEL startup, when the addin is loaded
|
|
{
|
|
Trace.Listeners.Add(new LogDisplayTraceListener("bts"));
|
|
IntelliSenseServer.Install();
|
|
ExcelIntegration.RegisterUnhandledExceptionHandler(ex => ex.ToString());
|
|
|
|
load_assemblies();
|
|
ExcelComAddInHelper.LoadComAddIn(new ContextTeardown(APIDispatch.service_context.Value));
|
|
}
|
|
|
|
private void load_assemblies()
|
|
{
|
|
var config = System.Configuration.ConfigurationManager.AppSettings["load_assemblies"];
|
|
if (config != null)
|
|
{
|
|
var asses = config.Split(new char[] { ';', ',', ' ' }, StringSplitOptions.RemoveEmptyEntries);
|
|
foreach (var ass in asses)
|
|
Utilities.bts_load_assembly(ass);
|
|
}
|
|
}
|
|
|
|
public static void calculate_full_rebuild()
|
|
{
|
|
object app = ExcelDnaUtil.Application;
|
|
var mi = app?.GetType().GetMethod("CalculateFullRebuild", BindingFlags.Instance | BindingFlags.Public);
|
|
if(mi != null)
|
|
mi.Invoke(app, null);
|
|
}
|
|
|
|
public static void calculate_full()
|
|
{
|
|
object app = ExcelDnaUtil.Application;
|
|
var mi = app?.GetType().GetMethod("CalculateFull", BindingFlags.Instance | BindingFlags.Public);
|
|
if (mi != null)
|
|
mi.Invoke(app, null);
|
|
}
|
|
}
|
|
} |