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

Trying to convert a SUMIF formula from Excel to Acrobat DC form (javascript)

Explorer ,
Feb 23, 2017 Feb 23, 2017

Hi,

I have an Excel spreadsheet with a few formulas in it.

One of them is a SUMIF, and I cannot figure out how to convert it into javascript (I'm not very familiar with jv at all) for an Acrobat form.

In one column there is a drop down for clients, ABC, DEF, LMO, etc. They can choose the same client in multiple rows. In the next column is the amount spent by each client.

There is a cell (field) where it totals the amount ABC spent.

So if we have

ABC     $1,000

DEF     $1,000

ABC     $500

LMO     $150

etc.

the field for ABC total would generate $1,500, for DEF $1,000 and so on.

There are a total of six clients on the drop down list and nine fields where $ entries can be made.

The formula in Excel is =SUMIF($F$17:$F$29,$F31,G$17:G$29)

But I'll be darned if I know how to get the same result in Acrobat.

Can anyone help?

Thank you

TOPICS
PDF forms
3.6K
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 ,
Feb 24, 2017 Feb 24, 2017

Let's say the fields in the first column are named Category1, Category2, etc., up to 10, and the second column fields are named Amount1, Amount2, etc.

To calculate the total amount for the ABC fields, you can use this custom calculation script:

var total = 0;

var maxFields = 10;

for (var i=1; i<=maxFields; i++) {

    if (this.getField("Category"+i).value=="ABC")

          total+=Number(this.getField("Amount"+i).value);

}

event.value = total;

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
Community Expert ,
Feb 24, 2017 Feb 24, 2017

Let's say the fields in the first column are named Category1, Category2, etc., up to 10, and the second column fields are named Amount1, Amount2, etc.

To calculate the total amount for the ABC fields, you can use this custom calculation script:

var total = 0;

var maxFields = 10;

for (var i=1; i<=maxFields; i++) {

    if (this.getField("Category"+i).value=="ABC")

          total+=Number(this.getField("Amount"+i).value);

}

event.value = total;

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
Explorer ,
Feb 24, 2017 Feb 24, 2017

It's close, but I can't get the variable to work (+i)

It will sort of work if I remove the +i and just put the full name of the field in (Amount1) but then the calculated field shows the amount in multiples of 10 (If I enter $1 the total field shows $10, and I've tried different number formats to no avail.)

It is so close, it's frustrating.

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
LEGEND ,
Feb 24, 2017 Feb 24, 2017

Please post a link to a sample of a form with your problem.

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 ,
Feb 24, 2017 Feb 24, 2017

Strange... Are there any error messages in the JS Console (Ctrl+J / Cmd+J) when you run it?

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
Explorer ,
Feb 24, 2017 Feb 24, 2017

There are no error messages when I run it with the exact name of the field.

I'm working on a copy I can link to (need to strip some information first since this is for my work.)

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
Explorer ,
Feb 24, 2017 Feb 24, 2017

Here is the link.

I hope the linking works.

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 ,
Feb 24, 2017 Feb 24, 2017

There are errors in the console when you change the value of one of the fields... The reason it's not working is you have only 9 sets of fields, not 10, as you've specified.

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 ,
Feb 24, 2017 Feb 24, 2017

PS. You also have errors in the calculation scripts for other fields...  There are multiple instances of this error message whenever I change a value:

TypeError: f is null

That means that you used an incorrect field name in one of the first two options under the Calculate tab, either by typing it incorrectly (under the second option) or by selecting a field and then renaming/deleting it (under the first option).

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
Explorer ,
Feb 24, 2017 Feb 24, 2017

Ah, ok.

I changed the names of many of the fields to conform more to the script you made, I must have missed some.

Changing the 10 to 9 worked!!

Thank you so much!

I really appreciate it.

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 ,
Feb 24, 2017 Feb 24, 2017

By the way, since you need to do pretty much the same thing for multiple fields I recommend using a function that takes the source value as a parameter. Put this code in a doc-level script:

function calcTotal(name) {

    var total = 0;

    var maxFields = 9;

    for (var i=1; i<=maxFields; i++) {

        if (this.getField("Source"+i).value==name)

              total+=Number(this.getField("FY1Row"+i).value);

    }

    event.value = total;

}

And then you can simply call it from the calculation script like this:

calcTotal("ARD");

Or:

calcTotal("DEPT");

etc.

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 ,
Feb 24, 2017 Feb 24, 2017

This code can also be adjusted so that the column number is also a parameter, and then you could use it for the entire Totals section, with just one function...

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
Explorer ,
Feb 24, 2017 Feb 24, 2017

Where would I put the column number in the script?

(Man, I gotta take some classes in scripting.)

You have been extremely patient with me, by the way. I really appreciate it!

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 ,
Feb 24, 2017 Feb 24, 2017

Change the main code to:

function calcTotal(name, col) {

    var total = 0;

    var maxFields = 9;

    for (var i=1; i<=maxFields; i++) {

        if (this.getField("Source"+i).value==name)

              total+=Number(this.getField("FY"+col+"Row"+i).value);

    }

    event.value = total;

}

And then the call to it to:

calcTotal("ARD", "1");

calcTotal("ARD", "2");

calcTotal("DEPT", "3");

etc.

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 ,
Feb 24, 2017 Feb 24, 2017

Actually, if you want to use it for the Total column as well it's better to do it like this:

function calcTotal(name, colName) {

    var total = 0;

    var maxFields = 9;

    for (var i=1; i<=maxFields; i++) {

        if (this.getField("Source"+i).value==name)

              total+=Number(this.getField(colName+"Row"+i).value);

    }

    event.value = total;

}

And then:

calcTotal("ARD", "FY1");

calcTotal("ARD", "FY2");

calcTotal("DEPT", "FY3")

calcTotal("VC", "Total")

etc.

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
Explorer ,
Feb 24, 2017 Feb 24, 2017

Ok, so jumping around one's cubicle saying "IT WORKED!" can get some strange looks from the rest of the office.

Thank you SO much!

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 ,
Jan 03, 2020 Jan 03, 2020
LATEST

Hi,

 

I also want to ask something about this sum ifs

Code    Hours                         Summary

BRV     5                                 Code         Hours

CTU    2                                   BRV        9

BRV     4                                   CTU       2

Total   11                                   Total        11

 

the left is the absent code that employee use and the left is the summary that total I have 31 rows.

 

Is this possible to make a script. it was working well in my excel but we need a pdf fillable.

 

I appreaciate any help..

 

Thankyou

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