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

Javascript code for a PDF Form

New Here ,
Nov 07, 2022 Nov 07, 2022

Greetings,

 

I'll start by saying that I'm not a computer coder by any means, however I have been able to search and almost find a solution to my task.  I'm not quite there yet and I don't understand enough of it to figure it out unfortunately.  I don't think what I'm trying to accomplish is very difficult either.  Here is my setup.

 

I have a pdf form with three columns. st1 + st2 + st3 = ttm(hidden cell) which is then converted to HH:MM in tt1 with this javascript code.

var v = Number(this.getField("ttm").valueAsString);
var hours = Math.floor(v/60);
var minutes= (v%60);
var minutesString = (minutes<10) ? "0"+minutes :  ""+minutes;
event.value = hours + ":" + minutesString;

 

That script works, however it is one step behind in calculations.  The ttm is updated whenever a value is entered in stx but the javascript code won't update unless something else is changed in the form.  The whole point of creating the hidden (ttm) was as a work around since I couldn't figure out the sum of fields in javascript and then assign that sum to var v. The javascript code should ideally calculate that sum and then apply the code to convert it into HH:MM format.

 

The next part of the form is column 3, Part 91 y1,y2,y3), which ideally would be a checkmark or I can make it into a text field.  The goal for that column is if a check mark or value is entered then that row would not be included in the calculation for tt1.

test.jpgexpand image

 

I have this working code that does what I want it to do, I just don't know how to modify it to incorporate it into my pdf form. 

 

var st1 = 60;
var st2 = 20;
var st3 = 50;
var y1 = '';
var y2 = '';
var y3 = 'y';

var v = 0;
for (x=1; x<4; x++) {
if (eval("y"+x).length === 0) { 
v += eval("st"+x);
}
}
var hours = Math.floor(v/60);
var minutes = (v%60);
var minutesString = (minutes<10)? "0"+minutes: ""+minutes;
var final = hours + ":" + minutesString;
console.log(final);

I've included the sample pdf that I've referenced so that it can be viewed in case my explanations were a bit vague.  Any assistance would be greatly appreciated.

 

Thanks,

TOPICS
JavaScript , PDF forms
2.2K
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 ,
Nov 07, 2022 Nov 07, 2022

Remove "ttm" field and delete every other script and use this as custom calculation script in "tt1" field:

var num = 0;
var x = 0;
for (var i=1; i<=3; i++) {
if(this.getField("st"+i).valueAsString != "" && this.getField("y"+i).valueAsString == "Off")
num += Number(this.getField("st"+i).valueAsString);
if(this.getField("L"+i).valueAsString != "" && this.getField("y"+i).valueAsString == "Off")
x += Number(this.getField("L"+i).valueAsString);}

function timeConvert() {
var hours = (num / 60);
var rhours = Math.floor(hours);
var minutes = (hours - rhours) * 60;
var rminutes = Math.round(minutes);
if(rminutes < 10)
return rhours + ":" + "0"+ rminutes;
else return rhours + ":" + rminutes;
}

event.value = timeConvert();
this.getField("TL1").value = x;

 

Here is your file with changes already made:

https://drive.google.com/file/d/1y7SCBERvyMPOaQe5cfZoxqAESqUZGpoP/view?usp=share_link 

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
Community Expert ,
Nov 07, 2022 Nov 07, 2022

Looks like you have issue with field calculation order.

Select 'Prepare form' tool, click on 'More' then select 'Set field calculation order' make sure that fields that calculate first are on top. For example, your field "ttm" should be above field "tt1" in field calculation order.

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 ,
Nov 07, 2022 Nov 07, 2022

Remove "ttm" field and delete every other script and use this as custom calculation script in "tt1" field:

var num = 0;
var x = 0;
for (var i=1; i<=3; i++) {
if(this.getField("st"+i).valueAsString != "" && this.getField("y"+i).valueAsString == "Off")
num += Number(this.getField("st"+i).valueAsString);
if(this.getField("L"+i).valueAsString != "" && this.getField("y"+i).valueAsString == "Off")
x += Number(this.getField("L"+i).valueAsString);}

function timeConvert() {
var hours = (num / 60);
var rhours = Math.floor(hours);
var minutes = (hours - rhours) * 60;
var rminutes = Math.round(minutes);
if(rminutes < 10)
return rhours + ":" + "0"+ rminutes;
else return rhours + ":" + rminutes;
}

event.value = timeConvert();
this.getField("TL1").value = x;

 

Here is your file with changes already made:

https://drive.google.com/file/d/1y7SCBERvyMPOaQe5cfZoxqAESqUZGpoP/view?usp=share_link 

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 ,
Nov 07, 2022 Nov 07, 2022
LATEST

Wow!  Thanks for the quick response and figuring it out for me.  I probably spent over a week trying to come up with a solution to get it to work.  Hopefully, I should be able to incorporate your script with slight modifications so that I can apply it to the actual document that has more than three rows.  It looks like I will just have to change, for (var i=1; i<=3; i++),  from 3 in the example to 20 or whatever the amount of rows are in the actual document I'm working on.

 

Again, thank you so much!

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