Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

JavaScript to Sum multiple fields

New Here ,
Apr 07, 2021 Apr 07, 2021

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.

TOPICS
JavaScript
6.0K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 08, 2021 Apr 08, 2021

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...

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Apr 12, 2021 Apr 12, 2021

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Apr 12, 2021 Apr 12, 2021

Can you share your file or tell us what format you use for hours and fields names?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Apr 12, 2021 Apr 12, 2021

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Apr 12, 2021 Apr 12, 2021

You input hours with decimal, like this 10.00 or just 10? and 8.35 you use decimal?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Apr 12, 2021 Apr 12, 2021

10.00 with the decimal.  That's just in formatting though so wouldn't be in the script would it?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Apr 12, 2021 Apr 12, 2021

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?

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Apr 14, 2021 Apr 14, 2021

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Apr 15, 2021 Apr 15, 2021

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 15, 2021 Apr 15, 2021

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);
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Apr 15, 2021 Apr 15, 2021

OMG that works...thank you so much!  Eternally grateful.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Apr 15, 2021 Apr 15, 2021

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???

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 15, 2021 Apr 15, 2021

Why not? If the times are consistently displayed as decimal numbers then it is correct, and they seem to be happy with the outcome...

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Apr 15, 2021 Apr 15, 2021

Your script is fine, I just mean it's not way to calculate time.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jun 24, 2021 Jun 24, 2021

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jun 24, 2021 Jun 24, 2021
LATEST

The multiplication operator in JS is "*", not "x".

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines