Issue 4695

Summary: INDIRECT function fails with named cells
Product: Calc Reporter: Unknown <non-migrated>
Component: codeAssignee: frank
Status: CLOSED FIXED QA Contact: issues@sc <issues>
Severity: Trivial    
Priority: P3 CC: issues, javibarroso, jonathan.n.mills, kyoshida, maremv, mloiseleur, ooo, r0polach, rpolach, sgautier.ooo, stx123
Version: OOo 1.0.0Keywords: ms_interoperability
Target Milestone: ---   
Hardware: All   
OS: All   
Issue Type: PATCH Latest Confirmation in: ---
Developer Difficulty: ---
Attachments:
Description Flags
Excel file
none
A patch which tries yo fix it
none
patch built upon Michel's earlier patch (SRC680_m236)
none
revised patch to handle relative references (SRC680_m236)
none
fixed memory leak - delete temporary ScTokenArray instance when going out of scope. none

Description Unknown 2002-05-09 17:10:40 UTC
The INDIRECT function works great if you refer to a cell that, in turn, refers to another cell with a 
value. The INDIRECT function fails if the referred cell contains a named cell, however.

For 
example, the following works great:

 - place the value 3 in cell C3
 - place the text "C1" in cell 
A1
 - call the function INDIRECT(A1) in cell A5

If you follow the above steps, the value 3 will 
appear in cell A5.

On the other hand, the following causes problems:

 - Place the value 3 in 
cell C3
 - Name C3 "test1" by using Insert->Name->Define
 - Test that the name worked by going to cell 
E1 and typing "=test1". The value 3 does in fact appear as it should.
 - Place the text "test1" in 
cell A1
 - call the function INDIRECT(A1) in cell A5

This results in "Err:502" in cell 
A5.

If I try this same process in MS Excel, it works fine.
Comment 1 jmills 2002-05-09 18:28:10 UTC
I have just discovered this too. I attach a similar (but different) 
spreadsheet that illustrates it...
Comment 2 jmills 2002-05-09 18:29:31 UTC
Created attachment 1602 [details]
Excel file
Comment 3 frank 2002-05-13 09:20:07 UTC
Hi Niklas,

one for you I think.

Frank
Comment 4 niklas.nebel 2002-05-22 16:50:21 UTC
INDIRECT doesn't support named ranges. We should extend it.
Comment 5 ooo 2002-05-22 17:28:59 UTC
yes, we should
Comment 6 Unknown 2002-05-29 11:05:02 UTC
Too bad this function isn't working properly: we can't abandon Excel because in my 
office some fundamental applications needs it :(
Comment 7 ooo 2002-12-09 15:00:57 UTC
Target to OOo 2.0
Comment 8 ooo 2004-05-27 12:04:25 UTC
Raise prio to P3 (only for statistical reasons to indicate that this really
should go into OOo2.0).
Comment 9 Martin Hollmichel 2004-05-28 15:01:21 UTC
according to the announcement on releases
(http://www.openoffice.org/servlets/ReadMsg?list=releases&msgNo=7503) this issue
will be re-targeted to OOo Later.
Comment 10 ooo 2004-06-01 10:47:19 UTC
Sigh.. seems the list of issues to be retargeted was created  before I changed
priority of this one.. so back to OOo2.0.
Comment 11 ooo 2004-07-30 11:40:46 UTC
Sigh again.. product management decided to postpone remaining features from
OOo2.0 to OOoLater. This was part of Q-PCD issue 20494.
Comment 12 mportier 2004-10-30 00:00:38 UTC
Just to be complete on this issue, when extending the indirect() to named cells
it should also be tested for external files, i.e.

=indirect('file://path-to-file.sxc#named_cell')

point being that today this doesn't even work for non-named cells:

=indirect('file://path-to-file.sxc'#sheet1.A1)  doesn't work either
Comment 13 frank 2004-11-02 13:41:55 UTC
*** Issue 36410 has been marked as a duplicate of this issue. ***
Comment 14 ooo 2004-12-07 13:56:57 UTC
mportier,

External references with non-named cells _do_ work, you just have to pass them
as string to INDIRECT(), as usual:
=indirect("'file://path-to-file.sxc'#sheet1.A1")
instead of
=indirect('file://path-to-file.sxc'#sheet1.A1)
unless you want the content of file://path-to-file.sxc'#sheet1.A1 being
evaluated by INDIRECT() ...

Eike
Comment 15 bdk7635 2005-02-04 16:28:11 UTC
OpenOffice is a great alternative to MS Excel! Keep up the good work.

The error is already reported, but I wanted to reiterate that this issue is a
critical roadblock to eliminating MS or porting advanced financial applications
to OO. 

Hopefully, it can be addressed soon.

Thanks
Comment 16 frank 2005-09-30 10:01:09 UTC
*** Issue 55244 has been marked as a duplicate of this issue. ***
Comment 17 dridgway 2005-12-03 23:55:28 UTC
*** Issue 58375 has been marked as a duplicate of this issue. ***
Comment 18 t_i_m_o 2006-05-29 10:06:29 UTC
Is an solution in nearer future possible?
Comment 19 ooo 2006-05-29 11:17:40 UTC
No. But this is quite on top of the list of features to implement.
Comment 20 maremv 2006-07-07 00:19:54 UTC
Is this going to be fixed soon? if not I can fix & submit
Comment 21 ooo 2006-07-13 14:35:11 UTC
Maremv,

Sure, working patches are welcome. Do you already have a fix in mind?

  Eike
Comment 22 maremv 2006-07-14 03:40:42 UTC
Nope, I have not even looked at the code yet. I just wanted to make sure I was
not duplicating work. I will begin looking into the fix -Thanks
Comment 23 maremv 2006-12-13 05:02:15 UTC
Sorry about the delay on this, I just got an email today that reminded me that I
said I would fixthis...

Ok I looked back at the source code I had downloaded when I originally was
looking at this problem and remembered quickly that I could not really find any
kind of structure to this program. To save me alot of time (because I'm guessing
some of the people on this board know this program pretty well) I need to be
pointed to the following:

Where & What is the source file/function name that has the function INDIRECT in it?
Where & What is the source file/function name that allows you to place =<named
field> into a cell and it will do the look up of that cell name and place the
value of that named cell into the cell?
Where & What is the source file/function name that allows cells values to be
looked up out of external spread sheets so I can make the following work as
requested in one of the messages below: =indirect(
'file://path-to-file.sxc#named_cell' )
Comment 24 ooo 2006-12-13 16:47:21 UTC
Hi Maremv,

Implementation is in sc/source/core/tool/interpr1.cxx method
ScInterpreter::ScIndirect(). After the calls to ConvertDoubleRef()
respectively ConvertSingleRef() the else path containing just
SetIllegalArgument() right now must be extended:

- check whether the string matches a defined name, similar to
  sc/source/core/tool/compiler.cxx ScCompiler::IsNamedRange(), but
  without creating a token, instead

- check whether the range data consists of just one single cell or
  a range, other data like multiple ranges or formula fragments are not
  valid. You may use pRangeData->IsReference() for this.

- Relative reference parts need a special handling, and to be Excel
  compatible the relative offsets have to be calculated with a basis of
  A1 and wrapped around the sheet if negative. I suggest to create
  another ScRangeData::IsReference() method that additionally takes
  a ScAddress position (ScInterpreter::aPos member variable), same for
  ScTokenArray::IsReference(), and enhance
  ScTokenArray::ImplGetReference() to take account of an optional
  position address passed in.

I suggest to refrain from trying to handle the external reference case
at this stage, which would unnecessarily complicate things, and let it
be a separate issue instead. However, if you feel confident that could
be accomplished by digging deep into the lcl_ScAddress_Parse_...()
methods of sc/source/core/tool/address.cxx

Please note that to integrate code or data contributed we need a signed
Joint Copyright Assignment form (JCA) filled-out, see
http://contributing.openoffice.org/programming.html#jca
To be able to lookup your name in the list of approved assignments I'd
appreciate if you stated your full name here in this issue.

  Eike
Comment 25 maremv 2006-12-13 23:22:38 UTC
I Have already filled out one of thoes forms and emailed it to
copyrightfax@sun.com. My full name is William Neil Hall. Thanks for the point in
the right direction, now let me dig in a little. -Thanks
Comment 26 ooo 2006-12-14 10:36:52 UTC
Hi William,

Yes, confirming your name is listed, so we're fine to proceed once we receive
your patch. Please go ahead :-)

Btw, forget the ScInterpreter::aPos member variable I mentioned above, that's
nonsense since we do not want the current formula's position but A1 instead, sorry.

Thanks
  Eike
Comment 27 maremv 2007-01-04 23:22:37 UTC
I have been looking for a resource page on openoffice.org on the best way to
compile my OO changes, but not having much luck. Can someone point me to a web
page? or tell me the best set of tools to use for Win-XP? -Thanks
Comment 29 Regina Henschel 2007-01-30 18:23:15 UTC
*** Issue 73962 has been marked as a duplicate of this issue. ***
Comment 30 t_i_m_o 2007-03-21 13:46:28 UTC
Is an solution in nearer future possible?
Thanks.
Comment 31 ooo 2007-03-21 14:03:00 UTC
William (maremv) volunteered to work on this, don't know about progress.

William, how are things proceeding?

  Eike
Comment 32 t_i_m_o 2007-03-21 14:27:49 UTC
Thanks er,
this Issue is very important for our company.
We want that all users use Open Office, but without this function it is not
possible.
Our customer calculation depends on it.


Comment 33 maremv 2007-03-21 17:50:25 UTC
Sorry I just haven't had enough time to work on this. It might be best if
someone else tries to fix.
Comment 34 kingshome 2007-04-03 12:27:44 UTC
Is there any chance of this issue being dealt with in 2.3?  It's been around for
5 years without resolution.

Comment 35 ooo 2007-04-03 12:38:20 UTC
@kingshome: if you find someone who'll implement it. I for myself won't have
time to do it for 2.3.
Comment 36 mloiseleur 2007-04-18 13:11:34 UTC
Created attachment 44510 [details]
A patch which tries yo fix it
Comment 37 mloiseleur 2007-04-18 13:13:18 UTC
Hello there,

   Here is a patch which fix this issue. Hope this helps. 


Regards,
Comment 38 frank 2007-04-18 14:05:04 UTC
changed Issue Type to PATCH
Comment 39 sgautier.ooo 2007-04-18 14:27:16 UTC
adding me to cc - sophie
Comment 40 ooo 2007-04-18 16:01:16 UTC
Hi Michel,

Thanks for contributing. The patch generally takes the right approach,
but doesn't handle relative references. I'll restate the requirement
from #desc25 here:

- Relative reference parts need a special handling, and to be Excel
  compatible the relative offsets have to be calculated with a basis of
  A1 and wrapped around the sheet if negative. I suggest to create
  another ScRangeData::IsReference() method that additionally takes
  a ScAddress position (ScInterpreter::aPos member variable), same for
  ScTokenArray::IsReference(), and enhance
  ScTokenArray::ImplGetReference() to take account of an optional
  position address passed in.

To accomplish that, the easiest and safest would be to clone the
ScTokenArray (pCode) in the newly to be implemented
ScRangeData::IsReference() method, stuff it into a compiler instance and
call MoveRelWrap(), similar to what is done in
ScRangeData::UpdateSymbol(), and then call the newly to be created
ScTokenArray::IsReference().

Btw, interpreter functions should never simply return without having set
an error value and/or pushed a result. The reason I mention this is the
line
+     if (rData == NULL) return;
of your patch. Btw2, in our naming conventions rData should be named
pData instead, since it is a pointer. If a premature return is needed,
the code instead should be something like
        if (pData == NULL)
        {
            SetError(errUnknownState);
            PushInt(0);
            return;
        }

Please note that to integrate code or data contributed we need a signed
Joint Copyright Assignment form (JCA) filled-out, see
http://contributing.openoffice.org/programming.html#jca

Thanks
  Eike
Comment 41 mloiseleur 2007-04-18 17:15:41 UTC
Thanks for your answer Eike. Here is mine :

>- Relative reference parts need a special handling, and to be Excel
>  compatible [...]
Can you please explain to me what exactly is a relative reference ? My patch
works well on samples documents. 

>Btw, interpreter functions should never simply return without having set
>an error value and/or pushed a result. The reason I mention this is the
>line
>+     if (rData == NULL) return;
Ok. I didn't really know what error should be returned. It was more for escaping
segv than anything else. It's not supposed to be an error in first place. But
you're right. 

>of your patch. Btw2, in our naming conventions rData should be named
>pData instead, since it is a pointer. If a premature return is needed,
>the code instead should be something like
        if (pData == NULL)
        {
            SetError(errUnknownState);
            PushInt(0);
            return;
        }
Heck, 3 lines for return, you're hard with me, I don't like code too long :). 

I will do the change to pData and for premature return in a patch. My CWS is not
here, but it's coming nicely and surely, thanks Sophie ^_^.

Regards,
Michel
Comment 42 ooo 2007-04-19 14:26:44 UTC
Hi Michel,

Relative references are displayed without the dollar character, in the
names dialog you may define them by editing the reference and removing
the dollars, for example modify $Sheet1.$A$1 to $Sheet.A1

The relative reference generated then depends on the position of the
cell cursor when you opened the name dialog, for example if the current
cell is B2, a reference of A1 actually means "one row up and one column
to the left". If you reopen the dialog on a different cell the reference
of that named range displayed is adjusted accordingly.

Note that the requirement I copied above actually has an error stating
"(ScInterpreter::aPos member variable)", for Excel compatibility this is
exactly not the case, but named relative references for the INDIRECT
function are based on A1 instead. Weird, and IMHO wrong, but that's how
Excel does it. Excel doesn't know relative sheet references in named
ranges, but Calc does, so the address to pass from within
ScInterpreter::ScIndirect() probably should be
ScAddress( aPos.Tab(), 0, 0)

> Heck, 3 lines for return, you're hard with me, I don't like code too
> long :). 

Well, the more common errors have convenience functions, such as
SetIllegalArgument() that sets the error and pushes a value, so one line
less to code, an errUnknownState just isn't that usual..

> I will do the change to pData and for premature return in a patch. My
> CWS is not here, but it's coming nicely and surely, thanks Sophie ^_^.

Huh? Does hat mean you want to create your own CWS for this? Do you
already have CVS write access, which is needed to do so?

  Eike
Comment 43 jodygoldberg 2007-05-25 19:26:31 UTC
Although XL seems to limit the content of the names to be simple, I don't see
why we want to continue that.  In which case the same problem that arose with
the validation lists comes up, how to interpret a general tokenarray in a
context outside a cell.

eg

define FOO as IF(A1;A2;A3)
=INDIRECT("FOO")
Comment 44 jodygoldberg 2007-05-25 22:04:04 UTC
XL compat will also require support for parsing
    Sheet!name
Comment 45 mloiseleur 2007-06-01 12:57:39 UTC
-> jodygoldberg : Do you have any clues on how to do that easily ? So I can
update my patch ?

Regards,
Comment 46 frank 2007-07-09 11:12:04 UTC
*** Issue 79354 has been marked as a duplicate of this issue. ***
Comment 47 kyoshida 2007-11-14 18:24:55 UTC
Created attachment 49656 [details]
patch built upon Michel's earlier patch (SRC680_m236)
Comment 48 kyoshida 2007-11-14 18:27:56 UTC
Hi *,

I had to work on this issue per customer request.  I basically applied Michel's
patch, cleaned up a bit, and applied the same principle to INDIRECT_XL as well
(INDIRECT_XL is disabled upstream at the moment).

Jody mentioned to cover named expressions, but IMO that should be a separate
issue to tackle as this patch alone solves many users' problems.

Kohei
Comment 49 kyoshida 2007-11-14 19:53:42 UTC
I'm still working on handling the relative reference.  So, the patch I just
posted only works reliably.  Expect an additional patch soon.
Comment 50 ooo 2007-11-14 20:01:16 UTC
Fine patch, thanks. As discussed on IRC, the case when the defined name contains
relative references needs to be handled though, else users will rely on a result
of foo:=B3 being identical to foo:=$B$3.
Comment 51 kyoshida 2007-11-14 20:06:07 UTC
bah!  I meant to say "the patch I just posted only works reliably _on named
ranges containing absolute references_."
Comment 52 mloiseleur 2007-11-14 20:23:18 UTC
->kohei: It's a nice and pretty way to have one line for exit. I really like
your improvement of your patch. Thanks a lot for showing me this cool approach.

Comment 53 kyoshida 2007-11-14 21:17:54 UTC
Created attachment 49659 [details]
revised patch to handle relative references (SRC680_m236)
Comment 54 kyoshida 2007-11-14 21:22:45 UTC
I think I got it.  All I did was follow Eike's fine instructions.  It was pretty
much painless. ;-)

mloiseleur: yup, that is a well-known (and very useful) construct when you need
to emulate a try-catch block where you can't really use exceptions.  It also
helps reduce indentations which helps readability.
Comment 55 kyoshida 2007-11-14 21:41:20 UTC
Created attachment 49660 [details]
fixed memory leak - delete temporary ScTokenArray instance when going out of scope.
Comment 56 Martin Hollmichel 2007-11-15 04:12:25 UTC
@kohei,er: so the target should be moved from OOoLater to OOo 2.4 ?
Comment 57 ooo 2007-11-15 09:11:54 UTC
Much appreciated, Kohei, thank you!
Comment 58 ooo 2007-12-14 12:39:09 UTC
Kohei, please take over and add your patch to a CWS for OOo2.4
Thanks
  Eike
Comment 59 kyoshida 2007-12-14 20:06:01 UTC
fixed in SRC680/kohei01 cws.
Comment 60 kyoshida 2007-12-21 14:48:06 UTC
reassigning to fst for verification
Comment 61 frank 2008-01-16 13:57:19 UTC
found fixed on cws kohei01
Comment 62 frank 2008-02-11 11:17:46 UTC
found integrated on master OOHm5 using Linux, Solaris and Windows builds
Comment 63 kingshome 2008-04-21 06:42:16 UTC
Does the 'fixed' label on this issue refer to ooo 2.4 or the upcoming ooo 3?
The reason I ask is because a problem I found in an xls file still hasn't been
resolved in ooo 2.4.  I thought it was caused by the problem with INDIRECT, but
if this issue has now been resolved, I'll have to look for other reasons.
Comment 64 kyoshida 2008-04-21 14:04:46 UTC
@kingshome: this patch has been integrated into 2.4, so whatever problem you
have is a new problem.  Please file a separate issue (or try to find an existing
issue if exists) describing the problem you're experiencing.  Thanks!
Comment 65 i5513 2011-06-12 14:29:59 UTC
I tried to use indirect inside vlookup / hlookup and it doesn't work

A1 cell contains the name of a named range, but formulas like :

vlookup ("A";indirect("A1");3;0)

doesn't work

I suppose, it should work if this issue is closed 

I'm testing it with openoffice 3.2.1. I will test in 3.3 again

Thanks!