2
# -*- coding: utf-8 -*-
5
Python script to import task data from a spreadsheet
7
(C)2011 Dag Andersen <danders@get2net.dk>
8
Licensed under LGPL v2.1+higher.
10
Requires Calligra Sheets
12
The data in the spreadsheet must be organized as follows:
14
1) The data must be found in sheet 1
15
2) Row 1 must define the header text for each column
16
3) The header text must match the task data property name
17
4) Column A must contain the WBS Code
18
5) Column A *MUST* be set to text format
19
6) The wbs code must be in the format defined by Tools -> Define WBS Pattern
20
(Default is arabic numbers with '.' separator, e.g. 1.2.3)
21
7) The separator must be a '.' for all levels
22
8) The tasks must be ordered in increasing wbs code order
23
9) The WBS codes must be consecutive:
24
If you have task 1.2 and 1.4 you must also have tasks 1, 1.1 and 1.3
28
WBSCode Name Responsible Allocation EstimateType Estimate Description
29
1 A Elvis Summary task
30
1.2 B Elvis Jane Effort 3d Task
31
1.1 C Elvis John Duration 4d Task
33
2 D Gardner Summary task
34
2.1 E Gardner Summary task
35
2.1.1 F Gardner Jane,John Effort 3h Next level
36
2.1.2 G Gardner Effort 0h Milestone
38
(Note that the resources listed in Allocation will be created if they do not already exists)
41
import os, sys, traceback
44
T = Kross.module("kdetranslation")
48
def __init__(self, scriptaction):
49
self.scriptaction = scriptaction
51
self.proj = Plan.project()
53
self.forms = Kross.module("forms")
54
self.dialog = self.forms.createDialog(T.i18n("Task Import"))
55
self.dialog.setButtons("Ok|Cancel")
56
self.dialog.setFaceType("List") #Auto Plain List Tree Tabbed
58
openpage = self.dialog.addPage(T.i18n("Open"),T.i18n("Tables Tasks"),"document-open")
59
self.openwidget = self.forms.createFileWidget(openpage, "kfiledialog:///tablestaskimportopen")
60
self.openwidget.setMode("Opening")
61
self.openwidget.setFilter("*.ods|%(1)s\n*|%(2)s" % { '1' : T.i18n("Tables"), '2' : T.i18n("All Files") } )
63
if self.dialog.exec_loop():
65
Plan.beginCommand( T.i18nc( "(qtundoformat)", "Import tasks" ) )
66
self.doImport( self.proj )
69
Plan.revertCommand() # in case partly loaded
70
self.forms.showMessageBox("Error", T.i18n("Error"), "%s" % "".join( traceback.format_exception(sys.exc_info()[0],sys.exc_info()[1],sys.exc_info()[2]) ))
72
def doImport( self, project ):
73
filename = self.openwidget.selectedFile()
74
if not os.path.isfile(filename):
75
raise Exception, T.i18n("No file selected")
77
Tables = Kross.module("kspread")
79
raise Exception, T.i18n("Could not start Calligra Sheets")
81
if not Tables.openUrl(filename):
82
self.forms.showMessageBox("Sorry", T.i18n("Error"), T.i18n("Could not open document: %1", [filename]))
85
def findParentTask(parent, wbs):
86
if wbs == project.data(parent, 'WBSCode'):
88
for i in range(parent.childCount()):
89
task = findParentTask(parent.childAt(i), wbs)
95
codes = wbs.split('.')
98
pwbs = '.'.join(codes)
99
for i in range(project.taskCount()):
100
parent = findParentTask(project.taskAt(i), pwbs)
101
if parent is not None:
105
def createTask(properties, data):
108
task = project.createTask(parentTask(data[0]))
110
raise Exception, T.i18n("Failed to create task")
112
for i in range(1, len(properties)):
113
project.setData(task, properties[i], data[i])
115
# Get data into list that can be sorted after wbs code
116
reader = Tables.reader()
120
props.append(reader.currentValues())
122
data = reader.currentValues()
123
# skip lines without wbs code
124
if data[0] is not None:
125
createTask(props[0], data)
127
reader.setSheet("Sheet1")
128
reader.connect("changedRow(int)", changedRow)