Creating new Excel formulas/functions with C# - c#

We are looking to be able to programmatically create an Excel workbook which would call custom code from within a cell. Cells would look something like:
=MyCode(A1:A10)
My first thought was to use VBA, but since the algorithm is proprietary the powers that be want it to be protected. I can put a password on it, but it is well documented (here on StackOverflow) on how to get around such passwords.
My second thought was to create an Excel 2013 Workbook project in Visual Studio, but I haven't found anything useful on how to expose a function in C# so it can be called like I described.
Next I thought about having VBA call the C#, and found instructions at https://msdn.microsoft.com/en-us/library/bb608613.aspx. I followed those instructions to the letter, but when I try to run the VBA code I get an error with the GetManagedClass function: Object Library Feature not Supported.
Are there any good references on how to do something like this?

You're looking for Excel-DNA.
This open-source library allows you to create managed Excel add-ins, and supports making user-defined functions, but also macros, real-time RTD data sources etc.
Creating an Excel UDF in C# is then as simple as:
[ExcelFunction(Description = "My first .NET function")]
public static string SayHello(string name)
{
return "Hello " + name;
}
and you can call from a cell as:
=SayHello("Walter")
For code protection with .NET, you'd need to use an obfuscator - there are a variety of free and paid-for ones available.

I have also tried this sample, with the same error. I found a solution that worked for me.
In the ISheet1.cs file, replace the ISheet1 interface declaration with the following code. This code makes the ISheet1 interface public, and it applies the http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.comvisibleattribute.aspx attribute to make the interface visible to COM.
C#
[System.Runtime.InteropServices.ComVisible(true)]
public interface ISheet1
{
void CreateVstoNamedRange(Microsoft.Office.Interop.Excel.Range range, string name);
}
Full article her: http://www.nullskull.com/q/10059408/c-code-to-set-excel-workbook-macro-enabled-and-to-trust-vba-projects.aspx

Related

A better alias list?

We have a system that manages generic physical resources. There are over 500 individual resources. The system is used for many different things and to make the software easier to write we use aliases.
For example, a physical resource TG67I9 is given an alias of "RightDoor". When code is written RightDoor is used instead of TG67I9 making the code more readable. This alias list is loaded as a text file with references to resources and their aliases. This system uses literally hundreds of different alias lists to reference the same physical resources.
This type of setup has two major shortcomings. First, when resources are called using their aliases, they are passed in as strings. Door.Open("RightDoor") for example. This does not give any tooltips or smart anything making the code more difficult to write. It basically requires constantly referencing the alias list. Is it RightDoor or Right_Door or right-door or... you get the idea. The second is that there is no validation of parameters until execution. All the compiler knows is that a string is passed in and then it's happy. Only when the code is run, the function tries to access the resource through its alias and fails because it can't find right-door because it's supposed to be RightDoor. An error is displayed. This requires tedious debugging and running the code over and over to weed out any bad aliases.
Is there a better way to do this? Such that an alias list can be made with a cross-reference of physical resources to their alias names and after the list is made that tooltips could appear suggesting resources. (Assume that a new system could be written from scratch)
I'm using the latest .NET with VisualStudio 2017 and C# to write the code.
The simplest approach is most likely a "string enum":
public class Resources {
public const string
LeftDoor = "TG67I8",
RightDoor = "TG67I9";
}
Sample use:
Door.Open(Resources.RightDoor);
Hovering over .RightDoor in VS shows a tooltip (constant) string Resources.RightDoor = "TG67I9"
Right-clicking .RightDoor and selecting Find All References will show where the variable is used.
Another option can be adding the strings in the Resources section of the Project Properties, and then:
using YourProjectNameSpace.Properties;
...
Door.Open(Resources.RightDoor);
That is a bit slower, because the resource(s) are retrieved at run-time, but allows to load the resources from a custom external file separate from the executable.
Use a static class with constants. I have done the same many times and still do. Plus .NET does this as well.
public static class PhysicalResources
{
public const string One = "Uno";
public const string Two = "Deux";
// ...
}

Call custom function from excel in C# Program

I have an excel addin (*.xla) that contains many complex functions that all take input parameters and return values. I unfortunately cannot create a *.XLL as I don't have access to the *.xla file.
I've managed to use C# to write the excel function into a cell (as a string), calculate the cell, and then retrieve the result back into C#. This method works but it feels very ad hoc and inefficient.
Given the situation described above, is there a better way for calling a custom excel function (that takes parameters) from C#?
How about using the excellent Excel-DNA to build a C# XLL that uses xlfEvaluate to invoke the UDF defined in the .xla? MSDN doc for xlfEvaluate: https://msdn.microsoft.com/en-us/library/office/bb687913.aspx. You can pass in any string that can appear in a worksheet cell. So it shouldn't be too difficult to code some C# to compose the name of the VBA function and some parameters into a string and then pass them to xlfEvaluate.

Parsing C# code for contextually aware semantic highlighting

I'm working on a semantic highlighting plugin for VS. Here you can see a web Example.
The goal:
Acquiring all variables and creating different Classifications for every one of them.
The problem:
Getting the variables from the code without writing a C# lexer.
My current approach uses an ITagger. I use an ITagAggregator to get the tags of all the spans that get passed to the ITagger. Then I filter those and get only spans with the "identifier" classification which includes varibles, methods names, class names, usings and properties.
public class Classifier : ITagger<ClassificationTag> {
public IEnumerable<ITagSpan<ClassificationTag>> GetTags(NormalizedSnapshotSpanCollection spans) {
ITextSnapshot snapshot = spans[0].Snapshot;
var tags = _aggregator.GetTags(spans).Where((span) => span.Tag.ClassificationType.Classification.Equals("identifier")).ToArray();
foreach(var classifiedSpan in tags) {
foreach(SnapshotSpan span in classifiedSpan.Span.GetSpans(snapshot)) {
//generate classification based on variable name
yield return new TagSpan<ClassificationTag>(span, new ClassificationTag(_classification));
}
}
}
}
It would be a lot easier to use the builtin C# Lexer to get a list of all variables bundled to a bunch of meta data. Is this data available for plugin development? Is there an alternative way I could acquire it, if not?
The problem: Getting the variables from the code without writing a C# lexer.
Roslyn can do this: https://roslyn.codeplex.com/
There's even a Syntax Visualizer sample that might interest you. I also found an example using Roslyn to create a Syntax Highlighter.
Visual Studio exposes that information as a code model.
Here is an example how you can access class, and then find attribute on the class, and parse attribute arguments:
Accessing attribute info from DTE
Here is more information about code models:
http://msdn.microsoft.com/en-us/library/ms228763.aspx
Here's also automation object model chart what I've been using quite few times: http://msdn.microsoft.com/en-us/library/za2b25t3.aspx
Also, as said, Roslyn is indeed also a possible option. Here is an example for VS2015 using roslyn: https://github.com/tomasr/roslyn-colorizer/blob/master/RoslynColorizer/RoslynColorizer.cs
For building language tools if may be better to use a parser generator for C#. The GOLD parsing system is one such toolkit which can handle LALR grammars. It has a .NET component based engine that you can use in your project and it can be used to integrate with any IDE. You can also find the grammars for various programming languages including C#.

How do I make my own method similar to String.Format using Composite Formatting in C#

I like how String.Format uses arguments to inject variables in to the string it is formatting. This is called Composite Formating and is discussed by MSDN here.
I want this functionality with my logging facade:
string foo = "fancy";
string bar = "message";
log.Debug("My {0} log {1}.", foo, bar)
My ILoggerFacade has the following method signature:
void Debug<T>(T message, params Object[] args);
And, I know I can implement this quite simply:
ILog m_Log = \\some logging implementation
public void Debug<T>(T message, params Object[] args)
{
m_Log.Debug(String.Format(message, args));
}
However, in Visual Studio I don't get the fancy highlighting of the {0}, {1}, ... arguments:
I guess it is ReSharper who is resposible for them, and it seems like it is just ignoring the formatting arguments and giving no "intellisense" help. This isn't good since the other developers who will be using the facade will be expecting this.
How do I get argument highlighting and "intellisense" for custom formatted methods similar to how these work:
Console.WriteLine(...)
String.Format(...)
etc...
Any help would be appreciated.
Check out ReSharpers External Annotations. Specifically, you want to use StringFormatMethodAttribute for this.
To use the External Annotations there are actually 3 methods. Two that it spells out, and one that you have to read between the lines to see.
Reference "JetBrains.Annotations.dll". I would recommend against this one. I don't like the idea of copying the DLL, or having to reference the ReSharper install directory. This could cause issues if you upgrade or re-install.
Copying and pasting attribute declarations into your solution. I'd recommend this as it gives you more control. Additionally, you can get rid of ReSharper (why would anyone do this? Stranger things have happened, I guess.), and still provide this feature to anyone that consumes your library. There are step by step instructions on how to do this in the first link.
Create an XML file, similar to what it uses for for the .NET Assemblies. I did this for the Silverlight Unit Test Framework. ReSharper does not recognize these tests by default.
To do this
Create a file name <assembly>.xml and put it in "ReSharper\vXX\Bin\ExternalAnnotations".
Add a root element "<assembly name="<assembly>">
Now add <member> elements for each member that you want to give an attribute.
I do not recommend doing this for your own code. However, if you have an assembly that you want to have this functionality, but cannot edit, this is the way to do it. This will only apply on your machine and each developer that uses the assembly will need to copy the xml file.

C# code generation

i'm about to make a graduation project application
this application is gonna some way receive a description for a situation , and then accordingly generate c# code
i want to know in what field i need to search or how to autogenerate C# code
Have a look at Kathleen Dollard's book on this if you can. She has a website for this topic as well.
You have three options essentially:
Brute-force - creating the code files yourself in a text file
CodeDOM generation - MS's built in way of creating code.
XSLT - What Kathleen uses.
T4 templates can help too -
http://www.hanselman.com/blog/T4TextTemplateTransformationToolkitCodeGenerationBestKeptVisualStudioSecret.aspx
And you could also generate IL on the fly. ;)
CodeDOM
I've done a wrapper around codedom. You only need to create your own C# script and specify the types being used.
Example
public interface IWorld
{
string Hello(string value);
}
string code = #"namespace MyNamespace
{
class Temp : IWorld
{
public string Hello(string value)
{
return "World " + value;
}
}
}";
Compiler compiler = new Compiler();
compiler.AddType(typeof(string));
compiler.Compile(code);
var obj = compiler.CreateInstance<IWorld>();
string result = obj.Hello("World!");
Note that it was a long time ago that I wrote it. The example might not work 100%. (The Compiler class do work, the example might use it incorrectly).
Compiler source code: http://fadd.codeplex.com/SourceControl/changeset/view/65227#925984
Reflection.Emit
You can also generate IL using Reflection.Emit: http://msdn.microsoft.com/en-us/library/3y322t50.aspx
It's a bit harder but more flexible, since CodeDOM generates a new Assembly each type you compile code.
There is a set of MatLab tools that generates C/C++ code from state-charts and data-flow diagrams:
Real Time Workshop
Real-Time Workshop Embedded Coder
Stateflow Coder
You should dig into it.
What will be the "description of a solution" in your case?

Categories

Resources