JavaScript to Sum multiple fields
Copy link to clipboard
Copied
Hi everyone. Needing help with a script.
I would like for my timesheet to total the hours worked (Regular Hours + Sick Hours) ONLY if there is information in the date field, and if not leave the total hours blank (maybe they don't work on Mondays for instance). I know in Excel it would be =IF(Date>0,Reg Hrs + Sick Hrs,""). Any help is greatly appreciated. Thanks.
Copy link to clipboard
Copied
Date and time calculations in Acrobat JS are not trivial. Do you have any experience with writing scripts for PDF files?
You can try searching these forums, as this issue was discussed many times in the past and there are some code examples and tutorials on the subject. Making the result dependent on the value of another field is actually the easy part...
Copy link to clipboard
Copied
Unfortunately, I have no experience with it. I've tried to follow the logic and tried to search for how to write script but I guess my brain just won't compute. I've also tried searching the forums for how to accomplish what I am trying to do but can't seem to find the exact answer. I will try searching again. Thanks.
Copy link to clipboard
Copied
Can you share your file or tell us what format you use for hours and fields names?
Copy link to clipboard
Copied
File attached. I want the Total Hours to add Regular Hours + Sick Hours only if there is a date on that line...otherwise I would like it to remain blank. Does that help? Thank you so much for your help.
Copy link to clipboard
Copied
You input hours with decimal, like this 10.00 or just 10? and 8.35 you use decimal?
Copy link to clipboard
Copied
10.00 with the decimal. That's just in formatting though so wouldn't be in the script would it?
Copy link to clipboard
Copied
I see you have fields formated with 2 decimal, that why I ask how you enter because it's not same in script if you enter 10.00 or 10 especially when calculating time, and in another example lets say 8.35 you need to use decimal, so that is two different input method you use?
Copy link to clipboard
Copied
All of the fields that I want to calculate will be formatted with two decimals. Thanks for your help with this. I really appreciate it.
Copy link to clipboard
Copied
I haven't received a definite response and was hoping that someone out there would be able to help. I'm hoping to get this file sent out soon. Thanks.
Copy link to clipboard
Copied
If those fields are just decimal numbers then you can use something like this for the first row, as the custom calculation script of "Wk1 Total Hrs Sat":
if (this.getField("Wk1 Date Sat").valueAsString=="") event.value = "";
else event.value = Number(this.getField("Wk1 Reg Hrs Sat").valueAsString) + Number(this.getField("Wk1 Sick Hrs Sat").valueAsString);
Copy link to clipboard
Copied
OMG that works...thank you so much! Eternally grateful.
Copy link to clipboard
Copied
Thats not the way to calculate hours, thats just adding two decimal numbers together,
What if your time is 2.31 and 2.31 it's 4.62h???
Copy link to clipboard
Copied
Why not? If the times are consistently displayed as decimal numbers then it is correct, and they seem to be happy with the outcome...
Copy link to clipboard
Copied
Your script is fine, I just mean it's not way to calculate time.
Copy link to clipboard
Copied
Thank you again for your solution...I tried to use this script in a similar situation and can't get it to work so I'm not sure what I am doing wrong. I want it to again look to see if the date is filled in and if so take the total number of hours and multiply it by the pay rate. Here is what I did:
if (this.getField("Date1").valueAsString=="") event.value = "";
else event.value = Number(this.getField("Hours1").valueAsString) x Number(this.getField("Pay Rate1").valueAsString);
Date is formated as Date and then Hours, Pay Rate and Total are all formatted as Number.
Any help is again appreciated. Thanks.
Copy link to clipboard
Copied
The multiplication operator in JS is "*", not "x".

