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

Help with calculations script

Explorer ,
Feb 05, 2025 Feb 05, 2025

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

 

TOPICS
Create PDFs , JavaScript , PDF , PDF forms
235
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 ,
Feb 05, 2025 Feb 05, 2025

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

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
Explorer ,
Feb 05, 2025 Feb 05, 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.

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 ,
Feb 05, 2025 Feb 05, 2025

Check the 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 ,
Feb 05, 2025 Feb 05, 2025

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

CalcOrder.png

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

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
Explorer ,
Feb 06, 2025 Feb 06, 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.

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 ,
Feb 06, 2025 Feb 06, 2025
LATEST

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 PDFScripting
Use the Acrobat JavaScript Reference early and often

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