LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5
Default Returning Arrays, emulating standard function behavior

I'm developing an add-in for Excel (in C#) to provide vector (numeric,
not stl)
and quaternion functions within spreadsheets. Typical standard Excel
function behavior is such that, you select a cell where you'd like the
results of a function, press the "=" button, select the desired
function, select the cell range(s) needed by the function, click OK,
and the single result appears in the initially selected cell. I can
emulate this behavior with a function like:

//Returns magnitude of vector selected as Range
public double GetMagnitude(object Range)
{
Excel.Range rangeThis = Range as Excel.Range;

double dS = 0.0;
Object[,] arrayThis;
arrayThis = (Object[,])rangeThis.Value2;
//this is overkill, but handles vectors arranged as rows or
columns, or even matrices
for (int i = arrayThis.GetLowerBound(0); i <=
arrayThis.GetUpperBound(0); i++)
{
for (int j = arrayThis.GetLowerBound(1); j <=
arrayThis.GetUpperBound(1); j++)
{
dS += ((double)arrayThis[i, j]) *
((double)arrayThis[i, j]);
}
}
return Math.Sqrt(dS);
}

This works great. However, sometimes (mostly) the result of a vector
or quaternion operation is not a single scalar value, but an array of
values. I have tried returning an array of doubles, but all that shows
up in Excel is the first element of the array. For example, here's a
(pointless) function to return a given range, doubled:

public double[] DoubleCells(object Range)
{
Excel.Range rangeThis = Range as Excel.Range;

Object[,] arrayThis;
arrayThis = (Object[,])rangeThis.Value2;
double[] dResult = new double[rangeThis.Count];
int iIndex=0;
for (int i = arrayThis.GetLowerBound(0); i <=
arrayThis.GetUpperBound(0); i++)
{
for (int j = arrayThis.GetLowerBound(1); j <=
arrayThis.GetUpperBound(1); j++)
{
dResult[iIndex] = ((double)arrayThis[i, j]) * 2.0;
++iIndex;
}
}
return dResult;
}

Problem is, it just fills a single cell with the initial array value
upon return. Even if I select a range of cells to be filled with the
result, all that appears is the first element of the arrray.

I know that one possible solution would be to define a specific range
in the worksheet and write the resulting array into that range, but
this is very inflexible. I gather that it is feasible to do things
like offsets to a range, but I don't want the result in a fixed offset
to the input range, I want it to appear as an offset to the cell where
I pushed the "=" button.

Any helpful suggestions would be appreciated; I'm predominantly a
C++/OpenGL/numerics programmer, and am new to this Excel/C# stuff.
Thanks :)

 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Returning arrays from custom worksheet functions in xll files JacksonRJones Excel Programming 0 March 22nd 06 05:47 PM
Can't figure out passing and returning arrays Dave B[_9_] Excel Programming 3 December 10th 05 12:41 AM
RTD returning arrays Jens Thiel[_2_] Excel Programming 0 February 3rd 05 10:41 PM
emulating xl consts noel mc Excel Programming 2 November 9th 04 04:21 PM
application.run for returning arrays levent Excel Programming 3 July 23rd 04 08:01 PM


All times are GMT +1. The time now is 08:06 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"