www.webdeveloper.com
Results 1 to 7 of 7

Thread: Convert Excel formula to Javascript

  1. #1
    Join Date
    Dec 2009
    Posts
    51

    Convert Excel formula to Javascript

    Hi

    Can anyone help me convert this formula to Javascript:

    =IF((E9*0.05)>2, E9*0.05, 2)
    (IF(logical_test,value_if_true,value_if_false))

    Not sure how to go about the if statement and the value if true function in Javascript.

    Hope some one can help!

    Many thanks!

  2. #2
    Join Date
    May 2013
    Location
    USA
    Posts
    8
    use if else condition in nested form, try it may help you

  3. #3
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,390

    Lightbulb

    Uses 'onblur' to effect change, but you can alter to some other event ...
    Code:
    <!DOCTYPE HTML>
    <html>
    <head>
    <title> Untitled </title>
    <meta charset="utf-8">
    
    </head>
    <body>
    <table border="1"><tr>
     <td><input type="text" id="E9a" value="1" onblur="adjustFormula(this.value)"></td>
     <td><input type="text" id="E9b" value=""></td>
    </tr></table>
    
    <script type="text/javascript">
    function adjustFormula(v) {
      v = Number(v);
      if (v >= 2) { document.getElementById('E9b').value = (v * 0.05).toFixed(2); }
             else { document.getElementById('E9b').value = 2; }
    }
    </script>
    
    </body>
    </html>

  4. #4
    Join Date
    Dec 2009
    Posts
    51
    JMRKER thanks so much for your help!!

    There was a mistake in my formula! Sorry about that it was supposed to be:
    =IF((E9*0.05)>2, E9*0.06, 2)

    I have the formula answer going into a field called "result".

    Is the figure entered into E9a going into 'v' variable?

    Many thanks for your help again...
    Hope you can help me further as I cannot get the formula to work!

    Quote Originally Posted by JMRKER View Post
    Uses 'onblur' to effect change, but you can alter to some other event ...
    Code:
    <!DOCTYPE HTML>
    <html>
    <head>
    <title> Untitled </title>
    <meta charset="utf-8">
    
    </head>
    <body>
    <table border="1"><tr>
     <td><input type="text" id="E9a" value="1" onblur="adjustFormula(this.value)"></td>
     <td><input type="text" id="E9b" value=""></td>
    </tr></table>
    
    <script type="text/javascript">
    function adjustFormula(v) {
      v = Number(v);
      if (v >= 2) { document.getElementById('E9b').value = (v * 0.05).toFixed(2); }
             else { document.getElementById('E9b').value = 2; }
    }
    </script>
    
    </body>
    </html>

  5. #5
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,390

    Question

    Response in RED below ...
    Quote Originally Posted by pc_mac View Post
    JMRKER thanks so much for your help!!

    There was a mistake in my formula! Sorry about that it was supposed to be:
    =IF((E9*0.05)>2, E9*0.06, 2)
    So just alter the function.

    I have the formula answer going into a field called "result".
    You will need to show YOUR code. I assume the 'E9b' would be your 'result' field.
    Is the figure entered into E9a going into 'v' variable? Yes

    Many thanks for your help again...
    Hope you can help me further as I cannot get the formula to work!
    What is wrong with your code. What errors are you getting in the error console?
    The program works fine as published in post #3. What are you using to trigger the event?
    Without seeing what you are doing, I'm no better at giving a haircut through the mail than I am at debugging your code!

  6. #6
    Join Date
    Dec 2009
    Posts
    51
    Quote Originally Posted by JMRKER View Post
    Response in RED below ...

    What is wrong with your code. What errors are you getting in the error console?
    The program works fine as published in post #3. What are you using to trigger the event?
    Without seeing what you are doing, I'm no better at giving a haircut through the mail than I am at debugging your code!
    I'm not getting any errors - nothing happens when I try to add the "E9*0.06, 2" in to the javascript code.

    Where would I add this code?

    I'm not fully understanding how this Javascript code works the same as the excel formula. Sorry, I hope you don't mind helping me out.

    This was my 1st attempt:

    if (parseInt(form. E9.value) * 0.05 > 2)
    { form.result.value = parseInt(form. E9.value) * 0.06 }
    else { form.result..value = 2 }


    Thanks again
    Last edited by pc_mac; 06-24-2013 at 11:24 AM.

  7. #7
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,390

    Exclamation

    Show your code!

    "E9" is not an element in the code I provided.
    It does not use a <form> tag.

    In some places you use form.result.value, in other you use form. E9.value (note the space)
    and in still another you use form.result..value ( note the double dots). Cannot be all three syntax correct.
    Note also that you do not define the form in your snippet.

    Show your code!
    And look at your error console.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles