Zu Hauptinhalten wechseln

FV Decipher Unterstützung

Alle Themen, Ressourcen für FV Decipher benötigt.

 
decipher

Importing External Offline Data with File()

Click here to learn how to import data using the survey builder.

Click here to learn an alternative way to import sample data sent by Campaign Manager.

1:  About Importing External Offline Data

Importing external offline data is the process of adding data from a tab-delimited file into your project. Sometimes you will want the data available immediately, as the survey is fielding, or after the survey has closed for keyword coding, categorization, etc..

2:  Example of Importing External Offline Data

In our example, sample data, a tab-delimited file is loaded into our survey using the File(...) function. 

2.1:  Sample Data to Load

For demonstration purposes, we are going to use the following tab-delimited file to show how to use File(...) to load this data to our project:

Click here to download the example tab-delimited file. 

When saving the file, be sure to end it with a .dat extension. Then upload it directly to your project's root directory. 

Note: When uploading files using File(...), it is best to use lowercase lettering throughout the file to avoid any issues with header conversion.

2.2:  Sample Survey

The XML code below will be the starting point for this example. We will load all of the data in the file above to this survey. One question below (q0) will be shown to the respondent and the rest will be <virtual> questions that can only be seen in the report and data downloads.

<?xml version="1.0" encoding="UTF-8"?>
<survey name="Survey"
 alt="External Data Import Demo"
 builderCompatible="1"
 extraVariables="source,list,url,record,ipAddress,userAgent"
 compat="127"
 state="testing"
 setup="time,quota,term">

<samplesources default="1">
  <samplesource list="1" title="default">
    <var name="source" unique="1"/>
    <exit cond="1"><b>Thank you!</b></exit>
  </samplesource>
</samplesources>

<html label="intro" where="survey">Welcome to the survey!</html>
<suspend/>

<text label="q0" optional="0">
  <title>
    Does this information look correct? If not, please correct and click "Continue".
  </title>
  <row label="name">Name</row>
  <row label="email">Email</row>
</text>
<suspend/>

<radio label="x1" virtual="1">
  <title>
    Radio Question
  </title>
  <row label="r1">Data Point 1</row>
  <row label="r2">Data Point 2</row>
  <row label="r3">Data Point 3</row>
</radio>

</survey>

Note in this example, we'll use a couple different approaches for accomplishing the same task. This is purely for demonstration purposes to illustrate the different ways you can populate a question's data. For example, the question "first" compared to the question "last".

3:  Modifying Our Survey to Load External Data

We have to make several modifications in our survey to load the external data.  These modifications are detailed in the following sections: 

3.1:  Adding the External File for Hidden Questions

The first step we need to do is load the file into our survey. To do that we are going to add the following code above q0 in our example survey. 

<exec when="init">
DataFile = File("include.dat", "source")
</exec>

In the above example we create a new variable DataFile that calls the File function with two arguments. The first is the name of our file in this case "include.dat". The second argument is the column name in the excel file that corresponds to the unqiue identifier that will be used to match respondents.

3.2:  Adding Logic for Hidden Questions

Once we have setup the file, we then need to populate q0 with the respondents data when they come in.

Below the previous exec block and above q0 we will add the following code block

<exec>
respInfo = DataFile.get(source)

if respInfo:
  q0.name.val = respInfo['name']
  q0.email.val = respInfo['email']
</exec>

In the exec block, we are going to access the corresponding source to pull the respondents data from the data file. From there we make sure that the record exists and if it does we are populating our hidden question. In our example for the respondent we are pulling their value from the name column and email column and assigning it to the row labeled name and email from q0 respectively.

 3.3:  Adding the External File for Virtual Questions

The code we add to add the external file for virtual questions is very similar to what we did for hidden questions. The main difference is instead of our exec block running at initialization (init) it runs at virtual initialization (virtualInit).

The following code can be placed anywhere but for our example we will place it before x1.

<exec when="virtualInit">
DataFile = File("include.dat", "source")
</exec>

3.4:  Adding Logic for Virtual Questions

Now that we have added the file to load at virtual initialization we need to add our code to populate x1.

Inside x1 we will remove the virtual="1" from the question tag and we are going to add the following code to replace it:

<virtual>
respInfo = DataFile.get(source)

if respInfo:
  x1.val = int(respInfo['x1']) -1
</virtual>

In the this code we are again accessing the corresponding source to pull the respondents data from the data file and then check to see if that record exists. The next line is the grabbing the value from the data file for the x1 column and assigning it to the value of the x1 question. Since the data from the data file is brought over as a string type, we need to use the in( ) function to type cast it as an integer, in order to assign it. The purpose of the -1 is to covert the data values 1-3 to the row indices 0-2 that are needed for assignment. 

4:  Example Solution

Making these edits we will end up with the following solution:

<?xml version="1.0" encoding="UTF-8"?>
<survey name="Survey"
 alt="External Data Import Demo"
 builderCompatible="1"
 extraVariables="source,list,url,record,ipAddress,userAgent"
 compat="127"
 state="testing"
 setup="time,quota,term">

<samplesources default="1">
  <samplesource list="1" title="default">
    <var name="source" unique="1"/>
    <exit cond="1"><b>Thank you!</b></exit>
  </samplesource>
</samplesources>

<html label="intro" where="survey">Welcome to the survey!</html>
<suspend/>

<exec when="init">
DataFile = File("include.dat", "source")
</exec>

<exec>
respInfo = DataFile.get(source)

if respInfo:
  q0.name.val = respInfo['name']
  q0.email.val = respInfo['email']
</exec>

<text label="q0" optional="0">
  <title>
    Does this information look correct? If not, please correct and click "Continue".
  </title>
  <row label="name">Name</row>
  <row label="email">Email</row>
</text>
<suspend/>

<exec when="virtualInit">
DataFile = File("include.dat", "source")
</exec>

<radio label="x1">
  <title>
    Radio Question
  </title>
<virtual>
respInfo = DataFile.get(source)

if respInfo:
  x1.val = int(respInfo['x1']) -1
</virtual>
  <row label="r1">Data Point 1</row>
  <row label="r2">Data Point 2</row>
  <row label="r3">Data Point 3</row>
</radio>

</survey>