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

Why is my for loop iterating only once and not calculating fields correctly?

New Here ,
Feb 23, 2019 Feb 23, 2019

Copy link to clipboard

Copied

Hello,

I'm trying to create a script which will calculate the proportional percentage of a numerical entry sectioned by date. For example:

Date%Adj. %Total
2/1/195%100%5
2/2/195%100%5

Date%Adj. %Total
2/15/19511.11%45
1022.22%
3066.66%
2/18/19675%8
225%


So, the "Adj. %" values associated with each "parent" "Date" value should equal to 100% when added together. My issue is that my script doesn't seem to iterate properly and I end up with strange "Adj. %" values that do not reflect the respective "Date" and "%" field value entries. I included a link to a pdf, which includes the script I'm having issues with: https://docdro.id/TDcy6RX


I'm going crazy over this, any help will be highly appreciated!

TOPICS
Acrobat SDK and JavaScript

Views

794

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 ,
Feb 24, 2019 Feb 24, 2019

Copy link to clipboard

Copied

There are errors in your code. They appear as soon as the file is opened.

Go to Edit - Preferences - JavaScript and tick the option to show the console on errors and messages. This is a must-have setting for developers.

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 ,
Feb 24, 2019 Feb 24, 2019

Copy link to clipboard

Copied

I've gone ahead and tinkered with my code in the console a bit and have been able to get both my for loops to iterate successfully and console print the results( ie. 0,1,2,3). This means that the error in my code is in the if statements, correct? If so, would you be able to point me in the right direction from here? When I isolate the if statements in the console log, I get "SyntaxError: syntax error". I've gone over the syntax of my code and it all looks fine to me so I am not sure how or if this is the root problem of my faulty script.

Also, I get the following errors when I first open the pdf, could they be contributing to my issues?:

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 ,
Feb 24, 2019 Feb 24, 2019

Copy link to clipboard

Copied

Post the new full code, as well as the full error message.

It might not cause it, but it's certainly not helping.

That message means one of the field names you defined in the array is incorrect.

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 ,
Feb 24, 2019 Feb 24, 2019

Copy link to clipboard

Copied

Here is an updated pdf/scipt: https://docdro.id/oC5RvN4


I've gone ahead and removed the document level jS function that was causing the error at start up. Also, since another user pointed towards my else statement, I tried tinkering with that, but my changes don't seem to be effective at all.

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 ,
Feb 24, 2019 Feb 24, 2019

Copy link to clipboard

Copied

There are no fields with the name "Dist1", "Dist2", "Dist3", "Dist4", "Dist5", "Dist6", ... in your form.

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 ,
Feb 24, 2019 Feb 24, 2019

Copy link to clipboard

Copied

Ah, thanks for pointing that out, that was from a previous project. I've gone ahead and deleted that from the updated pdf: https://docdro.id/oC5RvN4

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 ,
Feb 24, 2019 Feb 24, 2019

Copy link to clipboard

Copied

Why does you use following else part:

} else {

totalArray.value = "";

adjPerArray.value = "";

};

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 ,
Feb 24, 2019 Feb 24, 2019

Copy link to clipboard

Copied

That else statement is supposed to clear/reset the fields for a specific row "i" if there isn't at least one row above it that has both "Date" and "%" fields with values entered. In practice, I don't think my script is executing in said way and I can't figure out why.

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 ,
Feb 24, 2019 Feb 24, 2019

Copy link to clipboard

Copied

I suggest you re-think about how this should work. Instead of keeping the date fields empty I think you should make sure they all have a value, and then iterate over the fields with the same value, add them up and then iterate over them again to calculate the relative value of each one from the total.

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 ,
Feb 24, 2019 Feb 24, 2019

Copy link to clipboard

Copied

I tried rewriting my script to follow the logic you outlined, but it ends up calculating the correct "Adj %" for only the first and last rows that share the same date value, do you know what is causing this issue?:

Also, is there a way I can copy the formatted script into my replies(so I don't have to post a pic)? When I try to control-c it, it pastes without the formatting?

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 ,
Feb 25, 2019 Feb 25, 2019

Copy link to clipboard

Copied

You must post your code as text. It's nearly impossible to debug it like that.

After pasting it into a new reply click on Use Advanced Editor (at the top-right) of the reply window, then select the code and click the ">>" icon at the top.

Then select "Syntax highlighting" and "JavaScript".

Even if you don't have access to this option a plain-text code (without formatting) is better than an image.

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 ,
Feb 25, 2019 Feb 25, 2019

Copy link to clipboard

Copied

LATEST

Thanks for the tip, here you go:

var dateField = this.getField("date");

var dateArray = dateField.getArray();

var percentField = this.getField("precent");

var percentArray = percentField.getArray();

var totalField = this.getField("total");

var totalArray = totalField.getArray();

var adjPerField = this.getField("adjPer");

var adjPerArray = adjPerArray.getArray();

var dateGrouping = [];

for (var i = 0; i < dateArray.length; i++) {

if (dateArray.value !== "" && dateGrouping.indexOf(dateArray.value) == -1) {

totalArray.value = percentArray.value

dateGrouping.push(dateArray.value);

for (var j = i + 1; j < dateArray.length.value; j++) {

if ( dateArray.value == dateArray.value) {

totalArray.value = "";

totalArray.value += percentArray.value;

adjPerArray.value = ((percentArray.value / totalArray.value) / 100);

};

};

adjPerArray.value = ((percentArray.value / totalArray.value) / 100);

} else if (dateArray.value == "" || percentArray.value == "") {

adjPerArray.value = "";

totalArray.value = "";

};

};

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 ,
Feb 25, 2019 Feb 25, 2019

Copy link to clipboard

Copied

You collect values in the array "dateGrouping" but you doesn't use this array.

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 ,
Feb 25, 2019 Feb 25, 2019

Copy link to clipboard

Copied

I‘m using the “dateGrouping” as a test condition in my first if statement. Basically, I want my if statement to check if a date field has a value AND wether this value is part of the “dateGrouping“ array.

If both conditions are true it means that the date field has a value and that value was not found in any of the date fields of the rows about it. The date value would then be pushed into the “dateGrouping” array so the following iterations would know that all rows with that specific value have already had their percent fields added under one total and calculated the respective “adj%” 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