在一个函数中组合范围
Combine ranges in one function
我有一个函数可以随机排列具有不同数量值的多个列。对于每一列,我都有一个函数来打乱该列中的值。我将所有这些函数组合成一个函数,对所有列进行洗牌。
function shuffleLevel345() {
shuffleColumnA();
shuffleColumnB();
shuffleColumnC();
shuffleColumnD();
shuffleColumnE();
}
function shuffleColumnA() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('level_345');
var range = sheet.getRange("A1:A101");
range.setValues(shuffleArray(range.getValues()));
}
function shuffleColumnB() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('level_345');
var range = sheet.getRange("B1:B99");
range.setValues(shuffleArray(range.getValues()));
}
function shuffleColumnC() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('level_345');
var range = sheet.getRange("C1:C89");
range.setValues(shuffleArray(range.getValues()));
}
function shuffleColumnD() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('level_345');
var range = sheet.getRange("D1:D124");
range.setValues(shuffleArray(range.getValues()));
}
function shuffleColumnE() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('level_345');
var range = sheet.getRange("E1:E75");
range.setValues(shuffleArray(range.getValues()));
}
我可以将它组合成一个具有所有范围的函数吗?
我试过:
var ranges = sheet.getRangeList(['A1:A101','B1:B99','C1:C89','D1:D124','E1:E75']);
但这没有用。
大概可以这样用一个循环来完成:
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('level_345');
var ranges = ['A1:A101','B1:B99','C1:C89','D1:D124','E1:E75'];
ranges.forEach(r => shuffleRange(r, sheet));
function shuffleRange(r, sheet) {
var range = sheet.getRange(r);
range.setValues(shuffleArray(range.getValues()));
}
或者这样:
const ss = SpreadsheetApp.getActiveSpreadsheet();
const sheet = ss.getSheetByName('level_345');
const ranges = ['A1:A101','B1:B99','C1:C89','D1:D124','E1:E75'];
for (let r of ranges) {
let range = sheet.getRange(r);
range.setValues(shuffleArray(range.getValues()));
}
我有一个函数可以随机排列具有不同数量值的多个列。对于每一列,我都有一个函数来打乱该列中的值。我将所有这些函数组合成一个函数,对所有列进行洗牌。
function shuffleLevel345() {
shuffleColumnA();
shuffleColumnB();
shuffleColumnC();
shuffleColumnD();
shuffleColumnE();
}
function shuffleColumnA() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('level_345');
var range = sheet.getRange("A1:A101");
range.setValues(shuffleArray(range.getValues()));
}
function shuffleColumnB() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('level_345');
var range = sheet.getRange("B1:B99");
range.setValues(shuffleArray(range.getValues()));
}
function shuffleColumnC() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('level_345');
var range = sheet.getRange("C1:C89");
range.setValues(shuffleArray(range.getValues()));
}
function shuffleColumnD() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('level_345');
var range = sheet.getRange("D1:D124");
range.setValues(shuffleArray(range.getValues()));
}
function shuffleColumnE() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('level_345');
var range = sheet.getRange("E1:E75");
range.setValues(shuffleArray(range.getValues()));
}
我可以将它组合成一个具有所有范围的函数吗?
我试过:
var ranges = sheet.getRangeList(['A1:A101','B1:B99','C1:C89','D1:D124','E1:E75']);
但这没有用。
大概可以这样用一个循环来完成:
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('level_345');
var ranges = ['A1:A101','B1:B99','C1:C89','D1:D124','E1:E75'];
ranges.forEach(r => shuffleRange(r, sheet));
function shuffleRange(r, sheet) {
var range = sheet.getRange(r);
range.setValues(shuffleArray(range.getValues()));
}
或者这样:
const ss = SpreadsheetApp.getActiveSpreadsheet();
const sheet = ss.getSheetByName('level_345');
const ranges = ['A1:A101','B1:B99','C1:C89','D1:D124','E1:E75'];
for (let r of ranges) {
let range = sheet.getRange(r);
range.setValues(shuffleArray(range.getValues()));
}