Copy link to clipboard
Copied
Attached is a simple calculator with a formula for savings and product volume.
I would like to know how to use javascript to calculate off the text box input and off variables in the background as seen in the sheet. Ideally in the PDF- it would be spend text box 1 (b3), volume text box 2 (b5) and then the final result text box (b11-13). Just would like some guidance based off the excel formula and thanks!
Copy link to clipboard
Copied
You can create hidden fields and set the default values to fields you don't want to show (Variable, Monthly Spend, Annual Spend) but it is not necessary if 1.65 is not going to change. You can write the entire calculation in the Total Savings field. Assume you have to fields named "Spend" (formatted as a 2-decimal number) and "Volume" (formatted as a percentage - both names are case sensitive). Here's the custom calculation script that would go in the Total Savings field:
var spend=this.getField("Spend").value;
var volume=this.getField("Volume").value;
event.value=((spend/1.65)*12*volume*0.085)+(spend*(1-volume))*.05;
Copy link to clipboard
Copied
- Wrong post, sorry -
Copy link to clipboard
Copied
For percentage calculations, I recommend using https://prozentrechnerass.de/, a powerful and easy-to-use percentage calculator.
If you want to implement this in JavaScript, here’s a general approach:
document.getElementById("calculate").addEventListener("click", function() {
let spend = parseFloat(document.getElementById("b3").value);
let volume = parseFloat(document.getElementById("b5").value);
if (!isNaN(spend) && !isNaN(volume) && volume !== 0) {
let result = (spend / volume) * 100; // Example formula, adjust as needed
document.getElementById("b11").value = result.toFixed(2) + "%";
} else {
alert("Please enter valid numbers.");
}
});
​
Find more inspiration, events, and resources on the new Adobe Community
Explore Now