Skip to main content

You can use get* methods to get the attributes and formats for the cells you wish to shuffle, then reorder themthe cells all in a consistent random way, and then use the set* method to assign after reordering.

There isare a lot of get* methods: various font attributes, data validation rules, text alignment, cell backgrounds, etc. In the example below I only work with font weights (like bold), font styles (like italic), and data validation rules.

function shuffleRange(range) {
  var height = range.getHeight();
  var indexArray = new Array(height); 
  for (var i = 0; i < height; i++) {
    indexArray[i] = i; 
  }
  indexArray = shuffleArray(indexArray);
  //  indexArray holds numbers 0, 1, 2, 3 ... in random order
  
  range.setValues(range.getValues().map(reorder));
  range.setFontWeights(range.getFontWeights().map(reorder));
  range.setFontStyles(range.getFontStyles().map(reorder));
  range.setDataValidations(range.getDataValidations().map(reorder));
  //  add more such lines using get* and set* methods of Range object
  
  function reorder(x, i, arr) {
    return arr[indexArray[i]];
  }
}

An alternativealternative is to use the copyTo method which can carry all the attributes at once. But then, but then one would have to copy cell by cell, which is probably a lot slowedslower when you have a large range.

You can use get* methods to get the attributes and formats you wish to shuffle, then reorder them all in a consistent random way, and use set* method to assign after reordering.

There is a lot of get* methods: various font attributes, data validation rules, text alignment, cell backgrounds, etc. In the example below I only work with font weights (like bold), font styles (like italic), and data validation rules.

function shuffleRange(range) {
  var height = range.getHeight();
  var indexArray = new Array(height); 
  for (var i = 0; i < height; i++) {
    indexArray[i] = i; 
  }
  indexArray = shuffleArray(indexArray);
  //  indexArray holds numbers 0, 1, 2, 3 ... in random order
  
  range.setValues(range.getValues().map(reorder));
  range.setFontWeights(range.getFontWeights().map(reorder));
  range.setFontStyles(range.getFontStyles().map(reorder));
  range.setDataValidations(range.getDataValidations().map(reorder));
  //  add more such lines using get* and set* methods of Range object
  
  function reorder(x, i, arr) {
    return arr[indexArray[i]];
  }
}

An alternative is to use the copyTo method which can carry all the attributes at once. But then one would have to copy cell by cell, which is probably a lot slowed when you have a large range.

You can use get* methods to get the attributes and formats for the cells you wish to shuffle, then reorder the cells all in a consistent random way, and then use the set* method to assign after reordering.

There are a lot of get* methods: various font attributes, data validation rules, text alignment, cell backgrounds, etc. In the example below I only work with font weights (like bold), font styles (like italic), and data validation rules.

function shuffleRange(range) {
  var height = range.getHeight();
  var indexArray = new Array(height); 
  for (var i = 0; i < height; i++) {
    indexArray[i] = i; 
  }
  indexArray = shuffleArray(indexArray);
  //  indexArray holds numbers 0, 1, 2, 3 ... in random order
  
  range.setValues(range.getValues().map(reorder));
  range.setFontWeights(range.getFontWeights().map(reorder));
  range.setFontStyles(range.getFontStyles().map(reorder));
  range.setDataValidations(range.getDataValidations().map(reorder));
  //  add more such lines using get* and set* methods of Range object
  
  function reorder(x, i, arr) {
    return arr[indexArray[i]];
  }
}

An alternative is to use the copyTo method which can carry all the attributes at once, but then one would have to copy cell by cell, which is probably a lot slower when you have a large range.

Source Link
user135384
user135384

You can use get* methods to get the attributes and formats you wish to shuffle, then reorder them all in a consistent random way, and use set* method to assign after reordering.

There is a lot of get* methods: various font attributes, data validation rules, text alignment, cell backgrounds, etc. In the example below I only work with font weights (like bold), font styles (like italic), and data validation rules.

function shuffleRange(range) {
  var height = range.getHeight();
  var indexArray = new Array(height); 
  for (var i = 0; i < height; i++) {
    indexArray[i] = i; 
  }
  indexArray = shuffleArray(indexArray);
  //  indexArray holds numbers 0, 1, 2, 3 ... in random order
  
  range.setValues(range.getValues().map(reorder));
  range.setFontWeights(range.getFontWeights().map(reorder));
  range.setFontStyles(range.getFontStyles().map(reorder));
  range.setDataValidations(range.getDataValidations().map(reorder));
  //  add more such lines using get* and set* methods of Range object
  
  function reorder(x, i, arr) {
    return arr[indexArray[i]];
  }
}

An alternative is to use the copyTo method which can carry all the attributes at once. But then one would have to copy cell by cell, which is probably a lot slowed when you have a large range.