3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

【GAS】カラムの内容をシャッフルする

Last updated at Posted at 2017-04-09

準備

配列をシャッフルする関数を作成

function shuffle(array){
  var result = [];
  for(i = array.length; i > 0; i--){
    var index = Math.floor(Math.random() * i);
    var val = array.splice(index, 1)[0];
    result.push(val);
  }

  return result;
}

使用例

スプレッドシートの内容が以下だとして、

メンバ 当番     
佐藤 朝礼
鈴木 炊事
高橋 点呼
田中 掃除
伊藤 風呂

以下のコードを実行すると、

function shuffleMembers(){
  var sheet = SpreadsheetApp.getActiveSheet();
  var lastRow = sheet.getLastRow();
  var ranges = sheet.getRange(2, 1, lastRow-1);
  var values = ranges.getValues();

  ranges.clear();
  ranges.setValues(shuffle(values));
}

名前部分のみシャッフルされる

メンバ 当番     
高橋 朝礼
鈴木 炊事
佐藤 点呼
伊藤 掃除
田中 風呂
3
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?