With this feature, it is possible to import data from an Excel sheet into a template's content controls in Word. As an example, this can be used to create a recipient list from which the user can select a recipient, and thus importing the data for this specific recipient. It can also be used to create a list of products with their respective data from which the user can select what product to import data from. In this article, the recipient list will be used to elaborate the functionality of DataImporter, but if you would like to see an example of using this feature as an import of product data, take a look at Trucklageret.
Module for importing data is located here:
...\ModuleLibrary\DataImporter\01_nocontext_DataImporter\05_importDataFromExcel.xml
1: Create the template
To get this functionality, you will need to create an Excel sheet as a template for the data that the client can fill out. This means that the names of the columns must be decided, in order to make content controls to insert the data into templates. The sheet could look like the screenshot below, but the names can be adjusted to whatever is appropriate in your case. The important columns to be named are marked:
There can be as many rows as needed. The user will be able to choose between these in Word. In the example below, there is only one row where SkabelonDesign is listed, and the data about SkabelonDesign is divided by Navn, Virksomhed, Adresse 1,2,3 and ATT.
Save the template sheet as an 'Excel Template (*.xltx)' here:
Content\BaseExtensions\
Insert images with DataImporter
We support inserting images in Word with DataImporter. To do so, add a column in the template sheet called ImagePath
. In this column, the path to the specific image must be defined. We are able to define whether the inserted image should fit or fill. Therefore, the placeholder in Word, wherein the image should be inserted, must be placed in a content control that has this tag:
{"SkabelonDesign":{"type":"x","binding":"_.ImagePath"}}
x
must replaced with either ImageFill
or ImageFit
depending on whether it should fill or fit.
CMD
<Cmd case="BaseExtensions:exec" path="x"/>
This is the command for opening the template. Therefore, it is important to define the path to our saved template in the path parameter, i.e. path="Content\BaseExtensions\fileName.xltx"
. When clicking the button for this command, the template opens and enables the user to fill out data. This sheet must be saved as a new one and will be the source when selecting what data to import in the next command.
2: Select and import data from Excel
This button will provide the user with a prompting folder, where the user must select the Excel sheet with the filled in data to be inserted.
CMD
<Cmd case="DataImport:MultiXlSearchGui" prefix="_." filePath="x"/>
In order to import data, adjust the prefix in the command above.
prefix
is a name placed in front of a value, which in our case is the data. It is necessary to pick a short prefix, e.g. _ (underscore), if you use Office2010, as it has a limited character space.
It is optional to use filePath
, but this parameter is handy as it enable us to define the directly path to the Excel file from which the command should grab the data. When using this parameter, thus the command will not prompt a folder, but just selecting the defined Excel sheet right away.
The command above must be used in conjunction with two DocumentData commands; one that enables pending changes and one that commits. This is how it should be build:
<Button label="Import Data">
<Cmd case="SkabelonDesign.DocumentData:EnablePendingChanges"/>
<Cmd case="DataImport:MultiXlSearchGui" prefix="_."/>
<Cmd case="SkabelonDesign.DocumentData:Commit" selectionOnly="true"/>
</Button>
The first one stops Word and makes sure to gather all data before it will be imported. This is handy as it will import the data at one time instead of continuously. The last one commits the gathered data. The parameter selectionOnly
ensures that it only updates the active content control.
When the sheet is chosen, the user is allowed to pick in which row he/she want data to be inserted, as visualized below:
To receive the data in a template, content controls must be made in a document in the same folder as the Excel sheet. In this example, the document will be named "Recipients.docx".
In the document a content control linked to each column must be listed as visualized below:
The inner content control must have the following properties, name and tag. The name is optional, but the tag must contain a document link. Document link content controls only support documents located inside the Content/BaseExtensions folder, using the following syntax (JSON) and must be minimized into one line in the content control tag:
{"SkabelonDesign":{"type":"Text","binding":"_.Navn"}}
The visibility content control is optional, but beneficial as nothing will be displayed if the column in the Excel sheet is not filled out. The visibility content control must also be tagged using the following syntax:
{"_sdv":"Action/Operator/Binding/CompareValue"}
In order to support Office 2010, which has a 64 character limit for the tag field, a shorthand structure can be used for visibility groups. Action and operator are shortened to their CamelCase letter(s) in lowercase, e.g.: Keep, Show, StartsWith, Contains, EndsWith.
In the example below the visibility content control must show if the content control contains prefix from the column named navn, and therefore must be tagged:
{"_sdv":"s/c/_.Navn/"}
Use of DataImporter in specific templates
When the Excel sheet and the document with content controls are placed in the content folder, you are able to make use of the data from Excel in your template.
Make a content control with the name 'RecipientList', and add the following in the tag. Notice that the content control refers to the document named Recipients.docx with the listed content controls, as described above.
{"SkabelonDesign":{"LinkSource":"Recipients.docx"}}
When the user click "Import Data" and pick SkabelonDesign, the following will now be displayed in a template.
Requirements
Newest BaseExtensions, Dataimporter and SkabelonDesign.DocumentData plugin, and Alloy addin version 0.10.5.0 or newer.
Comments
0 comments
Article is closed for comments.