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

Custom Javascript: Defining target cell name based on host cell name

Guest
May 12, 2016 May 12, 2016

Hi,

I want to create a form for recording test results of large batches, so I have 400+ fields that I want to apply this percentage calculation to, the calculation will always be performed based on the values in the cells 1 & 2 positions to the left. So to avoid having to edit each of these fields manually and specifying the target fields name, I hope to be able to at the very least just have to paste in a custom script to each cell.

Given the number of fields adobe has created the form from a grid saved in the PDF. Is it possible to script the variable based on the cell name and apply it to the target names, see the example below?

Current script (I know the maths is currently wrong):

Cell name that holds this is Row1

var a = this.getField("FinishRow1"); var b = this.getField("StartRow1"); event.value = (a.value + b.value) * 0.0010;

Ps) Of course if I'm fundamentally approaching this in the wrong way any guidance would be appreciated.

TOPICS
Acrobat SDK and JavaScript
540
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 ,
May 12, 2016 May 12, 2016

What is the name of the target field, in the example you provided above?

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
Guest
May 12, 2016 May 12, 2016

Thanks for help,

Name of host field is: Row1

target field 1 is: StartRow1

target field 2 is: FinishRow1

hence I'm hoping to be able to:

var a = "start"+host field name

var b="finish"+host field name

var c= var a.value

var d = var b.value

event.value = var c.value / var d.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
Guest
May 12, 2016 May 12, 2016

I can extract (display) the individual values from the target cells with this code, but I don't seem to be able to perform the calculation with those values and I don't understand why.

Edit: The field displays NaN (Not a Number) I must be nearly there any assistance appreciated.

var S1 = event.target.name;

var S2 = "Finish".concat(S1);

art"+S1;

var S4 = S2+S1;

var S5 = S3+S1;

event.value = this.getField(S3).value; //Displays the numerical value stored in the appropriate cell for S2 or S3 as selected

//var a = this.getField(S2).value;

//var b = this.getField(S3).value;

//event.value = (a.value / b.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
Guest
May 12, 2016 May 12, 2016

Got it:

var S1 = event.target.name;

var S2 = "Finish".concat(S1); // concatenate works

var S3 = "Start"+S1; // simpler statement also works

event.value = (this.getField(S2).value / this.getField(S3).value)-1;

Cell format set to percentage.

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
Guest
May 12, 2016 May 12, 2016

Unfortunately although it achieved what I wanted it to, I was plagued by error messages when entering values into blank fields, so:

var S1 = event.target.name;

var S2 = "Finish".concat(S1);

var S3 = "Start"+S1;

if (this.getField(S2).value > 0 && this.getField(S3).value > 0) {

event.value = (this.getField(S2).value / this.getField(S3).value)-1;

}

else

{

event.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
Community Expert ,
May 12, 2016 May 12, 2016

What do the error messages say, exactly?

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
Guest
May 12, 2016 May 12, 2016

"The value entered does not match the format of the field (Row1)", when entering the first number onto the blank sheet, I got twelve error messages to click though (one for each custom script I had entered). It was a divide by zero error or there wasn't a value to display as a percentage.

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 ,
May 12, 2016 May 12, 2016
LATEST

OK, now I understand what's going on. You have to make sure that the value you're dividing by is not blank (or zero), as division by zero is not allowed.

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