Problem With an Undefined Variable

M

Mike

I am having a problem when a field is spaces being undefined. I wasn't
sure if the problem was Excel or Javascript, so I thought I would post
here first.

The users are able to select from a drop down list either a
pre-existing Excel spreadsheet or a blank spreadsheet where they can
enter the data. When they click the Store button I am using Javascript
to validate the fields. If a particular field is not entered or has
invalid data, then an error message will appear on the screen. In this
case, I was testing for a blank field and did not get the error
message I was expecting. So I put in some Response.Write statements to
display what was in the field and in this case is where it came back
as undefined. If there is data in the field, then the field displays
correctly. The following is an example of the code:

do
{
indexRow+=1;
}
while (mExcelApp.ActiveSheet.Cells(indexRow, 1) != "RECORD");
indexRow+=2;

RecordCount = 0;

do
{

var xlSourceDoc = mExcelApp.ActiveSheet.Cells(indexRow, 2);
var xlRefNumber = mExcelApp.ActiveSheet.Cells(indexRow, 3);
var xlFYR = mExcelApp.ActiveSheet.Cells(indexRow, 4);
var xlClass = mExcelApp.ActiveSheet.Cells(indexRow, 5);
var xlNNNNN = mExcelApp.ActiveSheet.Cells(indexRow, 6);
var xlAAAA = mExcelApp.ActiveSheet.Cells(indexRow, 7);
var xlLLLL = mExcelApp.ActiveSheet.Cells(indexRow, 8);
var xlFFF = mExcelApp.ActiveSheet.Cells(indexRow, 9);
var xlLineDollarAmt = mExcelApp.ActiveSheet.Cells(indexRow, 10);
var xlLineUnits = mExcelApp.ActiveSheet.Cells(indexRow, 11);
var xlLineDesc = mExcelApp.ActiveSheet.Cells(indexRow, 12);

if ((xlSourceDoc == "") || (xlSourceDoc == null) || (xlSourceDoc
== "undefined"))
{
mError = 11;
mResults = showErrorMsg();
break;
}
RecordCount+=1;
indexRow+=1;
}
while (RecordCount < xlRecCnt);

In showErrorMsg() is where I have my error messages and mError = 11
returns a particular message.

Being fairly new to both Excel and Javascript, I am at a lost as to
how to correct the problem. Does anyone have an idea what I need to
do? TIA for any help!

Mike
 
K

kaeli

I am having a problem when a field is spaces being undefined. I wasn't
sure if the problem was Excel or Javascript, so I thought I would post
here first.

if ((xlSourceDoc == "") || (xlSourceDoc == null) || (xlSourceDoc
== "undefined"))

Test for null first, then undefined, then for spaces. Order matters. It
can't test for spaces if it doesn't exist or is undefined. It can't test
for "undefined" if the object ref is null.

HTH


-------------------------------------------------
~kaeli~
Black holes were created when God divided by 0.
Not one shred of evidence supports the notion
that life is serious.
http://www.ipwebdesign.net/wildAtHeart
http://www.ipwebdesign.net/kaelisSpace
-------------------------------------------------
 
L

Lasse Reichstein Nielsen

kaeli said:
Test for null first, then undefined, then for spaces. Order matters. It
can't test for spaces if it doesn't exist or is undefined. It can't test
for "undefined" if the object ref is null.

It doesn't matter. There is no significant difference between comparing
to "null" and to "undefined" or the empty string. You can write
null == "undefined"
and it is false.

In fact, when you use "==" for comparison, some of the cases are
equivalent, since "==" performs type conversion before converting.
This conversion means that
null == undefined
(however, in the code above, it is not the value "undefined" that is
compared to, but the string containing the word "undefined").

Where it makes a difference is when you access properties of objects.
You cannot access the properties of the values "null" or "undefined".
Other values are converted to objects when you try, but those two
are not.
The following are legal, and give the result "undefined".
("dims").foo
(2).foo
(false).foo
These two are illegal, and give an error (an Exception of type
TypeError):
null.foo
undefined.foo

/L
 
K

kaeli

[email protected] enlightened us said:
Where it makes a difference is when you access properties of objects.

Which is as far as I can tell, what the OP was doing.

Did I misread the code?

var xlSourceDoc = mExcelApp.ActiveSheet.Cells(indexRow, 2);
....
if ((xlSourceDoc == "") || (xlSourceDoc == null) || (xlSourceDoc
== "undefined"))

That is a cell, isn't it, which is an object with properties, including
a value, color, etc?

-------------------------------------------------
~kaeli~
Black holes were created when God divided by 0.
Not one shred of evidence supports the notion
that life is serious.
http://www.ipwebdesign.net/wildAtHeart
http://www.ipwebdesign.net/kaelisSpace
-------------------------------------------------
 
M

Mike

Where it makes a difference is when you access properties of objects.
Which is as far as I can tell, what the OP was doing.

Did I misread the code?

var xlSourceDoc = mExcelApp.ActiveSheet.Cells(indexRow, 2);
...
if ((xlSourceDoc == "") || (xlSourceDoc == null) || (xlSourceDoc
== "undefined"))

That is a cell, isn't it, which is an object with properties, including
a value, color, etc?

-------------------------------------------------
~kaeli~
Black holes were created when God divided by 0.
Not one shred of evidence supports the notion
that life is serious.
http://www.ipwebdesign.net/wildAtHeart
http://www.ipwebdesign.net/kaelisSpace
-------------------------------------------------

Yes, this is a reference to a particular cell. IndexRow gets added to
keep track of what row is currently being processed on the
spreadsheet. I reversed the order of above If statement and tested for
null, then undefined, then spaces and I still got the same result. Is
there another way to reference a cell which is an object with
properties? Thanks!

Mike
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,077
Messages
2,570,569
Members
47,206
Latest member
MalorieSte

Latest Threads

Top