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.');
}
}
[this](http://stackoverflow.com/questions/6274339/how-can-i-shuffle-an-array-in-javascript)は役に立ちますか? –