{"id":640,"date":"2015-03-10T00:31:32","date_gmt":"2015-03-10T00:31:32","guid":{"rendered":"http:\/\/solverstudio.org\/?page_id=640"},"modified":"2015-04-13T10:45:01","modified_gmt":"2015-04-13T10:45:01","slug":"ironpython","status":"publish","type":"page","link":"https:\/\/solverstudio.org\/languages\/ironpython\/","title":{"rendered":"SolverStudio &#038; IronPython"},"content":{"rendered":"<p>SolverStudio contains a full IronPython interpreter; this is selected under the language &#8220;PuLP (IronPython)&#8221;.<\/p>\n<p>SolverStudio creates Python floats, strings, dictionaries, tuples and lists corresponding to the user defined DataItems, and injects these into the user&#8217;s scope so they can be accessed directly using the DataItem&#8217;s name. We recommend you look at the &#8220;Python Examples.xlsx&#8221; spreadsheet to see how items on a sheet are mapped to Python objects. Note that SolverStudio traps all writes to these variables, and updates its internal store for that DataItem. This can sometimes result in non-Pythonic behaviour in thatif two variables refer to the same Data Item, they can both have their values changed by a single assignment statement.<\/p>\n<p>SolverStudio allows direct access to the Excel API functions and methods using the three pre-defined objects<\/p>\n<p><a href=\"https:\/\/msdn.microsoft.com\/en-us\/library\/office\/ff194565.aspx\">Application<\/a>: The standard Excel application object,<br \/>\n<a href=\"https:\/\/msdn.microsoft.com\/en-us\/library\/office\/aa174742%28v=office.11%29.aspx\">ActiveSheet<\/a>: The currently active sheet,<br \/>\n<a href=\"https:\/\/msdn.microsoft.com\/en-us\/library\/office\/aa174744%28v=office.11%29.aspx\">ActiveWorkbook<\/a>: The currently active workbook.<\/p>\n<p>An an example, you can use the following code to access a named range on the active sheet:<\/p>\n<pre class=\"top-set:false bottom-set:false toolbar:2 show-lang:2 lang:default decode:true\">print ActiveSheet.Range(\"Test\").Value2<\/pre>\n<p>Please also see the VBA documentation for these (using the links above). More IronPython-specific details are available on using <a href=\"http:\/\/www.ironpython.info\/index.php?title=Interacting_with_Excel\" target=\"_blank\">Using IronPython with Excel<\/a>, but note that we have already set up the application, active sheet and activer workbook objects ready to use (and some of this seems out of date now).<\/p>\n<p>Advanced Note: Code written for IronPython using &#8220;<a href=\"https:\/\/msdn.microsoft.com\/en-us\/library\/office\/ff194565.aspx\">Application<\/a>&#8220;, &#8220;<a href=\"https:\/\/msdn.microsoft.com\/en-us\/library\/office\/aa174742%28v=office.11%29.aspx\">ActiveSheet<\/a>&#8221; and &#8220;<a href=\"https:\/\/msdn.microsoft.com\/en-us\/library\/office\/aa174744%28v=office.11%29.aspx\">ActiveWorkbook<\/a>&#8221; will often work under both IronPython and CPython. However, in some cases, IronPython requires a trailing (). We have used .Value2 above because this works identically under both IronPython and CPython. However, if we use .Value, then it must be ActiveSheet.Range(&#8220;Test&#8221;).Value in CPython and ActiveSheet.Range(&#8220;Test&#8221;).Value() (with brackets) in IronPython. This is because .Value can take an argument. Thus .Value(10) works, and .Value[10] also works, where 10 is &#8220;<a href=\"https:\/\/msdn.microsoft.com\/en-us\/library\/microsoft.office.interop.excel.xlrangevaluedatatype.aspx\"><span class=\"selflink\">xlRangeValueDefault<\/span><\/a>&#8221; and the [] notation is IronPython&#8217;s way of distinguishing between multiple versions of a call.<\/p>\n<p>As an example, the following code example shows how to directly access a range on a sheet as a .Net array of Variants. (This can be faster than using the more typical SolverStudio approach of a double-indexed dictionary of values. Note that the blank cells come through as &#8220;None&#8221; in the direct access. As the code shows, SolverStudio creates a list of tuples for this Data Item, but this excludes any blank cells.<\/p>\n<p><a href=\"http:\/\/solverstudio.org\/ironpython\/directsheetaccessexample\/\" rel=\"attachment wp-att-641\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-641\" src=\"http:\/\/solverstudio.org\/wp-content\/uploads\/2015\/03\/DirectSheetAccessExample.png\" alt=\"DirectSheetAccessExample\" width=\"667\" height=\"357\" srcset=\"https:\/\/solverstudio.org\/wp-content\/uploads\/2015\/03\/DirectSheetAccessExample.png 667w, https:\/\/solverstudio.org\/wp-content\/uploads\/2015\/03\/DirectSheetAccessExample-250x134.png 250w, https:\/\/solverstudio.org\/wp-content\/uploads\/2015\/03\/DirectSheetAccessExample-300x161.png 300w, https:\/\/solverstudio.org\/wp-content\/uploads\/2015\/03\/DirectSheetAccessExample-150x80.png 150w, https:\/\/solverstudio.org\/wp-content\/uploads\/2015\/03\/DirectSheetAccessExample-400x214.png 400w\" sizes=\"auto, (max-width: 667px) 100vw, 667px\" \/><\/a><\/p>\n<p>The code is as follows, and can be copied into a &#8220;PuLP (IronPython)&#8221; model in SolverStudio.<\/p>\n<pre class=\"toolbar:1 lang:python decode:true \">print \"Named Range Test=\",ActiveSheet.Range(\"Test\").Address()\r\nprint \"Test=\",Test\r\nFasterTest = ActiveSheet.Range(\"Test\").Value2\r\nfor i in (0,1,2):\r\n    for j in (0,1): \r\n       print \"FasterTest[\",i,j,\"]=\",FasterTest[i,j]\r\n<\/pre>\n<p>SolverStudio also supports using an external Python installed by the user; <a title=\"SolverStudio &amp; CPython\" href=\"http:\/\/solverstudio.org\/cpython\/\">learn more here<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>SolverStudio contains a full IronPython interpreter; this is selected under the language &#8220;PuLP (IronPython)&#8221;. SolverStudio creates Python floats, strings, dictionaries, tuples and lists corresponding to the user defined DataItems, and injects these into the user&#8217;s scope so they can be accessed directly using the DataItem&#8217;s name. We recommend you look at the &#8220;Python Examples.xlsx&#8221; spreadsheet [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":201,"menu_order":0,"comment_status":"open","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-640","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/solverstudio.org\/wp-json\/wp\/v2\/pages\/640","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/solverstudio.org\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/solverstudio.org\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/solverstudio.org\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/solverstudio.org\/wp-json\/wp\/v2\/comments?post=640"}],"version-history":[{"count":9,"href":"https:\/\/solverstudio.org\/wp-json\/wp\/v2\/pages\/640\/revisions"}],"predecessor-version":[{"id":727,"href":"https:\/\/solverstudio.org\/wp-json\/wp\/v2\/pages\/640\/revisions\/727"}],"up":[{"embeddable":true,"href":"https:\/\/solverstudio.org\/wp-json\/wp\/v2\/pages\/201"}],"wp:attachment":[{"href":"https:\/\/solverstudio.org\/wp-json\/wp\/v2\/media?parent=640"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}