Skip to main content
Participating Frequently
October 11, 2020
Answered

Calculation formula for multiple checkbox options

  • October 11, 2020
  • 2 replies
  • 4227 views

I am creating a form (using Acrobat Pro DC) that has multiple checkboxes set up along an X and Y access, mapping to a matrix of values.  The person MUST select one checkbox in Row 1, Row 2, Row 3, Row 4.  They then MUST select one check box in Column 1, Column 2 or Column 3.  I want to set up a custom calculation script that "sees" the selection combination (e.g. Row X with Column X) and auto-fills with value in that intersection location in the matrix.  The values in the matrix are fixed.   I really don't know Javascript, but I've had some success using it by adapting other Community post suggestions to create custom calculations...but for this particular situation I have not been able to find anything close enough to successfully adapt.   I have attached a simple mockup for easier visualization of the situation.  Any help would be greatly appreciated!

    This topic has been closed for replies.

    2 replies

    Nesa Nurani
    Inspiring
    October 12, 2020

    Hi, I made example file can you see if thats what you looking for? Code is in first text field custom calculation script.

    https://drive.google.com/uc?export=download&id=11m-Q-ZsctzUovS192lvYsg3NEFYWo8Oq 

    Participating Frequently
    October 12, 2020

    Thank for preparing this...I have pasted a sample in the thread of try67.  

    Nesa Nurani
    Nesa NuraniCorrect answer
    Inspiring
    October 12, 2020
    ls_rbls
    Braniac
    October 11, 2020

    There can be different valid ways to get this to work with Acrobat JavaScript

     

    But in my case, I would  run a conditional custom calculation script from each of the fields that you want to populate with a pre-defined value, not from the checkboxes.

     

    For example, format these fields as "Number" with currency $ symbol using the field properties "Format" tab--->>>"Number". Then add a conditional script  on each fields that will populate a value only if the checked state of both of the corresponding checkboxes are not in the "Off" state. Using this as a rule of thumb for your form is a lot easier than other methods in my opinion.

     

    See how I renamed the fields in my slide using your PDF template as example:

     

     

    Following up with the field names used in my example then you can use something like the script below for the corresponding fields under each of the Insurance columns :

     

     

     

    //custom calulcation script for the fields under column Insurance 1
    
    var single = this.getField("Single").value;
    var person = this.getField("Two Person").value;
    var family = this.getField("Family").value;
    var ins1 = this.getField("Insurance 1").value;
    var ins2 = this.getField("Insurance 2").value;
    var ins3 = this.getField("Insurance 3").value;
    
    // for field "value1"
    if (single && ins1 !=="Off") event.value = 10;
    else event.value = ""; 
    
    //for field value2
    if (person && ins1 !=="Off") event.value = 10;
    else event.value = ""; 
    
    //for field value3
    if (family && ins1 !=="Off") event.value = 10;
    else event.value = ""; 
    
    
    //custom calulcation script for the fields under column Insurance 2
    
    var single = this.getField("Single").value;
    var person = this.getField("Two Person").value;
    var family = this.getField("Family").value;
    var ins1 = this.getField("Insurance 1").value;
    var ins2 = this.getField("Insurance 2").value;
    var ins3 = this.getField("Insurance 3").value;
    
    // for field "value4"
    if (single && ins2 !=="Off") event.value = 10;
    else event.value = ""; 
    
    //for field value5
    if (person && ins2 !=="Off") event.value = 10;
    else event.value = ""; 
    
    //for field value6
    if (family && ins2 !=="Off") event.value = 10;
    else event.value = ""; 
    
    
    //custom calulcation script for the fields under column Insurance 3
    
    var single = this.getField("Single").value;
    var person = this.getField("Two Person").value;
    var family = this.getField("Family").value;
    var ins1 = this.getField("Insurance 1").value;
    var ins2 = this.getField("Insurance 2").value;
    var ins3 = this.getField("Insurance 3").value;
    
    // for field "value7"
    if (single && ins3 !=="Off") event.value = 10;
    else event.value = ""; 
    
    //for field value8
    if (person && ins3 !=="Off") event.value = 10;
    else event.value = ""; 
    
    //for field value9
    if (family && ins3 !=="Off") event.value = 10;
    else event.value = ""; 

     

     

     

    try67
    Braniac
    October 11, 2020

    A side-note: It doesn't make sense to have multiple, independent of each other if-else statements that apply a value to the same location. Since each one covers the entire range of possible "truth conditions" it means that only the last one matters. The rest of them will have no effect on the result whatsoever. Think about it like this:

    Let's calculate X.

    If the sky is blue X is 5, otherwise 10.

    If the ocean is dry X is 6, otherwise 10.

    If the desert is full of sand X is 2, otherwise 10.

    At the end of this process, what's the value of X? And does it matter if the sky is blue or the ocean is dry?

    No, because the last condition overwrites all previous conditions and so will determine the value of X, no matter what, so it can only be 2 or 10. I hope that makes sense...

     

    To prevent this from happening you should combine them to a single if-else if-else if-...-else statement (If the sky is blue X is 5, else if the ocean is dry X is 6, etc., else X is 10), OR set a default value up front and then only use if statements later on, without an else clause (X is 10. If the sky is blue X is 5. If the ocean is dry X is 6, etc.).

    ls_rbls
    Braniac
    October 11, 2020

    [Damn, I see now that each condition is for a different field... You can disregard all of the above, it doesn't apply. My apologies.]


    I also didn't organize my variables appropriately, there was no need to put them all together on each calulating field and I also did massive mistakes by copying and pasting the script many times. I didn't fix the fieldnames in the script.

     

    I should've posted the code like this without decalring variables:

     

    //custom calulcation script for the fields under column Insurance 1
    
    // for field "value1"
    if this.getField("Single").value &&  this.getField("Insurance 1").value !=="Off") event.value = 10;
    else event.value = ""; 
    
    //for field value2
    if ( this.getField("Two Person").value && this.getField("Insurance 1").value !=="Off") event.value = 10;
    else event.value = ""; 
    
    //for field value3
    if (this.getField("Family").value && this.getField("Insurance 1").value !=="Off") event.value = 10;
    else event.value = ""; 
    
    
    
    //custom calulcation script for the fields under column Insurance 2
    
    // for field "value4"
    if (this.getField("Single").value && this.getField("Insurance 2").value !=="Off") event.value = 10;
    else event.value = ""; 
    
    //for field value5
    if ( this.getField("Two Person").value && this.getField("Insurance 2").value !=="Off") event.value = 10;
    else event.value = ""; 
    
    //for field value6
    if (this.getField("Family").value && this.getField("Insurance 2").value !=="Off") event.value = 10;
    else event.value = ""; 
    
    
    
    
    //custom calulcation script for the fields under column Insurance 3
    
    // for field "value7"
    if ( this.getField("Single").value && this.getField("Insurance 3").value !=="Off") event.value = 10;
    else event.value = ""; 
    
    //for field value8
    if ( this.getField("Two Person").value && this.getField("Insurance 3").value !=="Off") event.value = 10;
    else event.value = ""; 
    
    //for field value9
    if (this.getField("Family").value && this.getField("Insurance 3").value !=="Off") event.value = 10;
    else event.value = "";