JavaScript to Sum multiple fields

New Here ,
Apr 07, 2021 Apr 07, 2021

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.

TOPICS
JavaScript

Views

389

Likes

Translate

Translate

Report

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
Most Valuable Participant ,
Apr 08, 2021 Apr 08, 2021

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

Likes

Translate

Translate

Report

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

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.

Likes

Translate

Translate

Report

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
Engaged ,
Apr 12, 2021 Apr 12, 2021

Copy link to clipboard

Copied

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

Likes

Translate

Translate

Report

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

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.

Likes

Translate

Translate

Report

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
Engaged ,
Apr 12, 2021 Apr 12, 2021

Copy link to clipboard

Copied

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

Likes

Translate

Translate

Report

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

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?

Likes

Translate

Translate

Report

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
Engaged ,
Apr 12, 2021 Apr 12, 2021

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?

 

Likes

Translate

Translate

Report

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

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.

Likes

Translate

Translate

Report

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

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.

Likes

Translate

Translate

Report

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
Most Valuable Participant ,
Apr 15, 2021 Apr 15, 2021

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);

Likes

Translate

Translate

Report

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

Copy link to clipboard

Copied

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

Likes

Translate

Translate

Report

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
Engaged ,
Apr 15, 2021 Apr 15, 2021

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

 

Likes

Translate

Translate

Report

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
Most Valuable Participant ,
Apr 15, 2021 Apr 15, 2021

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

Likes

Translate

Translate

Report

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
Engaged ,
Apr 15, 2021 Apr 15, 2021

Copy link to clipboard

Copied

LATEST

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

Likes

Translate

Translate

Report

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