Change Excel Cell Value / Call By Reference Parameter

Discuss code issues here

Change Excel Cell Value / Call By Reference Parameter

Postby SeanKw » Fri Oct 04, 2013 2:11 pm

Hi all,
I guess this is a simple question, but my google search did not result in any good answers. So maybe you guys have a quick solution.

I have the following C++ sample function
Code: Select all
double mySum(double &arg1, double &arg2)
{
  arg1++;
  return arg1+arg2;
}


I've built the xll (of course I'm using xlw) and use the Addin in Excel. In Excel I have 3 cells
A1: 1
A2: 3
A3: =mySum(A1;A2)

A3 displays the correct sum (=5), but A1 is not incremented (should be 2).

Somehow the call-by-reference in the C++ function does not work.
How do I have to make that function call?
Or alternatively: how do I change the A1 cell value from C++?

Cheers :)
Sean
SeanKw
 
Posts: 4
Joined: Fri Oct 04, 2013 1:51 pm

Re: Change Excel Cell Value / Call By Reference Parameter

Postby mj » Tue Oct 08, 2013 3:15 am

Changing values of cells is not supported by the InterfaceGenerator. So you'd have to work with XlfOpers and handcode the interfacing code.

I never do this so can't be much help. You'd probably get a better answer using the xlw-users mailing list.
mj
Site Admin
 
Posts: 1380
Joined: Fri Jul 27, 2007 7:21 am

Re: Change Excel Cell Value / Call By Reference Parameter

Postby SeanKw » Wed Oct 09, 2013 3:29 pm

Hi Mark,
thanks for your reply. I tried using XlfOpers, but somehow it does not work as I hoped.

Code: Select all
void // should write 'foo' to cell A1
printFoo() {
   xlw::XlfServices.Cell.SetContents(XlfRef(0, 0), "foo");
}


My intention is to write "=printFoo()" in my Excel cell, say C3. But instead of writing "foo" to my A1 cell, the formula returns FALSE in C3.
XLW somehow creates a new menu with the name of my lib in the menu bar, though. If I open the menu, there is a submenu with the description of my function ("should write 'foo' to cell A1").
If I click on that sub menu, "foo" appears in the A1 cell. So the function seems to work, but I cannot directly use in an Excel cell.

Maybe I'll ask on the mailing list, but I'm fine with another workaround.

Thanks and cheers!
Sean
Last edited by SeanKw on Tue Oct 15, 2013 8:55 am, edited 1 time in total.
SeanKw
 
Posts: 4
Joined: Fri Oct 04, 2013 1:51 pm

Re: Change Excel Cell Value / Call By Reference Parameter

Postby mj » Wed Oct 09, 2013 11:58 pm

my view of xlw is that it's an effective way to add new functions to EXCEL. I was never interested in using it to control EXCEL -- that's the job of VBA.

However, many people do think differently and do these sorts of things using xlw.
mj
Site Admin
 
Posts: 1380
Joined: Fri Jul 27, 2007 7:21 am

Re: Change Excel Cell Value / Call By Reference Parameter

Postby JohnAd » Wed Jan 27, 2016 9:27 am

Ty flagging the function as a macro sheet function with

//<xlw:macrosheet

in the header file just above the definition of your function, this will enable the command to fiddle with the sheet. I don't really recommend this approach though, there is usually a better way.

John
JohnAd
 
Posts: 7
Joined: Mon Dec 10, 2012 7:58 pm


Return to Code

Who is online

Users browsing this forum: No registered users and 1 guest