0

列Aから名前のリストを取得し、リストをランダム化してから、できるだけ(除算の余りを含めて)それらを均等にユーザー指定のグループ数。配列の値を貼り付ける際に列と行を移動する

私が必要なものの例は次のようである:名前の

リスト:3のA、B、C、D、E、F、G、H、I、J、K

結果グループ:

  • グループ1:D、A、F
  • グループ2:B、H、G、K
  • グループ3:E、C、I、J

編集済み:コードをクリーンアップしました。空の配列に名前のリストを割り当て、配列を正常にランダム化しました。今度は、これらの値をグループごとに独自の列に貼り付ける方法を理解する必要があります。どのように私は右にしても余り(最初の値は、各列のヘッダーです)を占め、各列の下の値を貼り付けます:

  • 列C:グループ1、D、A、F
  • カラムD:グループ2、B、H、G、K
  • カラムE:グループ3、E、C、I、J

これは私がこれまで持っているものである。

function onOpen() { 
SpreadsheetApp.getUi() // Or DocumentApp or FormApp. 
    .createMenu('Custom Menu') 
    .addItem('Show prompt', 'showPrompt') 
    .addToUi(); 
} 

function SortNames() { 
var ui = SpreadsheetApp.getUi(); 

var result = ui.prompt(
    'How many groups?', 
    ui.ButtonSet.OK_CANCEL); 

// Process the user's response. 
var button = result.getSelectedButton(); 
var groupquantity = result.getResponseText(); 
if (button == ui.Button.OK) { 
// User clicked "OK" - Need to clear the cells from the previous sorting in this step 

// Get the last row number of the names list 
var Avals = SpreadsheetApp.getActiveSheet().getRange("A1:A").getValues(); 
var Alast = Avals.filter(String).length; 

// Set an empty Array 
var ar = []; 

/****** In its original order, append the names to the array *****/ 
for (var i = 2; i < Alast+1; i++) { 

    var source = 'A' + i; 
    var Avals = SpreadsheetApp.getActiveSheet().getRange(source).getValues(); 
    ar.push(Avals); 
} 
/***************************/ 

/****** Shuffle the array *****/ 
function shuffle(a) { 
    var j, x, i; 
    for (i = a.length; i; i--) { 
    j = Math.floor(Math.random() * i); 
    x = a[i - 1]; 
    a[i - 1] = a[j]; 
    a[j] = x; 
    } 
} 

shuffle(ar); 
/***************************/ 


/****** Calculates the rounded down # of members per group *****/ 
var memberspergroup = ar.length/groupquantity; 
var memberspergroup = Math.floor(memberspergroup); 
/*********************************/ 



/****** Copy and Paste the rounded down number of members to each groups until 
the remainder is 0, then distribute evenly with remaining number of groups *****/ 

// First Cell location to paste 
var pasteloc = "C1"; 
for (var i = 1; i <= groupquantity; i++) { 

    SpreadsheetApp.getActiveSheet().getRange(pasteloc).setValue('Group ' + i); 
    var source = 'A' + i; 
    var Avals = SpreadsheetApp.getActiveSheet().getRange(source).getValues(); 
} 
/*********************************/ 



} 

else if (button == ui.Button.CANCEL) { 
// User clicked "Cancel". 
ui.alert('The request has been cancelled'); 
} 
else if (button == ui.Button.CLOSE) { 
// User clicked X in the title bar. 
ui.alert('You closed the dialog.'); 
} 

}

+1

[this](http://stackoverflow.com/questions/6274339/how-can-i-shuffle-an-array-in-javascript)は役に立ちますか? –

答えて

0

私は私の質問に対する解決策を見つけました。これは最良の形ではありません。名前リストの空のセルを考慮して改良を加えることができます。しかし、それは適切に機能し、私が探していたすべてのことを行います。

  • それは、配列
  • の名前のリストは配列
  • は、同じように(残りのアカウントをとる完全に均等に配布ランダムに割り当て、私は上記の例)

    function onOpen() { 
        SpreadsheetApp.getUi() // Or DocumentApp or FormApp. 
        .createMenu('Custom Menu') 
        .addItem('Show prompt', 'showPrompt') 
        .addToUi(); 
    /******closing function onOpen()*********************/ 
    } 
    
    function SortNames() { 
        var ui = SpreadsheetApp.getUi(); 
        var ss = SpreadsheetApp.getActiveSpreadsheet(); 
        var sheet = ss.getActiveSheet(); 
    
        var result = ui.prompt(
        'How many groups?', 
        ui.ButtonSet.OK_CANCEL); 
    
        // Process the user's response. 
        var button = result.getSelectedButton(); 
        var groupquantity = result.getResponseText(); 
        if (button == ui.Button.OK) { 
        // User clicked "OK" 
    
        // Get the last row number of the names list 
        var Avalues = sheet.getRange("A1:A").getValues(); 
        var Alast = Avalues.filter(String).length; 
    
        if(groupquantity > 0 && groupquantity <= Alast) 
        { 
        // User inputted a valid group quantity - Need to clear the cells from the previous sorting in this step 
        var lastRow = SpreadsheetApp.getActiveSheet().getMaxRows(); 
        sheet.getRange('C1:Z'+lastRow).clearContent(); 
    
        // Set an empty Array 
        var ar = []; 
    
        /****** In its original order, append the names to the array *****/ 
        for (var i = 2; i < Alast+1; i++) { 
    
        var source = 'A' + i; 
        var Avals = sheet.getRange(source).getValues(); 
    
        ar.push(Avals); 
    
        } 
        /***************************/ 
    
        /****** Shuffles array *****/ 
        function shuffle(a) { 
        var j, x, i; 
        for (i = a.length; i; i--) { 
         j = Math.floor(Math.random() * i); 
         x = a[i - 1]; 
         a[i - 1] = a[j]; 
         a[j] = x; 
        } 
        /**********closing function shuffle(a)*****************/ 
        } 
    
        shuffle(ar); 
    
        /****** Calculates the rounded down # of members per group *****/ 
        var memberspergroup = Math.floor(ar.length/groupquantity); 
        /*********************************/ 
    
    
        /****** Main Goal: Copy and Paste the rounded down number of members to each groups until 
        the remainder is 0, then distribute evenly with remaining number of groups *****/ 
        // 1. Define the first Cell location to paste 
        var pasteloc = "C1"; 
    
        // 2. Begin a for-loop: Navigate Horizontally across from the first cell location 
        for (var i = 1; i <= groupquantity; i++) 
        { 
    
         // 3. Set the Column Headings in the current column, i 
         sheet.getRange(1,i+2).setValue('Group ' + i); 
    
         /************** 4. Fill in the Rows of names for each groups **********/ 
         // List out the values in array "ar" by the expected group qty, until the remainder is zero 
    
         if ((ar.length)%(groupquantity-(i-1)) > 0) 
         { 
         for (var rows = 2; rows <= memberspergroup+1; rows++) 
         { 
          var j = 0; 
          sheet.getRange(rows,i+2).setValue(ar[j]); 
          var index = ar.indexOf(ar[j]); 
          ar.splice(index, 1); 
         } 
    
         } 
         else 
         { 
          var memberspergroup = ar.length/(groupquantity-(i-1)) 
          for (var rows = 2; rows <= memberspergroup+1; rows++) 
          { 
          var j = 0; 
          sheet.getRange(rows,i+2).setValue(ar[j]); 
          var index = ar.indexOf(ar[j]); 
          ar.splice(index, 1); 
          } 
         } 
    
        } 
        /*********************************/ 
    
    } 
        /*****************closing if(groupquantity > 0 && groupquantity <= Alast)****************/ 
    
    else{ 
    ui.alert("Error: " + '"' + groupquantity + '"' +" is not a proper group quantity") 
    } 
    
    
    
    
        } 
    
        else if (button == ui.Button.CANCEL) { 
        // User clicked "Cancel". 
        } 
    else if (button == ui.Button.CLOSE) { 
    // User clicked X in the title bar. 
    } 
    
    
    } 
    

私はこれが役に立てば幸い!

関連する問題