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/19 | 5% | 100% | 5 |
2/2/19 | 5% | 100% | 5 |
Date | % | Adj. % | Total |
---|---|---|---|
2/15/19 | 5 | 11.11% | 45 |
10 | 22.22% | ||
30 | 66.66% | ||
2/18/19 | 6 | 75% | 8 |
2 | 25% |
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!
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.
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?:
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.
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.
Copy link to clipboard
Copied
There are no fields with the name "Dist1", "Dist2", "Dist3", "Dist4", "Dist5", "Dist6", ... in your form.
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
Copy link to clipboard
Copied
Why does you use following else part:
} else {
totalArray.value = "";
adjPerArray.value = "";
};
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.
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.
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?
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.
Copy link to clipboard
Copied
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 = "";
};
};
Copy link to clipboard
Copied
You collect values in the array "dateGrouping" but you doesn't use this array.
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.