{"id":797,"date":"2013-11-22T17:04:58","date_gmt":"2013-11-22T17:04:58","guid":{"rendered":"http:\/\/putridparrot.com\/blog\/?p=797"},"modified":"2013-11-22T17:04:58","modified_gmt":"2013-11-22T17:04:58","slug":"using-managedxll-to-supply-functions-to-excel","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/using-managedxll-to-supply-functions-to-excel\/","title":{"rendered":"Using ManagedXLL to supply functions to Excel"},"content":{"rendered":"<p>In the last post I went through the steps for creating a class library for use with ManagedXLL to create an Excel add-in. We very briefly encountered the ManagedXLL way of marking methods for use in Excel. <\/p>\n<p>Now I&#8217;m going to delve a little deeper into what we can do with ManagedXLL. This will not be comprehensive, but will highlight some of the key features of ManagedXLL.<\/p>\n<p><strong>ExcelAddInEvent<\/strong><\/p>\n<p>We can mark a method with this attribute to carry out a task when the add-in is loaded (AddInEvent.Open), allowing us to carry out specific functionality, such as maybe getting an SSO token for the user or the likes. We can also use mark a method to be called when an add-in is being closed, i.e. when a workbook is closed, for example to allow us to save data to the cloud using AddInEvent.Close. We can also use AddInEvent.Add and AddInEvent.Remove to carry out a task when the add-in is added to the add-in manager or removed from it.<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n&#x5B;ExcelAddInEvent(AddInEvent.Open)]\r\npublic static void OnLoaded()\r\n{\r\n}\r\n<\/pre>\n<p>A couple of subclasses exist to handle Open and Close, named ExcelAutoOpen and ExcelAutoClose.<\/p>\n<p><strong>Excel Object<\/strong><\/p>\n<p>Before moving onto more attributes, let&#8217;s quickly look at the ManagedXLL Excel object. It includes methods to interact with Excel from our code, such as displaying a message box in Excel or getting at the sheets collection and so on, for example<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nExcel.MessageBox(&quot;You are not authorised to connect to the selected service&quot;, Excel.MessageBoxType.ErrorOK, &quot;MyExcelAddin&quot;);\r\n<\/pre>\n<p><strong>WorksheetFunction<\/strong><\/p>\n<p>The WorksheetFunction attribute is used to tell Excel, not only the name of the function (as seen by Excel users), but also how Excel should use the method in the sense of how it should display exceptions and whether to detect the user aborting of the method.<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n&#x5B;WorksheetFunction(&quot;CalculateFee&quot;, DetectUserAbort = true, Exceptions = Echo.Verbatim)]\r\npublic static double CalculateFee()\r\n{\r\n}\r\n<\/pre>\n<p><strong>ExcelDescription<\/strong><\/p>\n<p>We can mark methods, arguments and return values with the ExcelDescription attribute, allowing us to document what the various arguments and\/or return values of a method are as well as what the method itself does.<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n&#x5B;ExcelDescription(&quot;Gets a string based upon the supplied index&quot;)]\r\n&#x5B;return: ExcelDescription(&quot;Returns the selected string&quot;)]\r\npublic static string DoSomething(&#x5B;ExcelDescription(&quot;The Index&quot;)] int index)\r\n{\r\n}\r\n<\/pre>\n<p><strong>ExcelHidden<\/strong><\/p>\n<p>We can mark methods and hidden to hide them from the function wizard using the ExcelHidden attribute, this doesn&#8217;t stop us using the method, it just doesn&#8217;t appear in the function wizard<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n&#x5B;WorksheetFunction(&quot;MyName&quot;, DetectUserAbort = true, Exceptions = Echo.Verbatim)]\r\n&#x5B;ExcelDescription(&quot;Returns a string with the name of the add-in.&quot;)]\r\n&#x5B;return: ExcelDescription(&quot;A string representing the name of the add-in&quot;)]\r\n&#x5B;ExcelHidden]\r\npublic static string Name()\r\n{\r\n}\r\n<\/pre>\n<p><strong>ExcelCategory<\/strong><\/p>\n<p>The ExcelCategory is used on a class and allows us to create a category for groups of functions &#8211; this aids the function wizard in grouping the methods. You can create multiple classes and give them the same category or group together specific functionality into their own categories.<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n&#x5B;ExcelCategory(&quot;My Date &amp; Time Functions&quot;)]\r\npublic static class DateTimeFunctions\r\n{\r\n}\r\n<\/pre>\n<p><strong>ExcelAbbreviation<\/strong><\/p>\n<p>We can mark individual arguments of a method using this attribute. In essence we&#8217;re defining an alternate name for the parameter as viewed by Excel&#8217;s function wizard.<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\npublic static string Name(&#x5B;ExcelAbbreviation(&quot;Name&quot;)] String enterYourName)\r\n<\/pre>\n<p><strong>ExcelCommand<\/strong><\/p>\n<p>The ExcelCommand attribute allows us to define methods that are registered as Excel macro functions which can be run from the command&#8217;s listed in the Managed.XLL.config<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n&#x5B;ExcelCommand(&quot;.Run&quot;)]\r\npublic static void Run()\r\n{\r\n}\r\n<\/pre>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;command title=&quot;My Run Command&quot; macroName=&quot;.Run&quot;\r\n    description=&quot;Runs a command&quot; edition=&quot;Professional&quot;\/&gt;\r\n<\/pre>\n<p>This will add a command to the Excel menu and when clicked will run our macro\/<\/p>\n<p><!--\n<strong>ExcelAsyncTask<\/strong>\n\nManagedXLL also supports the ability to mark a method as an ExcelAsyncTask, which as the name suggested, creates a registration of an asynchronous task. \n\n[code language=\"csharp\"]\n[ExcelCategory(&quot;My Excel Functions&quot;)]\npublic class AsyncTasks\n{\n   [WorksheetFunction(&quot;AsyncTasks.RunIt&quot;)]\n   [ExcelAsyncTask(&quot;AsyncTasks.RunIt&quot;, TimeOut = 30)]\n   public static int LongRunning()\n   {\n   }\n}\n[\/code]\n\nThe code above can be called in Excel as\n\n[code language=\"excel\"]\n=AsyncTasks.RunIt()\n[\/code]\n\n<!--\n<strong>ExcelArray<\/strong>\n\nThe ExcelArray attribute can be used on arguments or returns to specify information about the array\n\n[code language=\"csharp\"]\n[WorksheetFunction]\n[return: ExcelArray(VectorOrientation = ExcelVectorOrientation.Column)]\npublic static string[] Letters()\n{\n   return new [] {&quot;A&quot;, &quot;B&quot;, &quot;C&quot;, &quot;D&quot;, &quot;E&quot;, &quot;F&quot;};\n}\n[\/code]\n--><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In the last post I went through the steps for creating a class library for use with ManagedXLL to create an Excel add-in. We very briefly encountered the ManagedXLL way of marking methods for use in Excel. Now I&#8217;m going to delve a little deeper into what we can do with ManagedXLL. This will not [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[54,57],"tags":[],"class_list":["post-797","post","type-post","status-publish","format-standard","hentry","category-excel","category-xll"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/797","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/comments?post=797"}],"version-history":[{"count":14,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/797\/revisions"}],"predecessor-version":[{"id":841,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/797\/revisions\/841"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=797"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=797"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=797"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}