vendredi 3 décembre 2021

Javascript to get the Random sample of data

enter image description here

Hi all, I am working on a dataset on Google Sheet and I need to get the random data (specified number) by the col 3 names to audit I am using google app script for the same but couldn't able to get the data. Here's the code I tried but I don't want the % of data I want to get it. I want the equal distributed random data for each employee.

 function myFunction() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var trn = ss.getSheetByName('Data');
  var originalData = trn.getRange(2, 1, trn.getLastRow() - 1, 3).getValues();

  var ReviewerEmail = data;

  var data = originalData.filter(function(item) {
    return item[1] === 'rob' || item[1] === 'john' || item[1] === 'toger';
  });

  //Logger.log(data);

  var targetsheet = ss.insertSheet(Reviewer);
  targetsheet.getRange(1, 1, data.length, data[0].length).setValues(data);
}
function getsampledata() {
  var sheet = SpreadsheetApp.getActiveSheet();
  var range = sheet.getRange('A:C');
  var values = range.getValues();
  var headers = values.shift();
  var nameColumn = 1;
  var uniqueNames = values
    .map((row) => row[nameColumn])
    .filter((item, i, ar) => ar.indexOf(item) === i)
    .filter(String);
  var data = [headers];
  uniqueNames.forEach(function(name) {
    var nameEntries = values.filter((row) => row[nameColumn] == name);
    var entries = nameEntries.length;
    var tenth = Math.round((entries / 100) * 27.35); //Sampling Percentage
    for (i = 0; i < tenth; i++) {
      var random = Math.floor(Math.random() * nameEntries.length);
      data.push(nameEntries[random]);
      nameEntries.splice(random, 1);
    }
  });
  return data;
}



Aucun commentaire:

Enregistrer un commentaire