首页 > 解决方案 > 再次单击随机化功能时如何随机化名称而不连续重复其名称?

问题描述

再会。我正在制定一个计划,每月向 46 名成员的 IT 员工发送调查。鉴于谷歌工作表中谷歌应用程序脚本中的代码,当我在第 2 次到第 6 次运行随机化函数时,如何限制费率的名称再次出现在同一行。谢谢你。

关于代码:

问题:鉴于以下数据,当您运行 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 个月对同一个人进行评分。

标签: randomgoogle-apps-scriptgoogle-sheets

解决方案


不要运行该功能 2 到 6 次。使用 30 列(5x6)运行一次函数。第一次使用前 5 列,第二次使用接下来的 5 列,依此类推。

var finalArr = getRandUniqMatrix(30, 46).map(function(row){return row.map(function(col){return data1d[col]})}); 

推荐阅读