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

Calculate additional text fields

Engaged ,
May 10, 2024 May 10, 2024

I'm using the following custom calculation script in a text field named 'TotalOTHrs' and it works great. However, I also need to calculate the hours from another 35 dropdowns name 'MBHrs1' to 'MBHrs35.   I'm strugging with how to input that and I could sure use some much appreciated help.  Thanks.

 

var maxFields = 35; //
var total = 0;
for (var i=1; i<=maxFields; i++) {
if (this.getField("OTPayType"+i).valueAsString!="CDO Banked")
total+=Number(this.getField("OTHrs"+i).valueAsString);
}
event.value = total;

TOPICS
JavaScript , PDF forms
954
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
1 ACCEPTED SOLUTION
Community Expert ,
May 10, 2024 May 10, 2024

So if value from dropdown is not "CDO Banked" you want to calculate both "OTHrs" and "MBHrs"?

I ask because in your two posts you used different condition in first you use != "CDO Banked" and in second == "CDO Banked", so which one is it?

If OTHrs and MBHrs are in the same row for each dropdown, you can just sum them, like this:

total+=Number(this.getField("OTHrs"+i).valueAsString)+Number(this.getField("MBHrs"+i).valueAsString);

 

 

 

View solution in original post

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
Adobe Employee ,
May 10, 2024 May 10, 2024

Hi there

Hope you are doing well and thanks for reaching out.

The workflow you are trying to achieve is might be possible using JavaScript. For more information, please check the help pages listed below:
https://acrobatusers.com/tutorials/javascript_console/
https://helpx.adobe.com/acrobat/using/applying-actions-scripts-pdfs.html

Hope it will help

Regards
Amal

Regards
Amal
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
Engaged ,
May 10, 2024 May 10, 2024

I definitely need help with the script above so the total hours can calculate from both OTHrs and MBHrs into the text field 'TotalOTHrs', except when the user selects 'CDO Banked' from the OT PayType dropdown.  Please help.

 

Note:  I also have another text field named 'CDOBankedHrs' with the following custom calculation script, which is good.

var total = 0; for (var i=1; i<=35; i++) { if (this.getField("OTPayType"+i).valueAsString=="CDO Banked") total+=Number(this.getField("OTHrs"+i).valueAsString); } event.value = total;

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 ,
May 10, 2024 May 10, 2024

So if value from dropdown is not "CDO Banked" you want to calculate both "OTHrs" and "MBHrs"?

I ask because in your two posts you used different condition in first you use != "CDO Banked" and in second == "CDO Banked", so which one is it?

If OTHrs and MBHrs are in the same row for each dropdown, you can just sum them, like this:

total+=Number(this.getField("OTHrs"+i).valueAsString)+Number(this.getField("MBHrs"+i).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
Engaged ,
May 11, 2024 May 11, 2024

Yes!  That's exactly what I need!  It works great now.  Thank you Nesa!!  You're the best 🙂

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
Engaged ,
May 11, 2024 May 11, 2024

Nesa, I forgot something.  I don't want the hours to calculate from OTHrs and MBHrs if 'Shift Premium 50%' is selected from the OTPayType dropdown, as well as 'CDO Banked' which is there now.  How do I add that?

//Construction Total OTHrs
var maxFields = 35; //
var total = 0;
for (var i=1; i<=maxFields; i++) {
if (this.getField("OTPayType"+i).valueAsString!="CDO Banked")
total+=Number(this.getField("OTHrs"+i).valueAsString)+Number(this.getField("MBHrs"+i).valueAsString);
}
event.value = total;

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
Engaged ,
May 11, 2024 May 11, 2024

Hi Nesa,  I got it to work!

 

var maxFields = 35; // 
var total = 0;
for (var i=1; i<=maxFields; i++) {
if (this.getField("OTPayType"+i).valueAsString!="CDO Banked")
if (this.getField("OTPayType"+i).valueAsString!="Shift Premium 50%")
total+=Number(this.getField("OTHrs"+i).valueAsString)+Number(this.getField("MBHrs"+i).valueAsString);
}
event.value = total;

 

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 ,
May 11, 2024 May 11, 2024

Glad you make it work, but your conditions may be a bit confusing, you can do it like this:

 

var total = 0;
for (var i=1; i<=35; i++) {
 var drop = this.getField("OTPayType"+i).valueAsString;
 var ot = Number(this.getField("OTHrs"+i).valueAsString);
 var mb = Number(this.getField("MBHrs"+i).valueAsString);

if (drop != "CDO Banked" && drop != "Shift Premium 50%")
 total += ot+mb;}

event.value = total;

 

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
Engaged ,
May 11, 2024 May 11, 2024

Awesome!!  Thanks Nesa!!

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
Adobe Employee ,
May 13, 2024 May 13, 2024
LATEST

Hi there

 

We are glad to hear that.

 

Marking a reply or response “Correct” will help future users with the same issue quickly identify the correct answer.

 

~Amal

Regards
Amal
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