Show a Number of Randomly Selected Rows from a Grid

Important Update to Custom Scripting

The CustomScript Action now supports the LUA programming language. Visit our NEW Lua Scripting Resources!


  1. New accounts (created after October 29, 2018) will only have the option to use Lua in scripts.
  2. As of October 29, 2018 Custom Scripting Actions will default to Lua as the scripting type in the Custom Scripting Action for accounts created before this date. You will be able to switch to the Legacy Custom Scripting; though we highly encourage using Lua.
  3. In the long term, Legacy Custom Scripting Actions will be switched to read-only. The exact date on this is to be determined; we will send notifications well ahead of time.

As part of our core randomization options you have the ability to randomize columns and randomize row order in a grid question. Unfortunately, there is not a built-in way to only randomly show a subset of the rows in your grid question. Never fear! We'll cover the steps to use a pretty simple script to accomplish just this. These steps assume a basic familiarity with Alchemer and programming.

Test it out in the survey here:

http://survey.alchemer.com/s3/2594372/x-random-rows

OR

Add this survey to your account!

Features used in this confession:

  • Our Custom Scripting action
  • Our Custom Scripting function sgapiExplode
  • Our Custom Scripting function sgapiShuffle
  • Our Custom Scripting function sgapiHideQuestion
  • PHP foreach loops

Survey Setup

Set up your grid question. Our example grid question looks like the below.

In the interest of reducing survey fatigue and survey abandonment and ultimately getting better data, we only want to burden each respondent with responding to three of the ten rows in this grid question.

To set this up we added a Custom Script Action to the top of the page with the grid question and pasted the below script. The highlighted portions of the script can be modified with the question IDs for the rows that you would like to randomly select from and the number of random rows you would like to show.

//Row IDs to be randomly hidden go here. Make sure they are comma separated, no spaces, and inside quotes.
%%questionsHideRan = "8,9,11,12,13,14,15,16,17,18" ;

//Number of rows you want shown from the list above. No quotes or commas are needed.
%%shown = 3;

//Do not change after this line.
%%arr = sgapiExplode(',',%%questionsHideRan);
sgapiShuffle(%%arr);
foreach(%%arr as %%question)
{
sgapiHideQuestion(%%question,true);
}
for (%%i=0; %%i<%%shown; %%i++)
{
sgapiHideQuestion(%%arr[''.%%i],false);
}

You're all set! Be sure to test it out to ensure that the rows are being randomly displayed.

Scripting and Other Out-of-the-Box Customizations

We’re always happy to help you debug any documented script. That said, we do not have the resources to write scripts on demand.

If you have customization ideas that you haven't figured out how to tackle, we're happy to be a sounding board for Alchemer features and functionality ideas that might meet your customization. Beyond this, you might want to consult with someone on our Programming Services Team; these folks might have the scripting chops to help you to achieve what you are looking for!

@plans @pro @ent