我有这个演示表 https://docs.google.com/spreadsheets/d/1pGkTrudeDfv7Xkm7ZGM0fmQzSCqU05hFVhuczJ4cWCQ/edit?usp=sharing
使用此代码对我的数据进行排序
function SortColumns(){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Ventas");
var range = sheet.getRange("A4:S");
var SORT_ORDER = [
{column: 19, ascending: false},
{column: 18, ascending: false},
{column: 13, ascending: false},
{column: 7 , ascending: false},
{column: 2 , ascending: true},
{column: 1 , ascending: true},
{column: 3 , ascending: true}
]
range.sort(SORT_ORDER);
}
可以正常工作
我需要的是, 如果M列包含“ Entregado”以这种方式进行排序, 如果包含以不同顺序排列的“眼镜蛇”排序, 如果包含“ Apartado”以不同顺序排序, 如果包含以不同顺序排序的“ Preventa”, 以及是否包含其他值以其他方式对它们进行排序
这可能吗?
谢谢!
编辑:我正在尝试这段代码,但无法正常工作
function ColumnContainString(column,string) {
var column=column || 13;//used for initial testing
var string=string || 'test';//used for initial testing
var ss=SpreadsheetApp.getActive();
var sheet=ss.getSheetByName("Ventas");
var cA=sheet.getRange(4,column,sheet.getLastRow(),1).getValues();
for(var i=0;i<cA.length;i++) {
if(cA[i][0].toString().indexOf(string)>-1) {//you may wish to change this line to some regular expression to find only words
Logger.log(i+1);
return(i+1);
}
}
Logger.log('-1');
return -1;
}
function SortColumns(){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Ventas");
var range = sheet.getRange("A4:S");
if(ColumnContainString(13,'Entregado')==-1) {
var SORT_ORDER = [
{column: 19, ascending: false}, // M
{column: 18, ascending: true}, // H
{column: 1 , ascending: true}, // Fecha
{column: 2 , ascending: true}, // Cliente
{column: 3 , ascending: true} // Sabor
]
range.sort(SORT_ORDER);
}
if(ColumnContainString(13,'Cobrar')==-1) {
var SORT_ORDER = [
{column: 18, ascending: true}, // H
{column: 13, ascending: true}, // Estatus
{column: 7 , ascending: false}, // Vendio
{column: 2 , ascending: true}, // Cliente
{column: 3 , ascending: true}, // Sabor
{column: 1 , ascending: true} // Fecha
]
range.sort(SORT_ORDER);
}
if(ColumnContainString(13,'Pendiente')==-1) {
var SORT_ORDER = [
{column: 18, ascending: true}, // H
{column: 13, ascending: true}, // Estatus
{column: 7 , ascending: false}, // Vendio
{column: 2 , ascending: true}, // Cliente
{column: 3 , ascending: true}, // Sabor
{column: 1 , ascending: true} // Fecha
]
range.sort(SORT_ORDER);
}
if(ColumnContainString(13,'Preventa')==-1) {
var SORT_ORDER = [
{column: 18, ascending: true}, // H
{column: 13, ascending: true}, // Estatus
{column: 7 , ascending: false}, // Vendio
{column: 2 , ascending: true}, // Cliente
{column: 3 , ascending: true}, // Sabor
{column: 1 , ascending: true} // Fecha
]
range.sort(SORT_ORDER);
}
if(ColumnContainString(13,'Apartado')==-1) {
var SORT_ORDER = [
{column: 18, ascending: true}, // H
{column: 13, ascending: true}, // Estatus
{column: 2 , ascending: true}, // Cliente
{column: 3 , ascending: true}, // Sabor
{column: 1 , ascending: true} // Fecha
]
range.sort(SORT_ORDER);
}
}
答案 0 :(得分:1)
尝试一下:
function ColumnContainString(column,string) {
var column=column || 13;//used for initial testing
var string=string || 'test';//used for initial testing
var ss=SpreadsheetApp.getActive();
var sheet=ss.getSheetByName("Ventas");
var cA=sheet.getRange(4,column,sheet.getLastRow(),1).getValues();
for(var i=0;i<cA.length;i++) {
if(cA[i][0].toString().indexOf(string)>-1) {//you may wish to change this line to some regular expression to find only words
Logger.log(i+1);
return(i+1);
}
}
Logger.log('-1');
return -1;
}
主函数将具有以下表达式:
if(ColumnContainString(13,'Entregado')==-1) {
//Then put your sort functions in here
}
因此您可以通过以下方式重写当前函数:
function SortColumns(){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Ventas");
var range = sheet.getRange("A4:S");
if(ColumnContainString(13,'Entregado')==-1) {
var SORT_ORDER = [
{column: 19, ascending: false},
{column: 18, ascending: false},
{column: 13, ascending: false},
{column: 7 , ascending: false},
{column: 2 , ascending: true},
{column: 1 , ascending: true},
{column: 3 , ascending: true}];
range.sort(SORT_ORDER);
}
if(ColumnContainString(13,'Apartado')==-1) {
var SORT_ORDER = [
{column: 19, ascending: false},
{column: 18, ascending: false},
{column: 13, ascending: false},
{column: 7 , ascending: false},
{column: 2 , ascending: true},
{column: 1 , ascending: true},
{column: 3 , ascending: true}];//change sort order
range.sort(SORT_ORDER);
}
}