Saturday, April 2, 2016

Find and Replace Placeholders in a Word Template

We have a template file that we use as a baseline in our report generation (see here for help creating a new Word document from a template). The template file has some placeholders for title, subtitle, author, etc. I wanted to be able to dynamically replace those with content when the rest of the document was generated. This took me way longer than I expected to figure out so here is hopefully some info to help the next person. First I found the relevant section in the XML (the Open XML Productivity Tools are a great help for this). Then with a bunch of trial and error I figured out how to find and replace the '[Document title]' placeholder.

Thursday, March 17, 2016

Custom validation in SurveyGizmo

SurveyGizmo allows you to add JavaScript directly to a survey page with the JavaScript Action. They use jQuery for survey interaction so that's already loaded on the page and available. Using this feature it's easy to customize a survey page.

One of the possible uses is for custom validation. For example to validate user input in some way that SurveyGizmo does not support out of the box. To do this you could start with the JavaScript below. It hooks the Next button click event and then hides or shows the error message based on a custom check. Note, we use the Next button click event and not the page submit event because that is called even when the Back button is selected.


Wednesday, March 16, 2016

Working with Arrays in SurveyGizmo Custom Scripting

As mentioned in my previous posts (here) we do a lot of work with SurveyGizmo's custom scripting feature. It's kind of like PHP and kind of like JavaScript but different enough that I forget how to do basic things like create arrays. So thought I would post some examples of working with arrays.

Some examples of working with single dimension arrays:

// Declare array with values
%%locations = array('Jacksonville, FL',
        'Toronto, Canada',
        'Orlando, FL',
        'Chicago, IL');

// Declare new empty array
%%copied = array();

foreach(%%locations as %%location) {
        // Add element to array
        %%copied[] = %%location;
}

And with a for loop (NOTE: You must use curly brackets when using a variable as the key).
for (%%i=0; %%i<%%count; %%i++) {
 %%output .= "<br>".%%locations{%%i};
}

And with a 2-dimension array (or hashtable):

// Declare array with values
%%locations = array('Toronto' => 'Ontario', 'Jacksonville' => 'FL',
        'Orlando' => 'FL', 'Chicago' => 'IL');

foreach (%%locations as %%key => %%value) {
        %%output .= "City: ".%%key." State/Province".%%value;
}

Tuesday, January 5, 2016

SurveyGizmo Custom Scripting

One nice feature of SurveyGizmo is their server-side custom scripting functionality (example below). It’s a PHP-like scripting language that allows you to modify the survey contents and operation at runtime. The script runs server-side before the page loads and offers a lot options for customizing a survey.

There are several things I would love to see with this feature:

  1. Syntax validation in the editor. Having to go through several survey pages to test your script and then find out you are missing a semi-colon or have a typo is no fun. A basic eval of the script would catch most of these errors.
  2. Using full PHP instead of PHP-lite. Having programmed a survey system I can understand why Survey Gizmo went with a more controlled subset of the language but when I need to use a function that isn’t there I’m not happy about it.
  3. Adding an option to run a script on page save. Currently, the scripts only run on page load so custom logic, validation and other items need to be on the next page. Sometimes it would be nice to just have them on the same page.

Having said that all in all its still pretty powerful functionality that we use with a lot of surveys at Researchscape.

Example of mapping the state question to another question that tracks the four main US Census regions.

Monday, January 4, 2016

SurveyGizmo Hacks

At Researchscape we’ve been using SurveyGizmo to run most of our surveys. It has a lot of flexibility built in but the customization options make it easy to adapt it for our needs. Going to start posting some quick hacks that I’ve used to customize various surveys.

First one up is a simple table customization. By default the table layout is dynamic but if you have a scale question with some labels (eg Strongly Disagree – 1, 2, 3, 4, Strongly Agree – 5) the column will not be equally sized. However we don’t want to change the whole table to a fixed layout which would squeeze the row heading.

Solution is to add a CSS class to the table question and in the Style section add custom CSS to the survey like follows:

.fixed-columns td:nth-child(n+2) {
    width: 8%;
}