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

Translating excel formula to javascript

New Here ,
Dec 14, 2020 Dec 14, 2020

Hello all,

  so I'm running into a bit of a problem. I'm trying to see if there's a way to translate the formula (in the formula bar) in the attached pic into a javascript. I have the same 4 field on a pdf file in the exact same order, but the names are different (01, 02, 03, 04) so the "formula" for the "04" field I'm looking for should go along the lines of:

=01-03+02

If you are familiar with excel formulas you know at 04 would pop up in DATE format.

 

Any and all help would be GREATLY APPRECIATED cause my brain is fried after trying to look for a similar javascript for the past 5 hours. Apologies if I seem to be rambling, I'm just out of it.

TOPICS
Create PDFs , Edit and convert PDFs , How to , JavaScript , PDF forms
1.5K
Translate
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
1 ACCEPTED SOLUTION
Community Expert ,
Dec 14, 2020 Dec 14, 2020

Assuming your fields are named
"Date", "Days Requested", "Days Substracted" and "Expiration Date"
Put code in "Expiration Date".
(In "Prepare form" mode, right click on "Expiration Date" field and select properties,
then select "calculate" tab and put code in "Custom calculation script".
If you want to rename your fields thats ok, just rename them in code also.
You can also change Date format if you wish, change mm/dd/yyyy to format you required.
"Expiration Date" field should auto update.

 

var req = this.getField("Days Requested").value;
var sub = this.getField("Days Substracted").value;
var cal = sub+req;
var tdate = this.getField("Date").valueAsString;
if(tdate == "") event.value = "";
else{
var cDate = util.scand("mm/dd/yyyy", tdate);
cDate.setDate(cDate.getDate()-cal);
event.value = util.printd("mm/dd/yyyy", cDate);}

View solution in original post

Translate
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 ,
Dec 14, 2020 Dec 14, 2020

Also, does the javascript automatically calculate the new date? Or do I have to click on something to make the "New Expiration Date" appear? I have no idea where I'm supposed to insert this javascript so help in navigating where this script is supposed to be posted would be greatly appreciate too. I'm sorry for being so demanding, I'm still new to this pdf stuff.

Translate
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 ,
Dec 14, 2020 Dec 14, 2020

Assuming your fields are named
"Date", "Days Requested", "Days Substracted" and "Expiration Date"
Put code in "Expiration Date".
(In "Prepare form" mode, right click on "Expiration Date" field and select properties,
then select "calculate" tab and put code in "Custom calculation script".
If you want to rename your fields thats ok, just rename them in code also.
You can also change Date format if you wish, change mm/dd/yyyy to format you required.
"Expiration Date" field should auto update.

 

var req = this.getField("Days Requested").value;
var sub = this.getField("Days Substracted").value;
var cal = sub+req;
var tdate = this.getField("Date").valueAsString;
if(tdate == "") event.value = "";
else{
var cDate = util.scand("mm/dd/yyyy", tdate);
cDate.setDate(cDate.getDate()-cal);
event.value = util.printd("mm/dd/yyyy", cDate);}
Translate
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 ,
Dec 14, 2020 Dec 14, 2020

YOU ARE A GODDESS!!! THANK YOU SO MUCH!!! THIS WORKS PERFECTLY!!!

Translate
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 ,
Dec 14, 2020 Dec 14, 2020

I'm sorry, I spoke too soon. The script written is subtracting both the days requested and the days subtracted. I'm trying to get it to subtract the days subtracted but add the days requested. Like:

 

Date - Days Subtracted + Days Requested

Translate
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 ,
Dec 14, 2020 Dec 14, 2020

Still though, I REALLY appreciate your help!

Translate
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 ,
Dec 14, 2020 Dec 14, 2020

Sorry about that, try this code.

var req = this.getField("Days Requested").value;
var sub = this.getField("Days Substracted").value;
var tdate = this.getField("Date").valueAsString;
if(tdate == "") event.value = "";
else{
var cDate = util.scand("mm/dd/yyyy", tdate);
cDate.setDate(cDate.getDate()-sub+req);
event.value = util.printd("mm/dd/yyyy", cDate);}
Translate
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 ,
Dec 14, 2020 Dec 14, 2020

Oh I just changed the sub+req to sub-req and it started working perfectly. Again, thank you so much! You really saved me a lot of headache.

Translate
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
Enthusiast ,
Dec 14, 2020 Dec 14, 2020

If you changed to sub-req then you get same result as you get with Nesa's first code, but you said that is not correct?

example:

Nesa's 1st code "100-(20+10)=70".

Nesa's 2nd code (where you asked for "Date - Days Subtracted + Days Requested") "100-20+10=90".

your code "100-20-10=70".
Or am I missing something here?

Translate
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 ,
Dec 14, 2020 Dec 14, 2020
LATEST

when I plugged the code in with the sub+req it was subtracting both the subtracted and days requested. When I changed it to sub-req it started doing what I needed it to so. I don't know exactly how scripts work, but the 1 change made it date-sub+req, cause before that it was doing date-sub-req.

Translate
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