Skip to the content.

Script Addin provides an easy way to define and run script interactions started from Excel.

Using ScriptAddin

Running an script is simply done by selecting the desired executable (R, Python, Perl, whatever was defined) on the dropdown “ScriptExecutable” in the Script Addin Ribbon Tab and clicking “run " beneath the Sheet-button in the Ribbon group "Run Scripts defined in WB/sheets names". With an activated "script output win" toggle button the script output is shown in an opened window. Selecting the Script definition in the ScriptDefinition dropdown highlights the specified definition range.

When running scripts, following is executed:

  1. The input arguments (arg, see below) are written to files,
  2. (optional, if defined) the scripts defined inside Excel are written,
  3. defined/written scripts are called using the executable located in ExePath/exec (see settings) and
  4. the defined results/diagrams that were written to files are read and placed in Excel.

When holding the Ctrl-Key pressed while clicking “run ", only step 4 is executed, skipping steps 1 to 3. This allows to quickly reinsert changed or deleted results/diagrams in Excel.

Image of screenshot1

Defining ScriptAddin script interactions (ScriptDefinitions)

script interactions (ScriptDefinitions) are defined using a 3 column named range (1st col: definition type, 2nd: definition value, 3rd: (optional) definition path):

The Scriptdefinition range name must start with “Script_” (or “R_Addin” as a legacy compatibility with the old R Addin) and can have a postfix as an additional definition name. If there is no postfix after “Script_”, the script is called “MainScript” in the Workbook/Worksheet.

A range name can be at Workbook level or worksheet level. In the ScriptDefinition dropdowns the worksheet name (for worksheet level names) or the workbook name (for workbook level names) is prepended to the additional postfixed definition name.

So for the 8 definitions (range names) currently defined in the test workbook testRAddin.xlsx, there should be 8 entries in the Scriptdefinition dropdown:

In the 1st column of the Scriptdefinition range are the definition types, possible types are

Scripts (defined with the script, scriptrng or scriptcell definition types) are executed in sequence of their appearance. Although exec, path and dir definitions can appear more than once, only the last definition is used.

Instead of script, scriptrng and scriptcell there can also be given skipscript as the type, indicating that this script should be skipped in the current run. Typically you would define this dynamically via a function in excel, depending on some other setting.

In the 2nd column are the definition values as described above.

In the 3rd column are the definition paths of the files referred to in arg, res and diag

The definitions are loaded into the ScriptDefinition dropdown either on opening/activating a Workbook with above named areas or by pressing the small dialogBoxLauncher “Show AboutBox” on the Script Addin Ribbon Tab and clicking “refresh ScriptDefinitions”:
Image of screenshot2

The mentioned hyperlink to the local help file can be configured in the app config file (ScriptAddin.xll.config) with key “LocalHelp”. When saving the Workbook the input arguments (definition with arg) defined in the currently selected Scriptdefinition dropdown are stored as well. If nothing is selected, the first Scriptdefinition of the dropdown is chosen.

The error messages are logged to a diagnostic log provided by ExcelDna, which can be accessed by clicking on “show Log”. The log level can be set in the system.diagnostics section of the app-config file (Scriptaddin.xll.config): Either you set the switchValue attribute of the source element to prevent any trace messages being generated at all, or you set the initializeData attribute of the added LogDisplay listener to prevent the generated messages to be shown (below the chosen level)

You can also run ScriptAddin in an automated way, simply issue the VBA command result = Application.Run("executeScript", <ScriptDefinitionName>, <headlessFlag>), where <ScriptDefinitionName> is the Name of the Script Definition Range and <headlessFlag> is a boolean flag indicating whether any user-interaction (as controllable by the Addin) should be avoided, all errors are returned in the result of the call.

Known Issues/Enhancements:

Installation of ScriptAddin and Settings

run Distribution/deployAddin.cmd (this puts ScriptAddin32.xll/ScriptAddin64.xll as ScriptAddin.xll and ScriptAddin.xll.config into %appdata%\Microsoft\AddIns and starts installAddinInExcel.vbs (setting AddIns(“ScriptAddin.xll”).Installed = True in Excel)).

Adapt the settings in ScriptAddin.xll.config:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <section name="UserSettings" type="System.Configuration.NameValueSectionHandler"/>
  </configSections>
  <UserSettings configSource="ScriptAddinUser.config"/> : This is a redirection to a user specific config file containing the same information below. These settings always override the appSettings
  <appSettings file="your.Central.Configfile.Path"> : This is a redirection to a central config file containing the same information below
    <add key="LocalHelp" value="C:\YourPathToLocalHelp\LocalHelp.htm" /> : If you download this page to your local site, put it there.
    <add key="ExePathR" value="C:\Program Files\R\R-4.0.4\bin\x64\Rscript.exe" /> : The Executable Path used for R
    <add key="FSuffixR" value=".R" /> : The File suffix used when writing temporary Files used in scriptrng/scriptcell for R
    <add key="StdErrXR" value="False" /> : Shall any output to standard err by R be regarded as an error that blocks further processing (default: True)
    <add key="ExePathPerl" value="C:\Strawberry\perl\bin\perl.exe" />
    <add key="PathAddPerl" value="C:\Strawberry\c\bin;C:\Strawberry\perl\site\bin;C:\Strawberry\perl\bin" /> : Additional Path Setting for Perl
    <add key="FSuffixPerl" value=".pl" />
    <add key="ExePathPython" value="C:\Users\rolan\anaconda3\pythonw.exe" />
    <add key="PathAddPython" value="C:\Users\rolan\anaconda3\Scripts;C:\Users\rolan\anaconda3\Library\bin;C:\Users\rolan\anaconda3\Library\bin;C:\Users\rolan\anaconda3\Library\usr\bin;C:\Users\rolan\anaconda3\Library\mingw-w64\bin" />
    <add key="FSuffixPython" value=".py" />
    <add key="ExePathWinCmd" value="C:\Windows\System32\cmd.exe" />
    <add key="ExeArgsWinCmd" value="/C" /> : Any additional arguments to the script executable
    <add key="FSuffixWinCmd" value=".cmd" />
    <add key="ExePathCscript" value="C:\Windows\System32\cscript.exe" />
    <add key="FSuffixCscript" value=".js" />
    <add key="presetSheetButtonsCount" value="24"/> : the preset maximum Button Count for Sheets (if you expect more sheets with ScriptDefinitions, you can set it accordingly)
  </appSettings>
  <system.diagnostics>
    <sources>
      <source name="ExcelDna.Integration" switchValue="All">
        <listeners>
          <remove name="System.Diagnostics.DefaultTraceListener" />
          <add name="LogDisplay" type="ExcelDna.Logging.LogDisplayTraceListener,ExcelDna.Integration">
            <!-- EventTypeFilter takes a SourceLevel as the initializeData:
                    Off, Critical, Error, Warning (default), Information, Verbose, All -->
            <filter type="System.Diagnostics.EventTypeFilter" initializeData="Warning" />
          </add>
        </listeners>
      </source>
    </sources>
  </system.diagnostics>
</configuration>

The settings for the scripting executables are structured as follows <ScriptExecPrefix><ScriptType> and form the selection of available script types in ScriptAddin.

Following ScriptExecPrefixes are possible:

The minimum requirement for a scripting engine to be regarded as selectable/usable is the ExePath entry. All other ScriptExecPrefixes are optional depending on the requirement of the scripting engine.

There are three settings files which can be used to create a central setting repository (<appSettings file="your.Central.Configfile.Path">) along with a user specific overriding mechanism (<UserSettings configSource="ScriptAddinUser.config"/>) defined in the application config file ScriptAddin.xll.config. All three settings files can be accessed in the ribbon bar beneaht the dropdown Settings.

Additionally you can find an insert Example mechanism in this dropdown that adds an example script definition range with the above described definition types and example configs.

Building

All packages necessary for building are contained, simply open ScriptAddin.sln and build the solution. The script deployForTest.cmd can be used to deploy the built xll and config to %appdata%\Microsoft\AddIns