Help

Re: How can I randomize the order of a CSV string in an airtable field?

2975 0
cancel
Showing results for 
Search instead for 
Did you mean: 
Ben_Baldwin
5 - Automation Enthusiast
5 - Automation Enthusiast

Does anyone know a script that I can use as follows: 1) take a field that has a string of 10-12 Comma Separated Values (CSV) and 2) randomize the order of those 10-12 values?

I’m not a technical person, so I’m sorry if this is a rudimentary question…

5 Replies 5

Part 1

let myArray = csvString.split(",");

Part 2

let myShuffledArray = shuffle(myArray);

function shuffle(array) {
  var currentIndex = array.length, temporaryValue, randomIndex;

  // While there remain elements to shuffle...
  while (0 !== currentIndex) {

    // Pick a remaining element...
    randomIndex = Math.floor(Math.random() * currentIndex);
    currentIndex -= 1;

    // And swap it with the current element.
    temporaryValue = array[currentIndex];
    array[currentIndex] = array[randomIndex];
    array[randomIndex] = temporaryValue;
  }

  return array;
}

Thanks Bill…I’m really not technical. Is it possible to walk me through a little bit re how to do this? This is my first time using this block…or attempting to “code” for that matter. Think of me like a 7-year old child, though I’m sure that’s an insult to many 7-year olds :frowning:

They can handle it.

Here’s an example…

  1. Create a script block
  2. Delete all the sample code it includes
  3. Replace the code with the code provided here
  4. Click Run

You should see something like this and each time you run it, the result will be different. Note - this doesn’t read the string from Airtable nor does it write the new randomized order back into Airtable. For that we need a little more code and ideally fit to your specific solution.

image

Script Code

output.markdown("# Shuffle Array");

// create the ordered list
let csvString       = "a,b,c,d,e,f,g";
output.markdown("## Ordered List");
output.inspect(csvString);

// parse the string into an array
let myArray         = csvString.split(",");
output.markdown("## Ordered Array");
output.inspect(myArray);

// shuffle the array
let myShuffledArray = shuffle(myArray);
output.markdown("## Shuffled Array");
output.inspect(myShuffledArray);

//
// shuffle array
//
function shuffle(array) {

    // initialize som needed variables
    var currentIndex = array.length, temporaryValue, randomIndex;

    // While there remain elements to shuffle...
    while (0 !== currentIndex) {

        // Pick a remaining element...
        randomIndex = Math.floor(Math.random() * currentIndex);
        currentIndex -= 1;

        // And swap it with the current element.
        temporaryValue = array[currentIndex];
        array[currentIndex] = array[randomIndex];
        array[randomIndex] = temporaryValue;
    }

    // return the new array
    return array;

}

Thanks so much, Bill.

I’ll try this out tonight.

Hi Bill, can we continue this to include getting the data back into the cell it was copied from?