Sum Repeating Time Values
Written by
Gregory Scot Collins
Tuesday, 14 April 2009, 2:31 PM
This article has been tested to work with the following products and versions. No guarantee of compatibility, with or without modification, is offered for products or versions other than those listed.
IN THIS ARTICLE:
If you have a need to add up time values in an InfoPath repeating table or section, you can use the eval() function to accomplish your task.
The eval() function
eval() takes two arguments and returns a node-set containing the results of the expression in the second argument (which must exist in quotes) as applied to the node-set in the first argument.
Convert time to seconds
InfoPath stores time in the XML format of HH:MM:SS (hours, colon-separator, minutes, colon-separator, seconds). In this example, we will convert each time value to seconds and get the sum. Once we've obtained the sum in seconds, we can then display the number of seconds, or divide out into minutes or whatever.
To convert a time value into seconds, we must individually select the hours, minutes and seconds. Hours are converted to seconds when multiplied by 3600 and minutes are converted to seconds when multiplied by 60.
To get each portion of an XML time value, we will use the substring() function. substring() is 1-based, meaning the first character is found at position 1.
Perform the sum
With a DOM structure of my:myFields/my:Table/my:Row/my:TimeField, you can use the expression shown in Listing 1 in an Expression Box or a default value formula to get the sum of time in seconds:
We included the filter [. != ''] to ensure that only fields with values are included in the evaluation. This prevents the expression from resulting in the 1.#QNAN error. However, you will still get this error if you have invalid or non-time values in your list.
Convert to minutes or hours
If you prefer to display the sum minutes, append the expression in Listing 1 with div 60, or div 3600 if you prefer hours.
Copyright ©2009
Braintrove. All rights reserved. This material may not be copied, published, broadcast, rewritten
or redistributed. You may, however, use the techniques, along with any associated code and files in
your development. This material is provided "AS IS" without warranty of any kind. You accept full
responsibility and liability for any and all results associated with use of this material.
Stumble It!
Digg It!
del.icio.us




