假设我在Google电子表格中有帐号,子帐号和值,其中重复的帐号可以包含不同的子帐号:
acc no / sub acc no / val
2 / 5 / 6
3 / 10 / 8
4 / 9 / 2
2 / 14 / 1
在我按照第三列的值对整张纸进行排序后,它会是这样的:
acc no / sub acc no / val
3 / 10 / 8
2 / 5 / 6
4 / 9 / 2
2 / 14 / 1
所以,我想要做的是分组同一帐户下的所有电表,同时原始排序保持不变:
acc no / sub acc no / val
3 / 10 / 8
2 / 5 / 6
2 / 14 / 1
4 / 9 / 2
似乎在按值排序之后,我需要一个循环通过帐户No列的脚本,对于每个单元格,它将检查剩余的帐号,如果有匹配的重复号码,它会被提升。
这是我到目前为止所能提出的:
function group(){
var sheet = SpreadsheetApp.getActiveSpreadsheet();
// find how many accounts we have
var Avals = sheet.getRange("A2:A").getValues();
var Alast = Avals.filter(String).length;
for (var i = 0; i < Alast-2; i++) {
if(Avals[i].toString() == Avals[i+1].toString()){
continue;
}
for (var j = i+2; j < Alast; j++){
if(Avals[i].toString() == Avals[j].toString()){
//move
break;
}
}
}
}
提前致谢!
答案 0 :(得分:1)
这是一个示例脚本。未经测试。假设工作表已按C列排序。
function group(){
var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
// find how many accounts we have
//get A2:C range& values
var rng = sheet.getRange(2,1,sheet.getLastRow()-1,3);
var val = rng.getValues();
var data =[];//output array
var k=0; //data array new row count
//loop through existing rows
for(i=0;i<val.length;i++){
//if A is not empty, new k row data array = original row.
if (val[i][0]) { //value check
data[k] = val[i];
k++; //increment k to next row
//Loop through rows after current row to find next similar A value,if any
for(j=i+1;j<val.length;j++) {
if (val[i][0] == val[j][0]) { //if one of the folllowing row's A value is == current A value
data[k] =val[j].slice(0); //new k data array = j row
k++; //increment k
val[j][0] =''; //set the duplicate j row's A value to null to skip during value check
}
}
}
}
//Logger.log(data);
sheet.getRange(2,1,data.length,data[0].length).setValues(data);
}