我正在使用 Google 表格中的自动化。你能帮帮我吗?
此问题用于向 46 人发送调查。每个人需要对这 46 个人中的 5 个人进行评分。
Requirements:
1. 1 rater, for 5 uniques ratees
2. No duplicate name per row (it should be 6 unique names in a row)
3. No duplicate name per column (it should be 46 unique names per column)
我们的预期输出是创建 46x6 的随机名称,行和列中没有重复项。
- -
最佳答案
流程:
如果可以创建横跨和下方的唯一矩阵,那么它的值可以用作实际名称数组的键。
- 创建一个长度为行数的二维数组
- 遍历所需数量的列和行
- 创建一个临时数组(
tempCol
)来存储当前列数据 - 用随机数填充数组
- 使用
indexOf
确定当前行/当前列中是否已经存在任何随机数,如果存在,则获取一个新的随机数。 - 在随机情况下,如果不可能用上下唯一的随机数填充临时列,请删除临时列并重做此迭代。
片段:
function getRandUniqMatrix(numCols, numRows) {
var maxIter = 1000; //Worst case number of iterations, after which the loop and tempCol resets
var output = Array.apply(null, Array(numRows)).map(function(_, i) {
return [i++]; //[[0],[1],[2],...]
});
var currRandNum;
var getRandom = function() {
currRandNum = Math.floor(Math.random() * numRows);
}; //get random number within numRows
while (numCols--) {//loop through columns
getRandom();
for (
var row = 0, tempCol = [], iter = 0;
row < numRows;
++row, getRandom()
) {//loop through rows
if (//unique condition check
!~output[row].indexOf(currRandNum) &&
!~tempCol.indexOf(currRandNum)
) {
tempCol.push(currRandNum);
} else {
--row;
++iter;
if (iter > maxIter) {//reset loop
iter = 0;
tempCol = [];
row = -1;
}
}
}
output.forEach(function(e, i) {//push tempCol to output
e.push(tempCol[i]);
});
}
return output;
}
console.info(getRandUniqMatrix(6, 46));
var data1d = data.map(function(e){return e[0]});
var finalArr = getRandUniqMatrix(6, 46).map(function(row){return row.map(function(col){return data1d[col]})});
destSheet.getRange(1,1,finalArr.length, finalArr[0].length).setValues(finalArr);
关于javascript - 如何自动随机化 46 个名称以在 Google 表格中创建 46 x 6 唯一行和列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56519601/