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

HELP! Script needed to get largest %age and fill field with colour based on answer

New Here ,
Mar 27, 2019 Mar 27, 2019

Copy link to clipboard

Copied

Hi everyone

Hoping one of you can help me please....

I have a pdf doc which is a questionnaire with a drop down list of responses for each answer (Yes, No and Partly). I then have a summary page at the end which counts the number of answers and calculates the percentage against each answer, i.e. Yes 34% No 23% Partly 43% ( i have a field for each of these).  What I would now like to do is have a field which calculates the largest percentage and fills the field with colour depending on the largest percentage, i.e if Yes is largest percentage then field is filled with Green, No with Red and Partly with yellow.

can someone help me with the script for this please?

Thank you so much in advance!!!

TOPICS
Acrobat SDK and JavaScript , Windows

Views

372

Translate

Translate

Report

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

correct answers 1 Correct answer

Community Expert , Mar 27, 2019 Mar 27, 2019

OK, then you can use this code as the custom calculation script of your field:

var newFillColor = color.transparent;

var values = [Number(this.getField("D1Yes%age").valueAsString), Number(this.getField("D1No%age").valueAsString), Number(this.getField("D1Partly%age").valueAsString)];

var colors = [color.green, color.red, color.yellow];

var max = 0;

for (var i in values) {

    if (values>max) {

        max = values;

        newFillColor = colors;

    }

}

event.value = max;

event.target.fillColor = newFillColo

...

Votes

Translate

Translate
Community Expert ,
Mar 27, 2019 Mar 27, 2019

Copy link to clipboard

Copied

So you already have a script that calculates the percentage of each answer? If so, what are the names of those fields?

Votes

Translate

Translate

Report

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
New Here ,
Mar 27, 2019 Mar 27, 2019

Copy link to clipboard

Copied

Yes I do - those fields are called D1Yes%age, D1No%age and D1Partly%age

Thank you!!!

Votes

Translate

Translate

Report

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 ,
Mar 27, 2019 Mar 27, 2019

Copy link to clipboard

Copied

OK, then you can use this code as the custom calculation script of your field:

var newFillColor = color.transparent;

var values = [Number(this.getField("D1Yes%age").valueAsString), Number(this.getField("D1No%age").valueAsString), Number(this.getField("D1Partly%age").valueAsString)];

var colors = [color.green, color.red, color.yellow];

var max = 0;

for (var i in values) {

    if (values>max) {

        max = values;

        newFillColor = colors;

    }

}

event.value = max;

event.target.fillColor = newFillColor;

Votes

Translate

Translate

Report

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
New Here ,
Mar 27, 2019 Mar 27, 2019

Copy link to clipboard

Copied

This works perfectly for the colour - thank you so much! But it returns a number in the field also.  How can I get this to just be colour filled with no text?

Votes

Translate

Translate

Report

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 ,
Mar 27, 2019 Mar 27, 2019

Copy link to clipboard

Copied

Sorry, thought you wanted to display the actual max value as well. Just remove line #11 from the code above.

Votes

Translate

Translate

Report

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 ,
Mar 27, 2019 Mar 27, 2019

Copy link to clipboard

Copied

(and clear the field)

Votes

Translate

Translate

Report

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
New Here ,
Mar 27, 2019 Mar 27, 2019

Copy link to clipboard

Copied

LATEST

THANK YOU SO MUCH!!!! I am so grateful!!  That's perfect!!!

Votes

Translate

Translate

Report

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