Skip to main content
emilym97521869
Participant
June 29, 2016
Question

Custom calculation script to ignore blank fields

  • June 29, 2016
  • 3 replies
  • 2812 views

Hello there,

I would love some help in finding out what is not working with my calculation code.

I have 10 fields that I want to add together.  The tricky bit is that the fields data is an smpte code in a hh:mm:ss:ff format so I can't do a standard calculation.

I have the calculation working to subtract one fields value with another (with help previously on here) and I have tweaked the code for this new function.  However the calculation only seems to appear in the required field when I delete the last fields entry - very odd as still includes this deleted data in the calculation?

Not sure how to solve this.

In addition I also need the calculation field to ignore any blank fields if any of the ten fields don't have any value in.

Appreciate any guidance.

I have pasted the code below - This is entered into the 'calculation fields' custom calculation script window.  Using Acrobat Pro XI

var t1 = this.getField("P1.Spawn page.P1.Actions page.CUT 1").value;

var t2 = this.getField("P1.Spawn page.P1.Actions page.CUT 2").value;

var t3 = this.getField("P1.Spawn page.P1.Actions page.CUT 3").value;

var t4 = this.getField("P1.Spawn page.P1.Actions page.CUT 4").value;

var t5 = this.getField("P1.Spawn page.P1.Actions page.CUT 5").value;

var t6 = this.getField("P1.Spawn page.P1.Actions page.CUT 6").value;

var t7 = this.getField("P1.Spawn page.P1.Actions page.CUT 7").value;

var t8 = this.getField("P1.Spawn page.P1.Actions page.CUT 8").value;

var t9 = this.getField("P1.Spawn page.P1.Actions page.CUT 9").value;

var t10 = this.getField("P1.Spawn page.P1.Actions page.CUT 10").value;

if (t1 != 0 && t2 != 0 && t3 != 0 && t4 != 0 && t5 != 0 && t6 != 0 && t7 != 0 && t8 != 0 && t9 != 0 && t10 != 0) {

  event.value = frames_to_timecode (timecode_to_frames(t1) + timecode_to_frames(t2) + timecode_to_frames(t3) + timecode_to_frames(t4) + timecode_to_frames(t5) + timecode_to_frames(t6) + timecode_to_frames(t7) + timecode_to_frames(t8) + timecode_to_frames(t9) + timecode_to_frames(t10));

}

else {

    event.value = "";

}

I also have the following (again thanks to assistance on here) pasted into the document level scripts to work out the smpte code format.

var framerate = 24

function timecode_set_framerate(rate) {

  framerate = rate;

}

function timecode_get_framerate() {

  return framerate;

}

function timecode_to_frames(timecode) {

    var a = timecode.split(':');

    return ((Number(a[0])*3600 + Number(a[1])*60 + Number(a[2]))*framerate + Number(a[3]));

}

function frames_to_timecode(frames) {

  return util.printf("%02d:%02d:%02d:%02d",

    Math.floor(frames / (3600 * framerate)),

    Math.floor((frames / (60 * framerate)) % 60),

    Math.floor((frames / framerate) % 60),

    frames % framerate);

}

This topic has been closed for replies.

3 replies

Al_Nikoros
Participant
July 30, 2016

Hello guys! Need your help please! Here is a similar problem.

I have a 3 fields: A1; B1; C1 - I have to multiply A1 and B1 and result will be in C1. But I don't want to see a "0" result in C1 because in some A's or B's will be empty. I have no idea about javascripting so please as simple as possible!

Thank you for your help!

Inspiring
July 30, 2016

Do you know how to do this in Excel, especially for division?

I would look at using the "Validation" tab in the "Properties" pop-up window for the result field and use a script like:

// if field value is zero, replace with a null string;

if(event.value == 0) event.value = "";

Al_Nikoros
Participant
July 30, 2016

It works!!!! Thanks a lot!!! You have saved my life

In excel it was easy! But I've decided to do a same form in pdf, so I can use it on mobile devices... And then this problem came out!

Thank you!!!

emilym97521869
Participant
June 30, 2016

Thank you - I really appreciate your help with this.  I have put the script you supplied into a sample form and here is a link:

https://www.yousendit.com/download/cUJYS3duTkFEbUxMYnRVag

Many thanks

Sarah

Inspiring
June 29, 2016

You should use "valueAsString" instead of "value" and then use the comparison != "" to remove null fields values.

If you can use arrays for the values you can then filter out the null strings.

emilym97521869
Participant
June 29, 2016

amazing! It works - so simple, thank you!

emilym97521869
Participant
June 29, 2016

Oh - spoke to soon.  More testing required, not working at all now!!

So I have tried various different ways of switching 'value' for 'valueAsString' and using the comparison !="" but I still don't get a result unless all the fields have data in them?