Skip to main content
Bloody_Lemming
Inspiring
October 18, 2020
Answered

Override Values in Calculating Field

  • October 18, 2020
  • 3 replies
  • 3743 views

I have fields that are filled automatically from a combobox. I would like the user to be able to override these fields. Is this possibe without adding another field to handle the users input? As it stands, when I enter a custom value, it just reverts to the calculated value. 

This topic has been closed for replies.
Correct answer Nesa Nurani

Try something like this then:

var drop = this.getField("Dropdown1").valueAsString;
if(drop == "One" || drop == "Two"){
event.value = drop;}
else event.rc = false;

 

I don't know what items you have in dropdown so

change "One" "Two" with dropdown items you have and add more if you have .

3 replies

Bloody_Lemming
Inspiring
October 22, 2020

@ls_rbls Currently the only plaec I've been able to successfully use changeEx is under the combobox Custom Keystroke Script.

 

// Get references to our form fields.
var name = this.getField("field.1")

// Is the new export value a keyword in global table?
if (event.changeEx in gTable)
name.value = gTable[event.changeEx].split('--')[0] // Yes: Split the string up and get the value at position 0

                                                                                  // and assign it to field.1

ls_rbls
Community Expert
Community Expert
October 26, 2020

 +++EDITED REPLY, did some corrections in the custom format script

 

Well, I've tried everything I could think of with my current level of JavaScript scripting.

 

I don't think that you're using the changeEx method appropriately. 

 

If I use the following :

 

  • Declare the global variable in a function using a literal Array method instead of "new Array(), "new Array", "Array" methods:

 

 

 

function testTable() {

gTable =   [

[" ", "-- -- --"],

["Item 1", "1H--25--3"],

["Item 2", "2H--17--4"],

["Item 3", "3--1H--9--7"]

] 


}

 

 

 

 

  • And then add a Custom Format script to the combobox to call that function:

 

 

 

 

testTable();

var f = this.getField("select.01");

f.setAction("MouseUp", "f.setItems(gTable);");


 

 

 

 

  •  And a custom Keystroke script in the same combobox:

 

 

 


if (event.willCommit) {

this.getField("field.01").value = event.value;

} else {

this.getField("field.02").value = event.changeEx.split("--")[0];
this.getField("field.03").value = event.changeEx.split("--")[1];
this.getField("field.04").value = event.changeEx.split("--")[2];

} 

 

 

 

 

It leads back to the same problem that you mentioned. You will get errors and undefined values wehn the commited and not commited values are processed., Also, it won't let you revert to the prior value when it is deleted in any of the text fields.

 

To work around this you may have to use the Adobe Acrobat API Reference which explain in full detail the differences of event.value, event.change, and event.changeEx when they're used in custom keystroes and  text boxes.

 

If you noticed in my script above there is no need to split the value that you want in field 1. This line alone does it:

 

this.getField("field.01").value = event.value;

 

That said, if you still prefer to use the code that I posted in my prior reply,  all you need to do is add this line

 

 

 

 

this.getField("field.01").value = gTable[b].split("--")[0];

 

 

 

 

into this script:

 

 

 

 

var f = this.getField("select.01");
var a = f.currentValueIndices;
var b =  f.getItemAt(a, false);
var c =  event.target.value;

if(event.source&&event.source.name=="select.01") {

if ( (c =="Item 1") || (c == "Item 2") || (c == "Item 3") ) {

this.getField("field.01").value = gTable[b].split("--")[0];
this.getField("field.02").value = gTable[b].split("--")[1];
this.getField("field.03").value = gTable[b].split("--")[2];
this.getField("field.04").value = gTable[b].split("--")[3];

} else {

if ( (c !=="Item 1") || (c !== "Item 2") || (c !== "Item 3") || (c =="") ) {

this.getField("field.01").value = this.getField("field.01").target.value;
this.getField("field.02").value = this.getField("field.02").target.value;
this.getField("field.03").value = this.getField("field.03").target.value;
this.getField("field.04").value = this.getField("field.04").target.value;

    }
  }
}
 

 

 

 

 

Nesa Nurani
Community Expert
Community Expert
October 18, 2020

Use this as custom calculation script of text field:

if(event.source&&event.source.name=="Dropdown1"){
event.value = this.getField("Dropdown1").value;}

 

Change "Dropdown1" to your "Combobox" name.

Bloody_Lemming
Inspiring
October 18, 2020

That works, if the combobox entry is overridden, but I need to be able to override the value in a field that gets calculated based on the selection in that combobox. So, I guess, the combobox is irrelevant to the issue...

 

Essentially I need:

 

if (fieldA == manual entry) {

     event.value = manual entry;

}

else { event.value = calculated entry; }

ls_rbls
Community Expert
Community Expert
October 19, 2020

That is wehere I started. I abandoned this when it came clear that if I changed anything in a table, I woud have to manually chage each pulldown menu that used that data. That's why I went for the global table instead. This way I would have a central place to edit and add to the list of items and I would only have to update the entry names on the pulldown menus. I hope to learn at some point to create dynamicly populating pulldown menus, so I can maintian the data in one spot, and hopefully never have to touch the pulldown menus again.

 

This works, however there are two remaioning issues with this. The first is, if I make any chages to the pulldown entry, everything resets. The other issue is, if an entry is cleared, it remains blank, instead of reverting back to the calculated value.

 

I know how to get around this by having a sencond field for each one, to handle the user input. I was hoping there was a more elegant solution.


To create dynamically populating dropdowns you may need to use a combination of an array .push method in combination with getItemAt (to get the face value of the selected item ; not the export value), and currentValueIndices (to get the current value selected).

 

This may not be a trivial task, but since you've mentioned about elegance these are the methods and of course some conditional statements will be necessary.

 

This is explained with examples in the Adobe Acrobat SDK JavaScript API Reference,  "Field methods" Page 417.

 

 

Inspiring
October 18, 2020

How do you get value from dropdown field? If you use code pls post it here.