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

New Here ,
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!!!

Most Valuable Participant
Correct answer by try67 | Most Valuable Participant

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;

TOPICS
Acrobat SDK and JavaScript, Windows

Views

51

Likes

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

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

New Here ,
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!!!

Most Valuable Participant
Correct answer by try67 | Most Valuable Participant

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;

TOPICS
Acrobat SDK and JavaScript, Windows

Views

52

Likes

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
Mar 27, 2019 0
Most Valuable Participant ,
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?

Likes

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
Reply
Loading...
Mar 27, 2019 0
New Here ,
Mar 27, 2019

Copy link to clipboard

Copied

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

Thank you!!!

Likes

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
Reply
Loading...
Mar 27, 2019 0
Most Valuable Participant ,
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;

Likes

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
Reply
Loading...
Mar 27, 2019 0
New Here ,
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?

Likes

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
Reply
Loading...
Mar 27, 2019 0
Most Valuable Participant ,
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.

Likes

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
Reply
Loading...
Mar 27, 2019 0
Most Valuable Participant ,
Mar 27, 2019

Copy link to clipboard

Copied

(and clear the field)

Likes

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
Reply
Loading...
Mar 27, 2019 0
lisajIHV LATEST
New Here ,
Mar 27, 2019

Copy link to clipboard

Copied

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

Likes

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
Reply
Loading...
Mar 27, 2019 0