Time custom field question

Rumceisz October 28, 2012

Hi All,

I created a scripted field which results the time in a particular status for each issue.

The endusers will export the report to excel which contains this custom field value. The problem is that this field - according to the time spent in that status - contains '2d 1h' or '3w 4d 4m' values and they need every value only in minutes because of the excel.

How can we do that?

Thanks in advance!

Rumi

2 answers

1 accepted

0 votes
Answer accepted
Florin Manaila
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
October 28, 2012

You need to specify a separate template for the excel view in your atlassian-plugin.xml and use it to format the time in minutes only.

<resource type="velocity" name="excel" location="mytemplate.vm"/>

Rumceisz October 28, 2012

Hi,

the excel export will be generated from Issue Navigator. Can I implement this script to a scripted field?

Florin Manaila
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
October 28, 2012

The excel view will be used whenever you custom field will be used in an "export to excel" action regardless of what/where it was triggered from.

I'm not sure I understand what you mean by scripted field. I thought you meant you wrote the plugin for the custom field yourself, in which case the above solution is correct. However, if you use a third party plugin you need to clarify this with the developer.

Rumceisz October 28, 2012

ok,

sorry:)

the current custom field which results the time spent in the particular status is a scripted field I made.

The field will be displayed in the issue navigator and the results (about 300 issues) will be exported to excel.
The users who need these custom field ask for displaying these time values in 'Minutes' in order to easily compare in the excel report.
So I think there is nothing to do with excel: can the script be altered to display the time spent in minutes?

here is the code:

import com.atlassian.core.util.DateUtils
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.history.ChangeItemBean
 
def componentManager = ComponentManager.getInstance()
def changeHistoryManager = componentManager.getChangeHistoryManager()
 
def inProgressName = "Clarification"
 
def rt = [0]
changeHistoryManager.getChangeItemsForField (issue, "status").reverse().each {ChangeItemBean item ->
 
    def timeDiff = System.currentTimeMillis() - item.created.getTime()
    if (item.fromString == inProgressName) {
        rt << -timeDiff
    }
    if (item.toString == inProgressName){
        rt << timeDiff
    }
}
 
// NOTE: doesn't show anything if less than 60 seconds
DateUtils.getDurationString(Math.round(rt.sum() / 1000))

Florin Manaila
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
October 28, 2012

Oh, the Scripted Field from the groovy plugin(?). Then you have no option but to always return the time in minutes.

JamieA
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
October 28, 2012

I'm not quite sure that's the case. You can use a custom template, and in that you can tell whether it's being rendered for excel or not, and do the conversion to pretty as necessary. Don't have a fully worked example though/

Rumceisz October 29, 2012

Hi Florin,

yes, this is a groovy script which works great. The only wish is to show the time spent value in minutes. But I'm out of idea...:(

Florin Manaila
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
October 29, 2012

Just replace

DateUtils.getDurationString(Math.round(rt.sum() / 1000))

with

Math.round(rt.sum() / 60000)

Rumceisz October 29, 2012

Hi Florin,

I get result but also a warning message after the code replace:

"The indexer for this field expects a java.lang.String but the script returned a java.lang.Long - this will cause problems."

Florin Manaila
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
October 29, 2012

Replace with

new String(Math.round(rt.sum() / 60000))

Rumceisz October 29, 2012

Hi Florin,

I insterted:

new String(Math.round(rt.sum() / 60000))

and it resulted:
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed: Script21.groovy: 23: unable to resolve class string @ line 23, column 1. new string(Math.round(rt.sum() / 60000)) ^ 1 error A stacktrace has been logged.

JamieA
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
October 29, 2012

You did "new string", not "new String". See the error message.

JamieA
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
October 30, 2012

OK, use:

Math.round(rt.sum() / 60000).toString()

Rumceisz October 30, 2012

You1re right, I corrected but it didn't work as well:

javax.script.ScriptException: groovy.lang.GroovyRuntimeException: Could not find matching constructor for: java.lang.String(java.lang.Long) A stacktrace has been logged.

Rumceisz October 30, 2012

Perfect!

thank you

Sleep well Jamie

0 votes
Maxim Abramovich October 28, 2012

Where you render field representaion? In script or in velocity template? If in template, you need separate template for excel export

Rumceisz October 28, 2012

Hi,

the excwel export will be generated from the Issue Navigator.

Suggest an answer

Log in or Sign up to answer