Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Web Programming > JavaScript > Javascript How-To
| Search | Today's Posts | Mark Forums Read
Javascript How-To Ask your "How do I do this with Javascript?" questions here.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Javascript How-To section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old March 6th, 2008, 12:34 PM
Registered User
 
Join Date: Mar 2008
Location: , , .
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default convert Excel formula to JavaScript

I'm trying to convert the following Excel formula to JavaScript:

=IF(C6>=500, C6*0.9,"")

I am trying to take the total of one column (C6) and if it is greater than 500, multiply it by .90 (and put that quantity in a new column).

I am creating a fillable pdf in Adobe Acrobat that will do calculations. It requires some JavaScript.

thank you
  #2 (permalink)  
Old March 6th, 2008, 01:03 PM
joefawcett's Avatar
Wrox Author
Points: 9,763, Level: 42
Points: 9,763, Level: 42 Points: 9,763, Level: 42 Points: 9,763, Level: 42
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Exeter, , United Kingdom.
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

The JavaScript equivalent is the ternary conditional operator:
expression ? when true : when false. So
Code:
var c6 = <value in cell C6>;
var result = (c6 >= 500) ? c6 * 0.9 : "";
--

Joe (Microsoft MVP - XML)
  #3 (permalink)  
Old March 6th, 2008, 01:50 PM
Registered User
 
Join Date: Mar 2008
Location: , , .
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thank you for your quick response. when I put the following in, I am getting a syntax error.

var C6 = <value in cell C6>;
var result = (C6 >= 500) ? C6 * 0.9 : ""

if (C6>=500) {
    C6 * 0.9 : "";
  }

I am just learning JavaScript so it may be something very basic and simple. Thank you for your time.
  #4 (permalink)  
Old March 7th, 2008, 06:31 AM
joefawcett's Avatar
Wrox Author
Points: 9,763, Level: 42
Points: 9,763, Level: 42 Points: 9,763, Level: 42 Points: 9,763, Level: 42
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Exeter, , United Kingdom.
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

The items delimited by <> are meant to be replaced by your code, I can't tell how you get the initial value of cell C6 into a variable.
You don't need
Code:
if (C6>=500) {
    C6 * 0.9 : "";
  }

The variable 'result' holds the correct value, you need to put that into your column.

--

Joe (Microsoft MVP - XML)


Similar Threads
Thread Thread Starter Forum Replies Last Post
Excel to Javascript formula assistance recei Javascript How-To 1 February 6th, 2007 10:25 AM
Excel calculations convert to pdf via javascript evetteogden Javascript How-To 0 May 31st, 2006 11:32 AM
Formula in Excel davekrunal46 Excel VBA 1 December 5th, 2005 06:51 AM
Formula to convert date format narooma.12 Crystal Reports 0 April 20th, 2005 04:54 PM
converting excel formula to javascript bRvO Javascript How-To 10 January 10th, 2005 12:41 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.