This is another part of my wanderings with google scripts, in the last question How to filter a 2D Array using two exact match criteria? I thought I had finished the script, but then it occurred to me to be able to order the array so that it shows me the data in the order I want, the intention is to put several criteria to order and to execute them sequentially.
The script I have is this:
function onEdit(e){
var range = e.range;
var colIndex = range.getColumnIndex(); //Same as getColumn()
var rowIndex = range.getRowIndex(); //Same as getRow()
//Donde utilizar el filtro
var rangeLlista = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Llista").getRange("A6:AX500").getValues();
//Lo que queremos borrar
var rangeBorrar = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Asignacion").getRange("A9:AX500");
//Asignacion de las celdas que tiene que filtrar.
var RangeAsignacion = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Asignacion").getRange("A2:F2");
//Los criterios a filtrar
var Asignacion1 = RangeAsignacion.getCell(1,2).getValue().toString();
var Asignacion2 = RangeAsignacion.getCell(1,5).getValue().toString();
//Columna de los criterios a filtrar, la culumna A = 0
var columnIndex1 = 5;
var columnIndex2 = 6;
//Mira si se modifican las celas que nosotros queremos
if(range.getSheet().getName() === 'Asignacion'){
var watchRange1 = {
top : 2, // start row
bottom : 2, // end row
left : 1, // start col
right : 6, // end col
};
if(colIndex >= watchRange1.left && colIndex <= watchRange1.right && rowIndex >= watchRange1.top && rowIndex <= watchRange1.bottom && e.Value != 0){
//Borrado del contenido anterior
/*var start = 9;
var end = rangeBorrar.getMaxRows();
rangeBorrar.deleteRows(start, end - start);*/
rangeBorrar.clear({contentsOnly: true});
//Se realiza el filtrado
var filteredData = rangeLlista.filter(function (dataRow){
return dataRow[columnIndex1] === Asignacion1 && dataRow[columnIndex2] === Asignacion2;
});
var rows = filteredData.length;
var columns = filteredData[0].length;
SpreadsheetApp.getActiveSheet().getRange(9,1,rows,columns).setValues(filteredData);
};
};
};
This is what I am trying:
var filteredData = rangeLlista.filter(function (dataRow).sort(19,true){ return dataRow[columnIndex1] === Asignacion1 && dataRow[columnIndex2] === Asignacion2; });
I leave a link to a test document:
This is what I have right now:
function onEdit(e){
var range = e.range;
var colIndex = range.getColumnIndex(); //Same as getColumn()
var rowIndex = range.getRowIndex(); //Same as getRow()
//Donde utilizar el filtro
var rangeLlista = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Llista").getRange("A6:AX500");
//Lo que queremos borrar
var rangeBorrar = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Asignacion").getRange("A9:AX500");
//Asignacion de las celdas que tiene que filtrar.
var RangeAsignacion = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Asignacion").getRange("A2:F2");
//Los criterios a filtrar
var Asignacion1 = RangeAsignacion.getCell(1,2).getValue().toString();
var Asignacion2 = RangeAsignacion.getCell(1,5).getValue().toString();
//Columna de los criterios a filtrar, la culumna A = 0
var columnIndex1 = 5;
var columnIndex2 = 6;
//Mira si se modifican las celas que nosotros queremos
if(range.getSheet().getName() === 'Asignacion'){
var watchRange1 = {
top : 2, // start row
bottom : 2, // end row
left : 1, // start col
right : 6, // end col
};
if(colIndex >= watchRange1.left && colIndex <= watchRange1.right && rowIndex >= watchRange1.top && rowIndex <= watchRange1.bottom && e.Value != 0){
rangeBorrar.clear({contentsOnly: true});
//La columna 19 es la T
rangeLlista.sort([{column: 19, ascending: true},{column: 16, ascending: true},{column: 17, ascending: true},{column: 18, ascending: true},{column: 2, ascending: true},{column: 3, ascending: true},{column: 4, ascending: true}]);
var llista = rangeLlista.getValues();
//Se realiza el filtrado
var filteredData = llista.filter(function (dataRow){
return dataRow[columnIndex1] === Asignacion1 && dataRow[columnIndex2] === Asignacion2;
});
var rows = filteredData.length;
var columns = filteredData[0].length;
SpreadsheetApp.getActiveSheet().getRange(9,1,rows,columns).setValues(filteredData);
};
};
};
The first 2 columns are fine for me, but the rest are not.
Basically you have two alternatives
Google Apps Script's own method
Class Range includes the sort(sortSpecObj) method . Below is the example of the official documentation with the translated comments
Use JavaScript methods
Related questions
I solved the problem by modifying these lines:
I had a bug saying what columns were