Table of Contents

Data Import

In Version 1.10 of time cockpit we have introduced an importer module that is capable of importing data from excel and CSV files.

Tip

Migrating from Excel? Read our comprehensive Excel to time cockpit migration guide and learn about time cockpit's integration capabilities.

Often it is necessary to import data from external sources into time cockpit. Use cases include importing a list of projects or customers, time sheet entries from previous time tracking solutions or from an external contractor not tracking time sheet entries with time cockpit.

In order for time cockpit's importer to be useful, it has to have an understanding of how columns in an Excel or CSV sheet relate to properties and relations of an entity. This information is held in an import definition object, a structure containing mappings from column names to properties for multiple entities. An import definition is therefore a 2 level structure with worksheet and column mappings. A worksheet mapping maps a specific worksheet in excel to a single specific entity in time cockpit and contains one or more column mappings. A column mapping maps a single column to a specific property within the entity mapped by the parenting worksheet mapping.

As an example, consider a situation where projects are imported. The import definition consists of a single worksheet mapping, mapping an excel worksheet to the project entity. The worksheet mapping contains multiple column mappings, mapping fields such as the project title and description, or the start date of the project. A column mapping may also map a relation, in which case a match criteria has to be chosen. The match criteria is a property related to the entity that contains the information that has to match the content of the cell that is imported. The importer queries the database for such an entity and ensures that exactly one item will be found. It will issue an error if none or multiple matches are found.

Note

An important thing to note about the importer is that it does not update existing rows but always creates new ones. Reimporting the sample file multiple times will cause duplicate items in time cockpit. Be sure to review the newly created data once it has been imported.

Viewing Import Definitions

The import definitions can be found in the Management module within the Settings section.

Import definitions

The link leads to the list of import definitions. Double-click an import definition to open it in the import definition editor. Each row also has the links Import (runs the import definition, see below) and Import Logs. As long as no import definition exists, the list is empty. To create a new import definition, click Add in the toolbar. This opens the import definition editor described below.

Import definitions list

Auto-Generating New Import Definitions

Open the import definition list and click the 'New' ribbon button. This will bring up the import definition editor pictured below. The import definition editor consists of four major regions:

  • General information: The name, the source type and whether to save a report log or not.
  • Sample file handling: Load, save or generate a sample file for an import definition.
  • The worksheet mapping grid: Lists mappings of worksheets to entities.
  • The column mapping grid: Lists mappings of source columns to properties for the currently selected worksheet mapping.

Import definition

In order to define a column mapping, the importer definition editor requires a clue about the source data that should be imported. There are two different common scenarios: First, you want to use the excel sheet as a way to enter data quickly. In this scenario you can freely define the structure of the source data, in which case it is best to let time cockpit's importer automatically generate the source file and column mapping. The second use case is that data is delivered to you in a specific format. In that scenario you have to adjust the mappings manually.

Model entity selection

For now, let's assume the first scenario, where the importer should auto generate an import definition as well as a sample file for us. Click Create sample file and select Customer from the list of available entities. You can select multiple entities as well and the importer will create multiple worksheet mappings, one per selected entity.

Clicking Select will generate an excel file as well as a corresponding import definition. Examining the created worksheet mapping, we can tell that the importer generated one worksheet mapping for a worksheet named Customer to the entity Customer as well as a column mapping for every property in customer (customer code, commercial register number, company name, contact information, email, fax, etc.) and a column mapping for the country relation.

Auto-generated import definition for customer The country relation is special, as the importer cannot just set the value of the property to the name of the country. Instead it has to set it to the entity object that is identified by the value in the column. The criteria to find the related object can be adjusted by changing the 'Reference Criteria' of the column mapping. In this simple scenario, the reference criteria could only be changed to the ISO code of the country, as country has no other properties to match.

Note

It is important that the relation criteria is a unique property of the related entity as the importer will warn and fail to import data if relations lead to ambiguous references.

Note

The relation criteria is not necessarily a direct property of the related entity. It can be any property that is 'reachable' from the target entity.

Finally, we can observe that the automatically generated import definition also maps a status column (Status Column). The status column is meant for the importer's result of each row. The web client shows that result on the import page: if rows fail, it lists each error with its worksheet, row number and message (see below).

In order to view the generated sample file, click the file name next to Download template (APP_Customer.xlsx). Opening the downloaded file will show an Excel file similar to the one pictured below:

Generated excel for customer

The name of the worksheet is the name of the source worksheet in the import definition and the name of the column is the content of the first cell of every column. Since we are fine with the import definition we enter a name (e.g. 'Customer import') and save the import definition which will then appear in the list of import definitions.

Performing an Import

In the previous section we created an import definition for the customer entity. We did not actually import any data, but rather prepared a sample file and a mapping between the sample files worksheets' columns to the respective entities and properties. This process is usually only performed once and every time data is imported to that structure, the import definition and sample file are reused.

In order to actually import data, first fill a sample file for the import with the data of the customers you want to import. For this test, the sample data used is the following:

Customer Code Commercial Register Number Company Name Contact Information Email Fax Hourly Rate Invoice FAO Invoicing Rules Phone Street Town VAT ID ZIP Code Country
CUST1 123450001 Nowhere Corp Ms. Customer 1 nobody@nowhere.com +4350609020 120 Mr. Nowhere none +4350609019 nowhere road nowhere town 90603010 204041 Austria

Leave the status column empty; it is meant for the importer's result of each row.

In order to import the file with the customer data, click the link Import next to the import definition in the list of import definitions. This opens the import page with the import definition selected. Make sure that the file you want to import is not open in Excel, then click Select file(s)... next to Upload file(s) or drop the file onto that field. The file is listed under Import File(s) with its last modification date.

Note

The file is read when you select it. Later changes to the file are not picked up automatically. If the file changes, remove it from Import File(s) and select it again.

Import page with a selected Excel file

Below the file, two buttons are available: Test Data and Import data. Testing rolls the transaction back after it has finished, so you can see whether the import would succeed without importing any rows. Importing commits the transaction only if no errors have occurred at all. In order to verify that our sample import works correctly, click Test Data. After a successful test, time cockpit shows "Your data has been tested successfully." and, per file and worksheet, the number of inserted and updated rows:

Successful import test

Note

An import only commits the transaction if no error occurred. Rows producing an error have to be removed from the source file or corrected before any row of the file is saved.

If rows fail, the import page shows "The import could not be executed successfully. The following errors occured:" and a table with the file, worksheet, row and message of every error.

After testing, start the actual import with Import Data Now next to the test result, or with Import data. After the import succeeded, the customer list shows the newly imported customers.

Advanced Example: Importing Projects

In the previous example, an import definition and a sample file were generated for a specific entity. In this example, projects will be imported, but only the fields required to actually create a project will be added.

The first step is to create an Excel file with the columns that should be imported. For importing projects, a first look at an empty new project form shows that three fields are required: the customer (a relation), the project code and the project name.

Create a new Excel workbook, rename the worksheet to 'Project' and enter 'Project Name', 'Project Code' and 'Customer Code' in the first cell of the first three columns. Also enter some sample projects to try the import definition later. Your worksheet should look similar to this:

Project Name Project Code Customer Code
Sample Project Sample1 Adventure
Follow-up Project Followup Adventure

The next step is to create a new import definition. Open the list of import definitions and click Add. Enter a name (for example 'Projects (minimal)'), keep the file type xlsx and upload the Excel file as template: click Select file(s)... next to Upload template or drop the file onto that field.

When you upload the template of a new import definition, the editor builds a worksheet mapping for every worksheet of the file. Because the worksheet name 'Project' matches the entity, the tab 'Project' already has 'Project' as Source Sheet and as Target Entity. If a worksheet name matches no entity, select the Target Entity yourself. The + button next to the worksheet tabs adds further worksheet mappings.

Note

time cockpit's importer is capable of automatically finding mappings if the name matches. This functionality can also be used on custom created sample workbooks by clicking Generate mapping automatically. It is therefore a best practice to keep the naming of columns in worksheets identical to entity properties or the (localized) names of those properties.

The second step is to check the mapping of the columns to the properties of the target entity (project in this case). The editor has already mapped the columns whose names match a property, here 'Project Name' and 'Project Code'. 'Customer Code' is not a property of the project, so its row has no target column, and the editor has added a row for the required relation 'Customer' without a source column. Select Customer Code as source column of the 'Customer' row and Customer Code as its reference criteria, then delete the row 'Customer Code' without a target column with its trash icon. Columns the editor did not create can be added with Add property. The result is the following mapping:

Source Column Target Column Reference Criteria
Project Name Project Name (empty)
Project Code Project Code (empty)
Customer Code Customer Customer Code
Note

When choosing the reference criteria for the 'Customer' relation, be sure to first select the target column, as the combo box for the relation criteria remains disabled until a relation member is actually selected.

After mapping the columns, the import definition should appear similar to the image below. Note that the order of the column mappings does not matter.

Import definition for Project

Note

In this example, we did not use a status column. The status column is optional.

After saving the import definition, the import can be performed just as if it was automatically generated.

Best Practices and Common Pitfalls

The order of worksheet imports is defined by the import definition. Use the arrows below the worksheet mappings to change the order of worksheet mappings. Within a single worksheet, the importer starts importing at the second row (the first row being the header) and works downwards to the last row.

When importing multiple entities with one excel sheet, the order of the worksheets in the import definition is relevant, because related objects may not be found. To correctly import dependencies, make sure that entities that are related in following worksheet mappings are imported first.

If an entity has a relation to itself, the order of the rows in the excel sheet is important. In order to resolve related objects, they have to be saved in the database. Therefore, ensure that related objects appear first in the excel file ( in a row with a lower row number).

Any data not related to the import table in a worksheet should be removed from the source worksheet, as it may cause the importer to determine an incorrect range. It will then try to import for instance empty rows. In case of uncertainty, copy the table to a fresh excel workbook and name the worksheet accordingly.

In some situations it may be possible that a previous row affects the current row. It is therefore advisable to resolve errors in the order they appeared, starting with the first row with an error message in the first mapped worksheet.

Since time cockpit's validation rules are defined at the data access layer, they are valid for the importer as well. Generally, it can be assumed that importing data using the importer is the equivalent of manually entering the data.

Give an import definition a name before automatically generating a sample file as the name of the sample file will be empty otherwise.