PDA

View Full Version : Excel <> javascript ::condensed::


energy0m
10-17-2004, 10:10 PM
so maybe i posted a bit too much info the first time ....
the premise here is to create an online tax and use form the form currently lives at
http://students.uat.edu/marjunea/test (non working stuff commented out)
and the excel it is based off of lives just nextdoor at
http://students.uat.edu/marjunea/test/excel.xls

take a look at this part.. its the part im having most trouble with and im not so sure the syntax is correct .. if any one can help id be forever greateful

function calculate(obj) {



timething=parseInt(obj.telltime.value);


/* these are here to make calculations based on hidden calculations */
//start da funky ride

dafunkyvars=eval(Math.round(timething)/31);
percenthing=eval(dafunkyvars*.05);
perthing2=eval(IF (dafunkyvars>0) (0.15/365*dafunkyvars) );
calcy1=eval(percenthing*SalesUse20);
calcy2=eval(perthing2*SalesUse20);

//end da funky ride


SalesUse21=eval(Math.round(IF (calcy1 >0) (calcy1)));
SalesUse22=eval(Math.round(IF (calcy2 >0) (calcy2)));
SalesUse23=eval(SalesUse21+SalesUse22);
SalesUse24=eval(z);
SalesUse25=eval(IF ((SalesUse23+SalesUse24) >0) (rounding(SalesUse23+SalesUse24) ));
SalesUse26=eval(IF ((SalesUse23+SalesUse24) <0) (rounding(SalesUse23+SalesUse24) ));
SalesUse28=(a)
SalesUse29=(b)


obj.SalesUse19.value=(SalesUse19);
obj.SalesUse20.value=(SalesUse20);
obj.SalesUse21.value=(SalesUse21);
obj.SalesUse22.value=(SalesUse22);
obj.SalesUse23.value=(SalesUse23);
obj.SalesUse24.value=(SalesUse24);
obj.SalesUse25.value=(SalesUse25);
obj.SalesUse26.value=(SalesUse26);
}
function rounding(n)
{
pennies = n * 100 ;
pennies = Math.round(pennies) ;
strPennies = "" + pennies ;
len = strPennies.length ;
return strPennies.substring(0, len - 2) + "." + strPennies.substring((len - 2), len);
}


my main question is this

are the if statements in there structured correctly and more so if the if statements are based of excel formulas
{for instance
perthing2=eval(IF (dafunkyvars>0) (0.15/365*dafunkyvars) );
is baesed off the excel formula
=IF(G91>0,0.15/365*(F90-F91),0)
}

are these equivalent ?
if they aren equivalent how would you structure them so they are?
thanx!!!

Roy Sinclair
10-18-2004, 07:30 PM
If you use the "eval" function you've probably made a mistake. If you use it a lot then you definitely have made a mistake.

For example:

dafunkyvars=eval(Math.round(timething)/31);

is the same as

dafunkyvars=Math.round(timething)/31;

except there's more overhead involved in processing the first one.

Also there is no such thing in Javascript as "IF", javascript is case sensitive so it's going to be "if". You appear to be wanting to use a ternary form of the "if" which would mean that while you coded:

perthing2=eval(IF (dafunkyvars>0) (0.15/365*dafunkyvars) );

you should have coded:

perthing2=(dafunkyvars>0) ? (0.15/365*dafunkyvars) : 0;

What that does is evaluate the expression "(dfunkyvars>0)" and if it's true then the result of the expression "(0.15/365*dafunkyvars)" is returned while a false result would cause the simple expression "0" to be returned. The whole thing written as a regular "if" statement would look like:

if (dafunkyvars>0)
perthing2 = 0.15/365*dafunkyvars;
else
perthing2 = 0;

energy0m
10-18-2004, 09:58 PM
awesome... thanx for your comments!!
fortunately i finished the project last night after someone had given me some of the same comments..

i didnt realise that you could write a conditional as
XX=((y>0) ? (z*Y) : 0) ;

i'll definetly be using that more ..... i definetly learned lots on this project as this is my first custom script mostly from the ground up ... the final product can be seen at
http://students.uat.edu/marjunea/test

;)
thanx for everyones help!!