Print Page | Close Window

Last None Blank field in a group

Printed From: Foxit's Planet PDF Forum
Category: Lets Talk PDF
Forum Name: JavaScript
Forum Description: Discussions relating to the use of JavaScript within PDF.
URL: http://forums.planetpdf.com/forum_posts.asp?TID=2016
Printed Date: 18 Jul 2019 at 8:59pm
Software Version: Web Wiz Forums 11.10 - http://www.webwizforums.com


Topic: Last None Blank field in a group
Posted By: Tgean
Subject: Last None Blank field in a group
Date Posted: 17 Jun 2016 at 9:43am
I am not an expert script writer but I am learning. My goal is, I have a group of fields "Item.1" to "Item.25" on the form. At the tops of the form I have a field named "Last". Last.value should = the last item listed in fields 1 -> 25. I have been working on this for weeks now and for some reason my code is not working. Here is a link to the PDF file  http://drive.google.com/file/d/0BzsJJd_3je99dU9CQ1VlbTZ5cTA/view?usp=sharing - http://drive.google.com/file/d/0BzsJJd_3je99dU9CQ1VlbTZ5cTA/view?usp=sharing  

Please help me make this work. Its driving me mad

The Code is written as a custom calculation script in the field named "Last".

{
var theResult = null;
for (var theIndex = 1; theIndex < 26; theIndex++)
{
    var theField = this.getField( "Item" + theIndex );
    if (theField.value) 
{
        theResult = theField.value;
    } else 
{
        break;
}
}
var show = this.getField("Last");
show.value = theResult
}



Replies:
Posted By: try67
Date Posted: 17 Jun 2016 at 10:22am
There are errors in your code. Check the JS console and you'll see them.


-------------
- Acrobat Expert - Contact me personally at try6767@gmail.com
Check out my custom-made scripts website: http://try67.blogspot.com


Posted By: try67
Date Posted: 17 Jun 2016 at 10:23am
There are also errors in the calculation scripts of other fields in your file. You have to solve all of them first.


-------------
- Acrobat Expert - Contact me personally at try6767@gmail.com
Check out my custom-made scripts website: http://try67.blogspot.com


Posted By: Tgean
Date Posted: 17 Jun 2016 at 10:36am
I have checked the console I am rather new at this and I dont really know what is wrong.  

TypeError: f is null
1051:byteCodeTool
TypeError: theField is null
6:AcroForm:Last:Calculate

I have no clue why.


Posted By: try67
Date Posted: 17 Jun 2016 at 10:41am
It's because you didn't spell the name of the fields correctly. In your code you're trying to access "Item1", "Item2", etc., but the actual field names are "Item.01", "Item.02", etc.

Here's the correct code to use:

var theResult = "";
for (var theIndex = 1; theIndex < 26; theIndex++) {
    var theField = this.getField( ((theIndex<10) ? "Item.0" + theIndex : "Item." + theIndex) );
    if (theField.value) {
        theResult = theField.value;
    } else {
        break;
    }
}
event.value = theResult;

The other issue is with the calculation of the Total field. You renamed the Value fields but didn't update it, so it's not working now.


-------------
- Acrobat Expert - Contact me personally at try6767@gmail.com
Check out my custom-made scripts website: http://try67.blogspot.com


Posted By: Tgean
Date Posted: 17 Jun 2016 at 11:15am
First off thank you so much for the help. Can you explain to me why this.getField( ((theIndex<10) ?. I dont understand the logic there. I am not saying your wrong, I just want to learn more. 


Posted By: try67
Date Posted: 17 Jun 2016 at 11:18am
You need to read the whole thing:
this.getField( ((theIndex<10) ? "Item.0" + theIndex : "Item." + theIndex) );

Basically what it does is adds the additional "0" in front of the index in case number is smaller than 10, because your fields are named "Item.01", "Item.02", and not "Item.1", "Item.2"... Makes sense?


-------------
- Acrobat Expert - Contact me personally at try6767@gmail.com
Check out my custom-made scripts website: http://try67.blogspot.com


Posted By: Tgean
Date Posted: 17 Jun 2016 at 11:31am
Perfectly Thank you again for your help. As I said I am still very new at scripting. This little project tought me alot. 



Print Page | Close Window

Forum Software by Web Wiz Forums® version 11.10 - http://www.webwizforums.com
Copyright ©2001-2017 Web Wiz Ltd. - https://www.webwiz.net