By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,680 Members | 1,486 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,680 IT Pros & Developers. It's quick & easy.

Problem With an Undefined Variable

P: n/a
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
Jul 20 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
In article <2e**************************@posting.google.com >,
wo***********@yahoo.com enlightened us with...
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.

<snip>

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
-------------------------------------------------
Jul 20 '05 #2

P: n/a
kaeli <in********************@NOSPAMatt.net> writes:
In article <2e**************************@posting.google.com >,

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.


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
--
Lasse Reichstein Nielsen - lr*@hotpop.com
Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #3

P: n/a
In article <u1**********@hotpop.com>, lr*@hotpop.com enlightened us
with...

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
-------------------------------------------------
Jul 20 '05 #4

P: n/a
> 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
Jul 20 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.