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%;

Tuesday, December 15, 2015

Scratchpad Tools

Whenever I want to test out some small C# code I use the excellent LINQPad. It's great as a quick scratchpad for everything from a couple of lines of code to a small program. You can reference your own class libraries so it's great for performance testing. I also use it a lot to transform files. The free version is nice but if you can swing it, Pro is worth it just for the auto-completion.

For JavaScript testing and debugging it's hard to beat JSFiddle. I use it often and like the ability to save and share snippets of JS. I was interested when I found .NET Fiddle online. Could it offer the same versatility of LINQPad and the online sharing of JSFiddle.

I took the code from my last blog post and pasted it in .NET Fiddle. Interestingly by default code is evaluated and run as you enter it (you can turn this option off by setting Auto Run to 'No'). The first thing that occurred was that I got this error message:

Fatal Error: Memory usage limit was exceeded

Apparently the number of iterations in my test loop was too many. Once I dialed that back to 100,000 the code ran fine. This code is pretty basic and not creating a lot of large objects so this limit seems low. Also looks like no GC is running so this limit my hinder other tests.

It does have a 'Share' link that gives you either a direct link to the fiddle or a widget that you can embed directly in a blog post. Here is the widget for this post:

Definitely a nice tool for sharing code but think I will stick with LINQPad for most of my scratchpad needs.

Monday, December 7, 2015

All Your Base Are Belong To Us

I wanted to encode a number in Base 36. A simple enough task, but as these things sometimes do, this took me down the rabbit hole of wanting to create a generic routine. I needed a set of methods, one that would take an integer and convert it to an arbitrary base and another to convert it back to a Base 10 number. There are a bunch of examples floating around the web but I wanted to create my own (combining some of the best elements from the code I found). It's a relatively easy problem to solve but along the way I found some interesting tricks to speed up the process.

So here is my C# take on this old problem:

I used LINQPad to create a quick program to test it out.