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.