Skip to main content
Participating Frequently
February 1, 2024
Answered

Addition, with a twist

  • February 1, 2024
  • 2 replies
  • 1944 views

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

This topic has been closed for replies.
Correct answer Nesa Nurani

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;

 

 

2 replies

try67
Community Expert
February 1, 2024

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.

Participating Frequently
February 1, 2024

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).

try67
Community Expert
February 1, 2024

You can't multiply by a value such as "4 Copper". Is this the same as 4?

Nesa Nurani
Community Expert
February 1, 2024

Post what you have tried so far.

Participating Frequently
February 1, 2024

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. 🙂

 

Thom Parker
Community Expert
February 1, 2024

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. 

 

 

 

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often