如何使用 google 应用程序脚本随机播放每一列?

How to shuffle every second column with google apps script?

我想随机化 D:E、F:G、H:I、J:K 如果 C 是 4,使用 google apps 脚本。

目前我使用这个低效且耗时的代码:

function shuffleAnswers() {
 var arr = [0, 2, 4, 6]; 
 for (var i = 2; i < lastRow()+1; i++)
 {
   var amount = sheet().getRange(i,3).getValue();
   if (amount == 4)
   {
     var source = sheet().getRange(i,4,1,2);
     var column = 4 + arr[(Math.random() * arr.length) | 0];
     var destination = sheet().getRange(i,column,1,2);
     var valuesSource = source.getValues();
     var valuesDestination = destination.getValues();
     source.setValues(valuesDestination);
     destination.setValues(valuesSource);     
   }
 }
 arr = [-2, 0, 2, 4]; 
 for (var i = 2; i < lastRow()+1; i++)
 {
   var amount = sheet().getRange(i,3).getValue();
   if (amount == 4)
   {
     var source = sheet().getRange(i,6,1,2);
     var column = 6 + arr[(Math.random() * arr.length) | 0];
     var destination = sheet().getRange(i,column,1,2);
     var valuesSource = source.getValues();
     var valuesDestination = destination.getValues();
     source.setValues(valuesDestination);
     destination.setValues(valuesSource);     
   }
 }
 arr = [-4, -2, 0, 2]; 
 for (var i = 2; i < lastRow()+1; i++)
 {
   var amount = sheet().getRange(i,3).getValue();
   if (amount == 4)
   {
     var source = sheet().getRange(i,8,1,2);
     var column = 8 + arr[(Math.random() * arr.length) | 0];
     var destination = sheet().getRange(i,column,1,2);
     var valuesSource = source.getValues();
     var valuesDestination = destination.getValues();
     source.setValues(valuesDestination);
     destination.setValues(valuesSource);     
   }
 }

 arr = [-6, -4, -2, 0]; 
 for (var i = 2; i < lastRow()+1; i++)
 {
   var amount = sheet().getRange(i,3).getValue();
   if (amount == 4)
   {
     var source = sheet().getRange(i,10,1,2);
     var column = 10 + arr[(Math.random() * arr.length) | 0];
     var destination = sheet().getRange(i,column,1,2);
     var valuesSource = source.getValues();
     var valuesDestination = destination.getValues();
     source.setValues(valuesDestination);
     destination.setValues(valuesSource);     
   }
 }
}

你有什么想法吗?也许用 range.randomize()? C=4 的每一行都应随机化。多行的列不应更改为相同的位置。

为什么这么慢?

  • 在循环中使用 getValues 会大大降低脚本速度。批量操作很重要。

脚本流程:

  • 创建一个 1 到 8 之间的随机数字数组,例如 [[1,2,5,6,3,4,7,8]],然后使用这些数字作为新行的索引。
  • 从 sheet 中获取所有值并仅重新排列 C=4 的行并一次性设置所有值。

示例脚本:

/* Create a Random arrray of numbers from 1 to 8 with couples
 * eg:[1,2,5,6,3,4,7,8]*/
const doubleShuffleFix = (n = 4) => {
  const generator = function*() {//TODO: boilerplate- can be avoided 
    let i = 1;
    yield i;
    while (true) {
      if (i < (n - 1) * 2) {
        yield (i += 2);
      } else {
        break;
      }
    }
  };
  const available = [...generator()];

  //Durstenfeld algo
  for (let i = available.length - 1; i > 0; i--) {
    let rand = Math.floor(Math.random() * i);
    [available[i], available[rand]] = [available[rand], available[i]];
  }
  return available.map(num => [num, ++num]).flat();
};

function shuffleAnswer() {
  const s = SpreadsheetApp.getActive().getSheetByName('Sheet1'),
    rg = s.getRange(2, 3, s.getLastRow() - 1, 9),
    values = rg.getValues();

  rg.setValues(
    values.map(row => {
      if (row[0] === 4) {
        return [4, ...doubleShuffleFix().map(num => row[num])];
      }
      return row;
    })
  );
}

参考文献: