Google 工作表脚本,用于按列对从另一个选项卡导入的行进行排序
Google sheets script to sort rows imported from another tab by column
我是 google 工作表中的脚本新手,我正在尝试在脚本中添加一个函数以按列值按升序对范围进行排序。
我使用的脚本允许我根据付款状态将行从一个选项卡移动到另一个选项卡。所有导入的行都放在第 7 行中,就在 header 下方。结果,线条完全乱了。
我尝试了很多东西,以至于我迷路了。我不知道我是否尝试了错误的功能,或者我是否将它们放在脚本中的错误位置。
function onEdit(e) {
var sh = e.source.getActiveSheet();
var rng = e.source.getActiveRange();
if (sh.getName()=='Suivi Clients' && rng.getColumn()==21){
if(rng.getValue()=='OUI' || rng.getValue()=='ANNULÉ'){
var dest = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Clients Finalisé')
dest.insertRowBefore(7)
var plage = sh.getRange('C' + rng.getRow() + ':G' + rng.getRow())
plage.copyTo(dest.getRange("C7"), SpreadsheetApp.CopyPasteType.PASTE_VALUES, false)
var plage = sh.getRange('L' + rng.getRow() + ':N' + rng.getRow())
plage.copyTo(dest.getRange("L7"), SpreadsheetApp.CopyPasteType.PASTE_VALUES, false)
var plage = sh.getRange('P' + rng.getRow())
plage.copyTo(dest.getRange("P7"), SpreadsheetApp.CopyPasteType.PASTE_VALUES, false)
var plage = sh.getRange('U' + rng.getRow() + ':W' + rng.getRow())
plage.copyTo(dest.getRange("U7"), SpreadsheetApp.CopyPasteType.PASTE_VALUES, false)
sh.deleteRow(rng.getRow())
}
}
if (sh.getName()=='Clients Finalisé' && rng.getColumn()==21){
if(rng.getValue()=='NON'){
var dest = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Suivi Clients')
dest.insertRowBefore(7)
var plage = sh.getRange('C' + rng.getRow() + ':G' + rng.getRow())
plage.copyTo(dest.getRange("C7"), SpreadsheetApp.CopyPasteType.PASTE_VALUES, false)
var plage = sh.getRange('L' + rng.getRow() + ':N' + rng.getRow())
plage.copyTo(dest.getRange("L7"), SpreadsheetApp.CopyPasteType.PASTE_VALUES, false)
var plage = sh.getRange('P' + rng.getRow())
plage.copyTo(dest.getRange("P7"), SpreadsheetApp.CopyPasteType.PASTE_VALUES, false)
var plage = sh.getRange('U' + rng.getRow() + ':W' + rng.getRow())
plage.copyTo(dest.getRange("U7"), SpreadsheetApp.CopyPasteType.PASTE_VALUES, false)
sh.deleteRow(rng.getRow())
}
}
// other onEdit here
}
Dans l'onglet "Suivi Clients" je voudrais trier par colonne G et dans l'onglet "Clients Finalisé" je voudrais trier par colonne P.
这是我尝试过的功能之一:
function sort() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Suivi Clients");
var range = sheet.getRange("B6:W");
range.sort(7);
}
对范围进行升序排序
function sort() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Suivi Clients");
var range = sheet.getRange("B6:W" + (sheet.getLastRow()-5));
range.sort({column:7,ascending:true});
}
function onEdit(e) {
var sh = e.range.getSheet();
if (sh.getName() == 'Suivi Clients' && e.range.getColumn() == 21) {
if (e.range.getValue() == 'OUI' || e.range.getValue() == 'ANNULÉ') {
var dest = e.source.getSheetByName('Clients Finalisé');
dest.insertRowBefore(7)
sh.getRange('C' + e.range.rowStart + ':G' + e.range.rowStart).copyTo(dest.getRange("C7"), SpreadsheetApp.CopyPasteType.PASTE_VALUES, false);
sh.getRange('L' + e.range.rowStart + ':N' + e.range.rowStart).copyTo(dest.getRange("L7"), SpreadsheetApp.CopyPasteType.PASTE_VALUES, false);
sh.getRange('P' + e.range.rowStart).copyTo(dest.getRange("P7"), SpreadsheetApp.CopyPasteType.PASTE_VALUES, false);
sh.getRange('U' + e.range.rowStart + ':W' + e.range.rowStart).copyTo(dest.getRange("U7"), SpreadsheetApp.CopyPasteType.PASTE_VALUES, false);
sh.deleteRow(e.range.rowStart);
}
}
if (sh.getName() == 'Clients Finalisé' && e.range.getColumn() == 21 && e.value == 'NON') {
var dest = e.source.getSheetByName('Suivi Clients')
dest.insertRowBefore(7);
sh.getRange('C' + e.range.rowStart + ':G' + e.range.rowStart).copyTo(dest.getRange("C7"), SpreadsheetApp.CopyPasteType.PASTE_VALUES, false);
sh.getRange('L' + e.range.rowStart + ':N' + e.range.rowStart).copyTo(dest.getRange("L7"), SpreadsheetApp.CopyPasteType.PASTE_VALUES, false);
sh.getRange('P' + e.range.rowStart).copyTo(dest.getRange("P7"), SpreadsheetApp.CopyPasteType.PASTE_VALUES, false);
sh.getRange('U' + e.range.rowStart + ':W' + e.range.rowStart).copyTo(dest.getRange("U7"), SpreadsheetApp.CopyPasteType.PASTE_VALUES, false)
sh.deleteRow(e.range.rowStart);
}
}
这种排序对数组中的值进行排序,然后使用 setValues 将值放回电子表格中。如果您在此范围内使用公式,则可能会损坏公式。它还假定范围内的column7中的值都是日期。
function sort() {
var ss = SpreadsheetApp.getActive();
var sheet = ss.getSheetByName("Sheet0");
var vs = sheet.getRange("B6:W" + sheet.getLastRow()).getValues();
vs.sort((a, b) => {
let vA = new Date(a[5]).valueOf();
let vB = new Date(b[5]).valueOf();
return vA - vB;
})
sheet.getRange("B6:W" + sheet.getLastRow()).setValues(vs);
}
尝试
function sortSC() { // Suivi Clients
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Suivi Clients");
var range = sheet.getRange("B6:W" + (sheet.getLastDataRow(3)));
range.sort({column:7,ascending:true});
}
function sortCF() { // Clients Finalisé
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Clients Finalisé");
var range = sheet.getRange("B6:W" + (sheet.getLastDataRow(3)));
range.sort({column:16,ascending:true});
}
Object.prototype.getLastDataRow = function (col) {
var lastRow = this.getLastRow();
if (col == null) { col = 1 }
var range = this.getRange(lastRow, col);
if (range.getValue() !== "") {
return lastRow;
} else {
return range.getNextDataCell(SpreadsheetApp.Direction.UP).getRow();
}
};
仅供参考,完整脚本
function onEdit(e) {
var sh = e.source.getActiveSheet();
var rng = e.source.getActiveRange();
if (sh.getName() == 'Suivi Clients' && rng.getColumn() == 21) {
if (rng.getValue() == 'OUI' || rng.getValue() == 'ANNULÉ') {
var dest = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Clients Finalisé')
dest.insertRowBefore(7)
var plage = sh.getRange('C' + rng.getRow() + ':G' + rng.getRow())
plage.copyTo(dest.getRange("C7"), SpreadsheetApp.CopyPasteType.PASTE_VALUES, false)
var plage = sh.getRange('L' + rng.getRow() + ':N' + rng.getRow())
plage.copyTo(dest.getRange("L7"), SpreadsheetApp.CopyPasteType.PASTE_VALUES, false)
var plage = sh.getRange('P' + rng.getRow())
plage.copyTo(dest.getRange("P7"), SpreadsheetApp.CopyPasteType.PASTE_VALUES, false)
var plage = sh.getRange('U' + rng.getRow() + ':W' + rng.getRow())
plage.copyTo(dest.getRange("U7"), SpreadsheetApp.CopyPasteType.PASTE_VALUES, false)
sh.deleteRow(rng.getRow())
SpreadsheetApp.flush()
sortCF()
}
}
if (sh.getName() == 'Clients Finalisé' && rng.getColumn() == 21) {
if (rng.getValue() == 'NON') {
var dest = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Suivi Clients')
dest.insertRowBefore(7)
var plage = sh.getRange('C' + rng.getRow() + ':G' + rng.getRow())
plage.copyTo(dest.getRange("C7"), SpreadsheetApp.CopyPasteType.PASTE_VALUES, false)
var plage = sh.getRange('L' + rng.getRow() + ':N' + rng.getRow())
plage.copyTo(dest.getRange("L7"), SpreadsheetApp.CopyPasteType.PASTE_VALUES, false)
var plage = sh.getRange('P' + rng.getRow())
plage.copyTo(dest.getRange("P7"), SpreadsheetApp.CopyPasteType.PASTE_VALUES, false)
var plage = sh.getRange('U' + rng.getRow() + ':W' + rng.getRow())
plage.copyTo(dest.getRange("U7"), SpreadsheetApp.CopyPasteType.PASTE_VALUES, false)
sh.deleteRow(rng.getRow())
SpreadsheetApp.flush()
sortSC()
}
}
if (sh.getName() != 'Suivi Clients') return;
var editRange = {
top: 6,
left: 3,
right: 7
};
var thisRow = e.range.getRow();
if (thisRow < editRange.top || thisRow > editRange.bottom) return;
var thisCol = e.range.getColumn();
if (thisCol < editRange.left || thisCol > editRange.right) return;
removeEmpty()
}
function sortSC() { // Suivi Clients
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Suivi Clients");
var range = sheet.getRange("B6:W" + (sheet.getLastDataRow(3)));
range.sort({ column: 7, ascending: true });
}
function sortCF() { // Clients Finalisé
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Clients Finalisé");
var range = sheet.getRange("B6:W" + (sheet.getLastDataRow(3)));
range.sort({ column: 16, ascending: true });
}
Object.prototype.getLastDataRow = function (col) {
var lastRow = this.getLastRow();
if (col == null) { col = 1 }
var range = this.getRange(lastRow, col);
if (range.getValue() !== "") {
return lastRow;
} else {
return range.getNextDataCell(SpreadsheetApp.Direction.UP).getRow();
}
};
我是 google 工作表中的脚本新手,我正在尝试在脚本中添加一个函数以按列值按升序对范围进行排序。
我使用的脚本允许我根据付款状态将行从一个选项卡移动到另一个选项卡。所有导入的行都放在第 7 行中,就在 header 下方。结果,线条完全乱了。
我尝试了很多东西,以至于我迷路了。我不知道我是否尝试了错误的功能,或者我是否将它们放在脚本中的错误位置。
function onEdit(e) {
var sh = e.source.getActiveSheet();
var rng = e.source.getActiveRange();
if (sh.getName()=='Suivi Clients' && rng.getColumn()==21){
if(rng.getValue()=='OUI' || rng.getValue()=='ANNULÉ'){
var dest = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Clients Finalisé')
dest.insertRowBefore(7)
var plage = sh.getRange('C' + rng.getRow() + ':G' + rng.getRow())
plage.copyTo(dest.getRange("C7"), SpreadsheetApp.CopyPasteType.PASTE_VALUES, false)
var plage = sh.getRange('L' + rng.getRow() + ':N' + rng.getRow())
plage.copyTo(dest.getRange("L7"), SpreadsheetApp.CopyPasteType.PASTE_VALUES, false)
var plage = sh.getRange('P' + rng.getRow())
plage.copyTo(dest.getRange("P7"), SpreadsheetApp.CopyPasteType.PASTE_VALUES, false)
var plage = sh.getRange('U' + rng.getRow() + ':W' + rng.getRow())
plage.copyTo(dest.getRange("U7"), SpreadsheetApp.CopyPasteType.PASTE_VALUES, false)
sh.deleteRow(rng.getRow())
}
}
if (sh.getName()=='Clients Finalisé' && rng.getColumn()==21){
if(rng.getValue()=='NON'){
var dest = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Suivi Clients')
dest.insertRowBefore(7)
var plage = sh.getRange('C' + rng.getRow() + ':G' + rng.getRow())
plage.copyTo(dest.getRange("C7"), SpreadsheetApp.CopyPasteType.PASTE_VALUES, false)
var plage = sh.getRange('L' + rng.getRow() + ':N' + rng.getRow())
plage.copyTo(dest.getRange("L7"), SpreadsheetApp.CopyPasteType.PASTE_VALUES, false)
var plage = sh.getRange('P' + rng.getRow())
plage.copyTo(dest.getRange("P7"), SpreadsheetApp.CopyPasteType.PASTE_VALUES, false)
var plage = sh.getRange('U' + rng.getRow() + ':W' + rng.getRow())
plage.copyTo(dest.getRange("U7"), SpreadsheetApp.CopyPasteType.PASTE_VALUES, false)
sh.deleteRow(rng.getRow())
}
}
// other onEdit here
}
Dans l'onglet "Suivi Clients" je voudrais trier par colonne G et dans l'onglet "Clients Finalisé" je voudrais trier par colonne P.
这是我尝试过的功能之一:
function sort() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Suivi Clients");
var range = sheet.getRange("B6:W");
range.sort(7);
}
对范围进行升序排序
function sort() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Suivi Clients");
var range = sheet.getRange("B6:W" + (sheet.getLastRow()-5));
range.sort({column:7,ascending:true});
}
function onEdit(e) {
var sh = e.range.getSheet();
if (sh.getName() == 'Suivi Clients' && e.range.getColumn() == 21) {
if (e.range.getValue() == 'OUI' || e.range.getValue() == 'ANNULÉ') {
var dest = e.source.getSheetByName('Clients Finalisé');
dest.insertRowBefore(7)
sh.getRange('C' + e.range.rowStart + ':G' + e.range.rowStart).copyTo(dest.getRange("C7"), SpreadsheetApp.CopyPasteType.PASTE_VALUES, false);
sh.getRange('L' + e.range.rowStart + ':N' + e.range.rowStart).copyTo(dest.getRange("L7"), SpreadsheetApp.CopyPasteType.PASTE_VALUES, false);
sh.getRange('P' + e.range.rowStart).copyTo(dest.getRange("P7"), SpreadsheetApp.CopyPasteType.PASTE_VALUES, false);
sh.getRange('U' + e.range.rowStart + ':W' + e.range.rowStart).copyTo(dest.getRange("U7"), SpreadsheetApp.CopyPasteType.PASTE_VALUES, false);
sh.deleteRow(e.range.rowStart);
}
}
if (sh.getName() == 'Clients Finalisé' && e.range.getColumn() == 21 && e.value == 'NON') {
var dest = e.source.getSheetByName('Suivi Clients')
dest.insertRowBefore(7);
sh.getRange('C' + e.range.rowStart + ':G' + e.range.rowStart).copyTo(dest.getRange("C7"), SpreadsheetApp.CopyPasteType.PASTE_VALUES, false);
sh.getRange('L' + e.range.rowStart + ':N' + e.range.rowStart).copyTo(dest.getRange("L7"), SpreadsheetApp.CopyPasteType.PASTE_VALUES, false);
sh.getRange('P' + e.range.rowStart).copyTo(dest.getRange("P7"), SpreadsheetApp.CopyPasteType.PASTE_VALUES, false);
sh.getRange('U' + e.range.rowStart + ':W' + e.range.rowStart).copyTo(dest.getRange("U7"), SpreadsheetApp.CopyPasteType.PASTE_VALUES, false)
sh.deleteRow(e.range.rowStart);
}
}
这种排序对数组中的值进行排序,然后使用 setValues 将值放回电子表格中。如果您在此范围内使用公式,则可能会损坏公式。它还假定范围内的column7中的值都是日期。
function sort() {
var ss = SpreadsheetApp.getActive();
var sheet = ss.getSheetByName("Sheet0");
var vs = sheet.getRange("B6:W" + sheet.getLastRow()).getValues();
vs.sort((a, b) => {
let vA = new Date(a[5]).valueOf();
let vB = new Date(b[5]).valueOf();
return vA - vB;
})
sheet.getRange("B6:W" + sheet.getLastRow()).setValues(vs);
}
尝试
function sortSC() { // Suivi Clients
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Suivi Clients");
var range = sheet.getRange("B6:W" + (sheet.getLastDataRow(3)));
range.sort({column:7,ascending:true});
}
function sortCF() { // Clients Finalisé
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Clients Finalisé");
var range = sheet.getRange("B6:W" + (sheet.getLastDataRow(3)));
range.sort({column:16,ascending:true});
}
Object.prototype.getLastDataRow = function (col) {
var lastRow = this.getLastRow();
if (col == null) { col = 1 }
var range = this.getRange(lastRow, col);
if (range.getValue() !== "") {
return lastRow;
} else {
return range.getNextDataCell(SpreadsheetApp.Direction.UP).getRow();
}
};
仅供参考,完整脚本
function onEdit(e) {
var sh = e.source.getActiveSheet();
var rng = e.source.getActiveRange();
if (sh.getName() == 'Suivi Clients' && rng.getColumn() == 21) {
if (rng.getValue() == 'OUI' || rng.getValue() == 'ANNULÉ') {
var dest = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Clients Finalisé')
dest.insertRowBefore(7)
var plage = sh.getRange('C' + rng.getRow() + ':G' + rng.getRow())
plage.copyTo(dest.getRange("C7"), SpreadsheetApp.CopyPasteType.PASTE_VALUES, false)
var plage = sh.getRange('L' + rng.getRow() + ':N' + rng.getRow())
plage.copyTo(dest.getRange("L7"), SpreadsheetApp.CopyPasteType.PASTE_VALUES, false)
var plage = sh.getRange('P' + rng.getRow())
plage.copyTo(dest.getRange("P7"), SpreadsheetApp.CopyPasteType.PASTE_VALUES, false)
var plage = sh.getRange('U' + rng.getRow() + ':W' + rng.getRow())
plage.copyTo(dest.getRange("U7"), SpreadsheetApp.CopyPasteType.PASTE_VALUES, false)
sh.deleteRow(rng.getRow())
SpreadsheetApp.flush()
sortCF()
}
}
if (sh.getName() == 'Clients Finalisé' && rng.getColumn() == 21) {
if (rng.getValue() == 'NON') {
var dest = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Suivi Clients')
dest.insertRowBefore(7)
var plage = sh.getRange('C' + rng.getRow() + ':G' + rng.getRow())
plage.copyTo(dest.getRange("C7"), SpreadsheetApp.CopyPasteType.PASTE_VALUES, false)
var plage = sh.getRange('L' + rng.getRow() + ':N' + rng.getRow())
plage.copyTo(dest.getRange("L7"), SpreadsheetApp.CopyPasteType.PASTE_VALUES, false)
var plage = sh.getRange('P' + rng.getRow())
plage.copyTo(dest.getRange("P7"), SpreadsheetApp.CopyPasteType.PASTE_VALUES, false)
var plage = sh.getRange('U' + rng.getRow() + ':W' + rng.getRow())
plage.copyTo(dest.getRange("U7"), SpreadsheetApp.CopyPasteType.PASTE_VALUES, false)
sh.deleteRow(rng.getRow())
SpreadsheetApp.flush()
sortSC()
}
}
if (sh.getName() != 'Suivi Clients') return;
var editRange = {
top: 6,
left: 3,
right: 7
};
var thisRow = e.range.getRow();
if (thisRow < editRange.top || thisRow > editRange.bottom) return;
var thisCol = e.range.getColumn();
if (thisCol < editRange.left || thisCol > editRange.right) return;
removeEmpty()
}
function sortSC() { // Suivi Clients
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Suivi Clients");
var range = sheet.getRange("B6:W" + (sheet.getLastDataRow(3)));
range.sort({ column: 7, ascending: true });
}
function sortCF() { // Clients Finalisé
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Clients Finalisé");
var range = sheet.getRange("B6:W" + (sheet.getLastDataRow(3)));
range.sort({ column: 16, ascending: true });
}
Object.prototype.getLastDataRow = function (col) {
var lastRow = this.getLastRow();
if (col == null) { col = 1 }
var range = this.getRange(lastRow, col);
if (range.getValue() !== "") {
return lastRow;
} else {
return range.getNextDataCell(SpreadsheetApp.Direction.UP).getRow();
}
};