En el siguiente código de ejemplo, ¿cómo puedo filtrar solo aquellas filas de "Vender" en el elemento "Estado" y más del 30 % en el elemento "Beneficio" de la variable "datos", usando la variable "criterios"? ¿Alguien puede ayudarme a terminar el código? Solía usar las funciones de filtro de Hojas de cálculo de Google, SpreadsheetApp.nuevos criterios de filtro().whenNumber..., pero me tomó mucho tiempo (5 minutos) actualizar mis Hojas de cálculo de Google, ya que mi aplicación real tiene 3000 filas y 140 columnas para filtrar y necesitaba tantas llamadas. Más bien, aprendo cómo hacer operaciones de filtrado dentro del nivel de JavaScript, esperando que sea mucho más rápido. Luego puedo enviar los datos filtrados a Hojas de cálculo de Google. ¡Gracias por cualquier ayuda!
function test() { var data = [['Product', 'Status', 'Price', 'Profit'], ['Apple', 'Hold', 10, '10%'], ['Mango', 'Sell', 20, '20%'], ['Orange', 'Buy', 30, '30%'], ['Juice', 'Sell', 40, '40%'], ['Coffee', 'Sell', 50, '50%']]; data.splice(0, 1); // Remove header var criteria = ['', 'Sell', '', '>30%']; var filteredData = data; for (var i = 0; i < criteria.length; ++i) { if (criteria[i]) { filteredData = filteredData.filter(???); console.log(filteredData); } } }
Puede iterar todos los criterios junto con sus valores de columna y verificar el valor o calcular el valor porcentual.
const criteria = ['', 'Sell', '', '>30%'], data = [['Product', 'Status', 'Price', 'Profit'], ['Apple', 'Hold', 10, '10%'], ['Mango', 'Sell', 20, '20%'], ['Orange', 'Buy', 30, '30%'], ['Juice', 'Sell', 40, '40%'], ['Coffee', 'Sell', 50, '50%']], result = data .slice(1) .filter(a => criteria.every((c, i) => { if (!c || c === a[i]) return true; if (c.slice(-1) === '%') return eval(a[i].slice(0, -1) + c.slice(0, -1)); })); console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
function test() { var data = [['Product', 'Status', 'Price', 'Profit'], ['Apple', 'Hold', 10, '10%'], ['Mango', 'Sell', 20, '20%'], ['Orange', 'Buy', 30, '30%'], ['Juice', 'Sell', 40, '40%'], ['Coffee', 'Sell', 50, '50%']]; data.shift(); let fdata = data.filter(r => r[1] == 'Sell' && parseInt(r[3]) > 30); Logger.log(fdata.join('\n')); } 1:45:07 PM Notice Execution started 1:45:08 PM Info Juice,Sell,40,40% Coffee,Sell,50,50% 1:45:08 PM Notice Execution completed
.filter
, .map
, .reduce
, .forEach
y otras funciones similares,
tiene algunas opciones con respecto a los parámetros que puede pasar, puede leer más al respecto aquí .
Para este caso de uso, en realidad no necesita ningún criteria
,
simplemente puedes hacer:
function test() { var data = [['Product', 'Status', 'Price', 'Profit'], ['Apple', 'Hold', 10, '10%'], ['Mango', 'Sell', 20, '20%'], ['Orange', 'Buy', 30, '30%'], ['Juice', 'Sell', 40, '40%'], ['Coffee', 'Sell', 50, '50%']]; data.splice(0, 1); // Remove header var filteredData = data; filteredData = filteredData.filter(currentData => { if (currentData[1] === 'Sell' && parseInt(currentData[3]) > 30) { return currentData; } }); }
Una vez hecho esto, filteredData
se llena con las matrices 'Juice' y 'Coffee' en este caso, y luego puede hacer lo que necesite con él.