random - 再次单击随机化功能时如何随机化名称而不连续重复其名称?
问题描述
再会。我正在制定一个计划,每月向 46 名成员的 IT 员工发送调查。鉴于谷歌工作表中谷歌应用程序脚本中的代码,当我在第 2 次到第 6 次运行随机化函数时,如何限制费率的名称再次出现在同一行。谢谢你。
关于代码:
- 它可以生成随机名称,每行没有重复,name1-name46。
- 它可以为每列生成随机名称,名称1-46,所有名称1-46都在每列中
- 当您重新运行给定的代码时,每行中的名称有可能再次出现在同一行中。
问题:鉴于以下数据,当您运行 randomize 函数第 2-6 次时,ratee 1-5 不应再次出现在同一行中。
function randomize() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheetname = "Emails";
var sheet = ss.getSheetByName(sheetname);
// some variables
var randomcount = 2; // how many random names
var rowstart = 2; // ignore row 1 - the header row
var width = 6; // how many names in each row - 1/rater plus 5/ratee
var thelastrow = sheet.getLastRow();
//Logger.log("DEBUG:last row = "+thelastrow)
// get the employee names
var employeecount = thelastrow-rowstart+1;
//Logger.log("DEBUG: employee count = "+employeecount);//DEBUG
// get the data
var datarange = sheet.getRange(rowstart, 3, thelastrow - rowstart+1);
//Logger.log("DEBUG: range = "+datarange.getA1Notation());//DEBUG
var data = datarange.getValues();
//Logger.log("data length = "+data.length);
//Logger.log(data);
var data1d = data.map(function(e){return e[0]});
var finalArr = getRandUniqMatrix(5, 46).map(function(row){return row.map(function(col){return data1d[col]})});
sheet.getRange(2,4,finalArr.length, finalArr[0].length).setValues(finalArr);
}
//Credits to: TheMaster
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++];
});//[[1],[2],[3].....]
var getRandom = function() {
return Math.floor(Math.random() * numRows);
};//getrandom number within numRows
while (numCols--) {//loop through columns
for (
var row = 0, currRandNum = getRandom(), tempCol = [], iter = 0;
row < numRows;
++row
) {//loop through rows
//unique condition
if (!~output[row].indexOf(currRandNum) && !~tempCol.indexOf(currRandNum)) {
tempCol.push(currRandNum);
} else {
currRandNum = getRandom();//get a new random number
--row;
++iter;
if (iter > 1000) {//reset loop
iter = 0;
tempCol = [];
row = -1;
}
}
if (row + tempCol.length + 1 === numRows * 2) {//last row, Combine output+tempCol
output.forEach(function(e, i) {
return e.push(tempCol[i]);
});
}
}
}
return output;
}
console.info(getRandUniqMatrix(5, 46));
预期结果:
当您在第 2 次、第 3 次、第 4 次、第 5 次和第 6 次运行随机化功能时,每行费率的名称完全不同。在发送调查的实际应用中,我们不想在下个月或直到第 6 个月对同一个人进行评分。
解决方案
不要运行该功能 2 到 6 次。使用 30 列(5x6)运行一次函数。第一次使用前 5 列,第二次使用接下来的 5 列,依此类推。
var finalArr = getRandUniqMatrix(30, 46).map(function(row){return row.map(function(col){return data1d[col]})});
推荐阅读
- r - 如何修复 Rmarkdown 中的“yaml.load 未使用的参数”
- python - 绘制 Pandas Query 和 Eval 函数性能的折线图
- java - Httpclient 4.5 需要双倍时间来关闭 http 连接
- rubygems - 如何在 Appium ruby 客户端库中获取当前时间戳
- typescript - google.script 的“未解析变量或类型 google”
- selenium - endWhile 不工作,如果不编辑索引就无法启动 while
- javascript - Nativescript - 具有嵌套数据的中继器
- javascript - 如何根据百分比值更改图标列表的颜色?
- c++ - FFMPEG解码使用GPU渲染?
- c++ - C++ rw/rwdate.h 库没有这样的文件或目录