Back to How-Tos

Manual Counter in Designer!

While the Generate Counter script is really, really useful for things like Raffle Tickets, it's unfortunately unuseable if you already have your own Data File as it cannot complement that data file automatically.

The script below can be used instead of the Generate Counter, and uses the current record index to calculate the current counter value. Note that we added one additional bonus feature to this script : you can "reset" it every nth step! Pure Awesomeness.

To use this script simply add a placeholder to your page (such as @counter@ ) and create a Script with the @counter@ Text selector. Then paste the code below in the script editor:

// CONFIGURATION

// Beginning value
var startValue = 1;
// Increment by this number every step
var incrementValue = 1;
// Reset to startValue after this number of steps (use 0 for no reset)
var stepToReset = 0;
// To pad the counter, set the min width: 
var counterWidth = 0;
// If padding, set the character to pad with: 
var paddingChar = "0";
// Add an optional prefix: 
var prefix = "";
// Add an optional suffix: 
var suffix = "";


// CODE - DO NOT CHANGE
var counter = record.index;
if(stepToReset) counter = ((record.index-1) % stepToReset) + 1;
if(incrementValue) counter = (counter * incrementValue)-incrementValue;
if(startValue) counter = startValue + counter;
if(counterWidth) counter = pad(counter, counterWidth, paddingChar);
counter = prefix + counter + suffix;

results.text(counter);

// Stolen from StackOverflow
function pad(n, width, z) {
  z = z || '0';
  n = n + '';
  return n.length >= width ? n : new Array(width - n.length + 1).join(z) + n;
}