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.
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.
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;
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.
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.
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.
Copy link to clipboard
Copied
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.