Copy link to clipboard
Copied
Hi there,
I'm not sure if what I'm looking to do is even possible in Acrobat/Javascript but here goes.
I'm trying to design a digital character sheet for a typing impaired person to use. As such I have tried to make as much of it as possible click and choose, dropdown menu's, lists etc. Things are going well so far, with some help from Nesa and Thom, but I have run into an issue I'm hoping someone can help me solve. As part of the sheet when someone makes choices and chooses weapons, equipment, armour and such text fields are populated with details for the item chosen. One of those fields is the weight of the item. I have figured out how to keep a running total of all items chosen so that the user can see exactly how much everything they have weigh's in total. Another of the fields is the cost of the item, with items ranging in values of Copper, Bronze, Silver and Gold. Here is where I have run into my stumbling block. I would like to be able to keep a running count/total of the cost of everything the user buys but can't figure out how to add the four cerrencies, Copper, Bronze, Silver and Gold together. Anything I've tried ends up with a 'NaN' result in the result box. Is there a way of adding all four currencies together. I don't mind if the resulting output will be XGold-XSilver-XBronze-XCopper, or XGold,XSilver,XBronze,XCopper or a variant of those, as long as it will add the amounts and display them where the user can see.
Any help, even letting me know it can't be done, would be gratefully received. 🙂
thanks,
Jim
You can use this as custom calculation script of "TotalProjectileTypePrice" field, also change to however you want values to be shown in the last line:
var Copper = 0;
var Bronze = 0;
var Silver = 0;
var Gold = 0;
for(var i=1; i<=5; i++){
var amt = Number(this.getField("ProjectilesAmount.row"+i).valueAsString);
var str = this.getField("ProjectilesPrice.row"+i).valueAsString;
if(str !== ""){
var parts = str.split(" ");
var cost = Number(parts[0]);
var currency = parts[1];
...
Copy link to clipboard
Copied
Post what you have tried so far.
Copy link to clipboard
Copied
Hi Nesa, I didn't keep a copy of the javascript I tried for the addition problem, but having just set it up again on my sheet and by using the 'Calculate' tab on my output text field and picking the fields for it to add to my sum I am getting a numerical value but not the name of the coins. So if I have 3 items costing 2 Gold, 4 Silver and 6 Bronze then all I am getting in the output field is 12. It's adding just the numbers when I need it to add the various denominations and display the Gold, Silver, Bronze and Copper amounts. If you need me to I can upload an example.
As an aside, and a sort of related question, I do have an example of another calculation issue I am having while trying to multiply two different fields by each other. It's related to the Gold, Silver, Bronze and Copper issue above but is to do with multiplication rather than addition. I've uploaded a sheet with what I have so far. What I'm trying to do is multiply a projectile amount by the cost of a single unit of that projectile. Again I'd like the output to display in Gold, Silver, Bronze and Copper.
So far with that issue I've tried these three tests but none have worked so far
Test 1:
(ProjectilesAmount.row1*ProjectilesPrice.row1)+(ProjectilesAmount.row2*ProjectilesPrice.row2)+(ProjectilesAmount.row3*ProjectilesPrice.row3)+(ProjectilesAmount.row4*ProjectilesPrice.row4)+(ProjectilesAmount.row5*ProjectilesPrice.row5)
Test 2:
event.value = ( this.getField("ProjectilesAmount.row1").value * this.getField("ProjectilesPrice.row1").value + ( this.getField("ProjectilesAmount.row2").value * this.getField("ProjectilesPrice.row2").value + ( this.getField("ProjectilesAmount.row3").value * this.getField("ProjectilesPrice.row3").value + ( this.getField("ProjectilesAmount.row4").value * this.getField("ProjectilesPrice.row4").value + ( this.getField("ProjectilesAmount.row5").value * this.getField("ProjectilesPrice.row5").value );
Test 3:
var v1 = Number(this.getField("ProjectilesAmount.row1").valueAsString)
var v2 = Number(this.getField("ProjectilesPrice.row1").valueAsString)
var v3 = Number(this.getField("ProjectilesAmount.row2").valueAsString)
var v4 = Number(this.getField("ProjectilesPrice.row2").valueAsString)
var v5 = Number(this.getField("ProjectilesAmount.row3").valueAsString)
var v6 = Number(this.getField("ProjectilesPrice.row3").valueAsString)
var v7 = Number(this.getField("ProjectilesAmount.row4").valueAsString)
var v8 = Number(this.getField("ProjectilesPrice.row4").valueAsString)
var v9 = Number(this.getField("ProjectilesAmount.row5").valueAsString)
var v10 = Number(this.getField("ProjectilesPrice.row5").valueAsString)
event.value = ( v1 * v2 ) + ( v3 * v4 ) + ( v5 * v6 ) + ( v7 * v8 ) + ( v9 * v10 );
I've added a sample sheet with the issue for you to have a look. 🙂
Copy link to clipboard
Copied
There are several issues with the scripting. However, the main problem is that the "ProjectilesPrice.row#" fields do not contain numbers, so they cannot be used in a numerical calculation. You'll need to fix this first.
Another issue is the code fomatting. While this does not affect the execution of the code, it creates a barrier to your ability to maintain it, and for the rest of us to help you.
Copy link to clipboard
Copied
Hi Thom,
Yes I understand that the "ProjectilesPrice.row#" is not just a numerical value, that's what I'm trying to overcome with my question. As I said I'm not sure if it's even possible to do. If you look at my reply to try67 below I've tried to state what it is I'm trying to achieve.
I'm not sure what you mean by the code formatting. Could you explain that to me if possible?
Copy link to clipboard
Copied
You can use this as custom calculation script of "TotalProjectileTypePrice" field, also change to however you want values to be shown in the last line:
var Copper = 0;
var Bronze = 0;
var Silver = 0;
var Gold = 0;
for(var i=1; i<=5; i++){
var amt = Number(this.getField("ProjectilesAmount.row"+i).valueAsString);
var str = this.getField("ProjectilesPrice.row"+i).valueAsString;
if(str !== ""){
var parts = str.split(" ");
var cost = Number(parts[0]);
var currency = parts[1];
switch(currency){
case "Copper":
Copper += amt * cost;
break;
case "Bronze":
Bronze += amt * cost;
break;
case "Silver":
Silver += amt * cost;
break;
case "Gold":
Gold += amt * cost;
break;}}}
event.value = "C:"+Copper+" B:"+Bronze+" S:"+Silver+" G:"+Gold;
Copy link to clipboard
Copied
Hi Nesa,
thank you very much for that script. I've just tried it on my sheet and it works brilliantly. I should also, with some trial and error be able to use the same script to figure out my addition problem as well so I'm going to mark yours as the correct answer. Thanks again I do appreciate your help! 🙂
Copy link to clipboard
Copied
The best method is to place numerical values in the price fields, then use custom formatting to show the value. This solves the problem in the simplest way and creates the most robust maintainable code.
Copy link to clipboard
Copied
Hi Thom,
thanks for the reply and the advice. Try67 has said the same thing to me. I never even thought of doing it that way. I think my brain may be hardwired a certain way. Thanks for the help. 🙂
Copy link to clipboard
Copied
These things are not obvious. It is not well knowns that the format script on a field only changes how the data is presented to the user. So you can add text, and it makes no difference to value of the field. This allows fields that are used for calculations to contain the data needed for the caculation, but show the user a value that has meaning to them. Separation of data, presentation, and behavior are key components to good programming. But you'd actually have to have studied software development to know this. It is unfortunate that there are hoards of people (and AI's) out there writing code that have no idea what they are doing, rathing than hiring a professional. You may think that your code works, but what you may be missing is that it only work under specific circumstances. When circumstances change things break.
Copy link to clipboard
Copied
Just be aware this is very dependent on your users getting it right. If they enter "Cooper" or "gold", it will break. That's why using a drop-down for the currency is the better option, in my opinion.
Copy link to clipboard
Copied
Do your users enter the currencies manually into the fields, or are they added by the Format setting you selected?
If the former, you must remove them in your code before adding them up. If the latter then it should work automatically.
Copy link to clipboard
Copied
Hi try67, the currencies are autopopulated depending on the choice of item from a dropdown box. Some items are expensive (Gold), other are very cheap (Copper).
Copy link to clipboard
Copied
You can't multiply by a value such as "4 Copper". Is this the same as 4?
Copy link to clipboard
Copied
As I said I'm not sure if this is even possible in Acrobat/Javascript. Is there a way of multiplying a number by a number and word? Basically this:
Q. 2Gold X 4
A. 8Gold
and further than that,
Q. (2GoldX4) + (4CopperX2) + (3SilverX3)
A. 8Gold,9Silver,8Copper
1. The user chooses Long bow arrows from the dropdown list. This autopopulates the cost and weight of a single arrow into the cost and weight field.
2. They then choose the amount of arrows from a second dropdown list.
Copy link to clipboard
Copied
It's possible, but unnecessarily complicated. I recommend you split the number field from the "currency" field, and make the latter a drop-down. That will make things much much easier, as you could use the number for the formula, and then just add the currency text at the end of the result.
Copy link to clipboard
Copied
Hi try67,
thanks for the reply and the advice. I wonder if 'unnecessarily complicated' is what Thom meant about my code formatting above. It seems like my javascript is a lot like my DIY, it may not look pretty but it gets the job done.
Copy link to clipboard
Copied
Thom was talking about the structure of your code, if I understood him correctly, while I was referring more to the setup of the fields in your file.