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

I Need to Have a Custom Script Which Terminates Calculations if No Input is Entered.

Community Beginner ,
Jul 01, 2024 Jul 01, 2024

Copy link to clipboard

Copied

I am creating a mileage log for our fleet vehicles. I know how to do basic calculations. What I need is a custom script that will terminate the calculations if no input is entered on the EndOdomReadingRow. Also I need it to calculate if there is input. Because if I go with the basic calculation EndOdomReadingRow3 - EndOdomReadingRow2  which equals MilesTraveledRow3 and there is no input in EndOdomReadingRow3 it puts a negative number in there. So far the only way I have to do it is to go in and correct every line in which there is no input. I know there must be an easier way. Any help will be greatly appreciated.

TOPICS
JavaScript , PDF forms

Views

243

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

correct answers 1 Correct answer

Community Expert , Jul 01, 2024 Jul 01, 2024

Yes, I missed a closing parentheses on the "if" condition. Here's the updated code

 

var nOdomRow3 = this.getField("EndOdomReadingRow3").valueAsString;
var nOdomRow2 = this.getField("EndOdomReadingRow2").valueAsString;
if(nOdomRow3 && nOdomRow3 && !isNaN(nOdomRow3) && !isNaN(nOdomRow2))
     event.value = Number(nOdomRow3) - Number(nOdomRow2);
else
     event.value = "";

 

If there is an error in the code, then everything after the error is meaningless, because code execution stops at the error. 

 

Votes

Translate

Translate
Community Expert ,
Jul 01, 2024 Jul 01, 2024

Copy link to clipboard

Copied

Here's a calculation script that checks the input values and sets the output to a blank if either input is empty, or zero, or a non-numeric value. 

 

var nOdomRow3 = this.getField("EndOdomReadingRow3").valueAsString;

var nOdomRow2 = this.getField("EndOdomReadingRow2").valueAsString;

if(nOdomRow3 && nOdomRow3 && !isNaN(nOdomRow3) && !isNaN(nOdomRow2)

      event.value = Number(nOdomRow3) - Number(nOdomRow2);

else

     event.value = "";

 

Another way to do this is to use a custom format script that sets the output to a blank if the calculation is <= 0, or not a number

 

Custom Format Script

 

event.value = (isNaN(event.value)|| (event.value <= 0))?"":event.value;

 

 

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

Votes

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
Community Beginner ,
Jul 01, 2024 Jul 01, 2024

Copy link to clipboard

Copied

var nOdomRow3 = this.getField("EndOdomReadingRow3").valueAsString;
var nOdomRow2 = this.getField("EndOdomReadingRow2").valueAsString;
if(nOdomRow3 && nOdomRow3 && !isNaN(nOdomRow3) && !isNaN(nOdomRow2)
event.value = Number(nOdomRow3) - Number(nOdomRow2);
else
event.value = "";

The first one said it had a SyntaxError: missing ) after condition 4: at line 5 and the second one does not subtract if there is input.

I know that it is close though. I appreciate your help and thank you so much.

Votes

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
Community Expert ,
Jul 01, 2024 Jul 01, 2024

Copy link to clipboard

Copied

Yes, I missed a closing parentheses on the "if" condition. Here's the updated code

 

var nOdomRow3 = this.getField("EndOdomReadingRow3").valueAsString;
var nOdomRow2 = this.getField("EndOdomReadingRow2").valueAsString;
if(nOdomRow3 && nOdomRow3 && !isNaN(nOdomRow3) && !isNaN(nOdomRow2))
     event.value = Number(nOdomRow3) - Number(nOdomRow2);
else
     event.value = "";

 

If there is an error in the code, then everything after the error is meaningless, because code execution stops at the error. 

 

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

Votes

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
Community Beginner ,
Jul 02, 2024 Jul 02, 2024

Copy link to clipboard

Copied

Thom,

Thank you so much that worked perfectly and I am now going to implement it for the other 24 lines. I truly appreciate your help with this. I looked everywhere online to find the code. If I have other questions I will be sure to let you know. Thank you again.

Votes

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
Community Expert ,
Jul 02, 2024 Jul 02, 2024

Copy link to clipboard

Copied

LATEST

I kinda thought this calc would be repeated.  The best approach for table calculations is to rewrite the code as a function that can be used for every line. Place the function in a document script and call it from the calculated field. That way the code is in one location and updates only require changes to a single location.  How the script is turned into a fucntion depends on how the associated fields are named. 

 

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

Votes

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