Para mi rastreador de gastos, una de mis funciones personalizadas en Hojas de cálculo de Google permite a los usuarios ver cuánto han gastado en una marca de ropa. Aquí hay una imagen de la hoja de Google:
La siguiente función se utiliza para lograr esto:
function perCentBrand(brand){ var sh = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet(); var values = sh.getRange(2,1,sh.getLastRow()-1,sh.getLastColumn()).getValues(); var total = 0; var sum = 0; values.forEach(function(row){ total+=row[1]; if (row[2].toLowerCase() == brand.toLowerCase()){sum+=row[1]} }) var val = "You spent a total of " + (sum*-1) + " on " + brand + " out of " + (total*-1); var ui = SpreadsheetApp.getUi(); ui.alert(val) }Sin embargo, esta función también tiene en cuenta los números positivos, que se supone que son cifras de ingresos, no cifras de gastos. Por lo tanto, al ejecutar la función, se muestra este mensaje:
Como se tienen en cuenta los números positivos, el total dice -270 cuando debería ser 90 . ¿Cómo puedo hacer que la función ignore los números positivos?
Editar:
Alerta después de la respuesta sugerida: 
Función después de la respuesta sugerida:
function perCentBrand(brand){ var sh = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet(); var values = sh.getRange(2,1,sh.getLastRow()-1,sh.getLastColumn()).getValues(); var total = 0; var sum = 0; values.forEach(function(row) { if(row[1] < 0 ) { total+=row[1]; } else if (row[2].toLowerCase() == brand.toLowerCase()){sum+=row[1]} }) var val = "You spent a total of " + (sum*-1) + " on " + brand + " out of " + (total*-1); var ui = SpreadsheetApp.getUi(); ui.alert(val) }La parte fila[2] debe ser separada si está dentro de la parte (fila[1] < 0). No es un otro si.
values.forEach(function(row) { if (row[1] < 0) { total+=row[1]; if (row[2].toLowerCase() == brand.toLowerCase()) { sum+=row[1]; } } })Dale una oportunidad a esto:
function perCentBrand(brand) { const spreadSheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet(); const values = spreadSheet .getRange(2, 1, spreadSheet.getLastRow() - 1, spreadSheet.getLastColumn()) .getValues(); let total = 0; let sum = 0; values.forEach((row) => { const brandNamesMatch = row[2].toLowerCase() == brand.toLowerCase(); const valueIsAnExpense = row[1] <= 0; if (valueIsAnExpense) { const expenseValue = Math.abs(row[1]); total += expenseValue; if (brandNamesMatch) { sum += expenseValue; } } }); const alertText = `You spent $${sum} out of $${total} on ${brand}`; const modal = SpreadsheetApp.getUi(); modal.alert(alertText); }