Created
June 28, 2012 16:48
-
-
Save govert/3012444 to your computer and use it in GitHub Desktop.
Excel-DNA - Range processing via the C API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<DnaLibrary Name="RangeConvert using C API" Language="C#" RuntimeVersion="v4.0"> | |
<Reference Name="System.Windows.Forms" /> | |
<![CDATA[ | |
using System; | |
using System.Windows.Forms; | |
using ExcelDna.Integration; | |
public static class TestRangeMacros | |
{ | |
[ExcelCommand(MenuName="Test Range Macros - C API", MenuText="Double the Range")] | |
public static void RangeDoubleCAPI() | |
{ | |
ExcelReference inRange; | |
ExcelReference outRange; | |
object[,] inValues; | |
object[,] outValues; | |
try | |
{ | |
inRange = (ExcelReference)XlCall.Excel(XlCall.xlfInput, "Range to read: ", 8 /*type_num = 8 : Range */, "Doubling function"); | |
outRange = (ExcelReference)XlCall.Excel(XlCall.xlfInput, "Range to write: ", 8 /*type_num = 8 : Range */, "Doubling function"); | |
inValues = (object[,])inRange.GetValue(); | |
outValues = new object[inValues.GetLength(0), inValues.GetLength(1)]; | |
for (int i = 0; i < inValues.GetLength(0); i++) | |
{ | |
for (int j = 0; j < inValues.GetLength(1); j++) | |
{ | |
if (inValues[i,j] is double) | |
outValues[i,j] = (double)inValues[i,j] * 2.0; | |
else | |
outValues[i,j] = "!ERROR"; | |
} | |
} | |
outRange.SetValue(outValues); | |
} | |
catch (Exception e) | |
{ | |
MessageBox.Show(e.ToString()); | |
} | |
} | |
} | |
]]> | |
</DnaLibrary> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment