Skip to main content
Inspiring
February 5, 2025
Question

Help with calculations script

  • February 5, 2025
  • 2 replies
  • 571 views

Hi. I have a script for calculations that are giving me a problem.  I enter time 1 and the other 3 times are calculated.  I can also, edit times and the following times are calculated.  The problem I am having is that Time 4 calculation doesn't always register.  I have to delete and reenter the prior times to get it to calculate.  Can someone recommend a solution? Thanks  

 

 

//start of code for when end time entered manually

var start = this.getField("end").valueAsString;

if (start=="") event.value = "";
else {
	
var t1c = Number(start.substr(0,2)); 
var t2c = Number(start.substr(2,2));
var hrc = t1c;
var minc = t2c;


if(t2c > 30){
    hrc = t1c + 1;
    minc = "0" + ((t2c - (-30) - 60));
}
if(t2c == "30"){
    hrc = t1c + 1;
    minc = "00";
}
if(t2c < 30){
    hrc = t1c;
    minc = t2c + 30;		
}
if(t2c > 40){
    hrc = t1c + 1;
    minc = ((t2c - (-30) - 60));
}
if(t2c == "40"){
    hrc = t1c + 1;
    minc = ((t2c - (-30) - 60));
}

var hr2c = hrc;
var min2c = minc;

if(hr2c < 10){
  hr2c = "0" + hrc;
  min2c = minc;
}
if(hr2c > 10){
  hr2c = hrc;
  min2c = minc;
}


//start of code for when finish time entered manually

var start = this.getField("finish").valueAsString;

if (start=="") event.value = "";
else {

var t1b = Number(start.substr(0,2)); 
var t2b = Number(start.substr(2,2));
var hrb = t1b;
var minb = t2b;

if(t2b > 29){
    hrb = t1b + 1;
    minb = "0" + ((t2b - (-31) - 60));
}
if(t2b == "29"){
    hrb = t1b + 1;  
    minb = "00";
}
if(t2b < 29){
    hrb = t1b;
    minb = t2b + 31;
}
if(t2b > 39){
    hrb = t1b + 1;
    minb = ((t2b - (-31) - 60));
}
if(t2b == "39"){
    hrb = t1b + 1;
    minb = ((t2b - (-31) - 60));
}

  var hr2b = hrb;
  var min2b = minb;

if(hr2b < 10){
  hr2b = "0" + hrb;
  min2b = minb;
}
if(hr2b > 10){
  hr2b = hrb;
  min2b = minb;
}




//start of code for when procend stop time entered manually

var start = this.getField("procend").valueAsString;

if (start=="") event.value = "";
else {
	
var t1 = Number(start.substr(0,2)); 
var t2 = Number(start.substr(2,2));
var hr = t1;
var min = t2;


if(t2 > 28){
    hr = t1 + 1;
    min = "0" + ((t2 - (-32) - 60));
}
if(t2 == "28"){
    hr = t1 + 1;  
    min = "00";
}
if(t2 < 28){
    hr = t1;
    min = t2 + 32;
}
if(t2 > 38){
    hr = t1 + 1;
    min = ((t2- (-32) - 60));
}
if(t2 == "38"){
    hr = t1 + 1;
    min = ((t2- (-32) - 60));
}

  var hr2 = hr;
  var min2 = min;

if(hr2 < 10){
  hr2 = "0" + hr;
  min2 = min;
}
if(hr2 > 10){
  hr2 = hr;
  min2 = min;
}

var final = hr2.toString() + min2.toString() ;

if (event.source === this.getField("procend")){
        event.value = final;
}        
}

//end of code for when procend time entered manually


var finalb = hr2b.toString() + min2b.toString() ;

if (event.source === this.getField("finish")){
        event.value = finalb;
}        
}

//end of code for when finish time entered manually


var final = hr2c.toString() + min2c.toString() ;

if (event.source === this.getField("end")){
        event.value = final;
}       
}

//end of code for when end time entered manually

 

2 replies

Thom Parker
Community Expert
Community Expert
February 6, 2025

You have a calculation order problem. And checking the source to block the calculation is inefficient, use event.rc.

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
Inspiring
February 6, 2025

I don't understand the comment: checking the source to block the calculation is inefficient.  Also, I've already placed the code to be able to overwrite in custom format.  That part works fine.

Thom Parker
Community Expert
Community Expert
February 6, 2025

The correct method for overridding a calculation so the field value can be entered manually is to set event.rc to false. 

 

 

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
PDF Automation Station
Community Expert
Community Expert
February 6, 2025

You can't manually enter values into fields that are calculated because the value will always revert to the calculated value.

Inspiring
February 6, 2025

I wrote the code to be able to overwrite the calculations. But, the calculation and overwriting works fine.  I am having an issue with the calculation of Time 4 not popping up untill I delete and reenter Time1.

PDF Automation Station
Community Expert
Community Expert
February 6, 2025