javascript - Average of Numbers in Column If Condition In Different Column Satisfied -


i have data range of first column dates , second column numerical values. there rows second column value blank , not want them counted.

i trying find average of second column values if satisfy criteria of being within 3 months ago today (blank values should not counted).

but stuck cannot total correct. , not know how proceed further average.

the code belows seem give me appended strings instead of summing numbers mathematically.

can please?

thanks in advance.

 function average() {    // open spreadsheet    var spreadsheet = spreadsheetapp.openbyid("spreadsheetid");    // set named sheet active    var sheet = spreadsheetapp.setactivesheet(spreadsheet.getsheetbyname("sheetname"));     // figure out last row    var lastrow = spreadsheet.getsheetbyname("form responses 1").getlastrow();    // rows indexed starting @ 1, , first row headers, start row 2    var startrow = 2;    // data range    var responsesvalues = spreadsheet.getsheetbyname("form responses 1").getrange("a1:q" + lastrow).getvalues();     // define dates    var timetoday = new date().gettime();    var datetoday = new date().getdate();    var date = new date();    var threemonthsago = new date(date.getfullyear(), date.getmonth() - 3, 0);     // grab column 1 (date of entry column) (second variable in getrange function below)    var datarange = sheet.getrange(2,1,lastrow-startrow+1,1 );    var numrows = datarange.getnumrows();    var dateofentryvalues = datarange.getvalues();     // grab column 2 (values averaged)    range = sheet.getrange(2, 2, lastrow-startrow+1, 1);    var values = range.getvalues();     var warning_count = 0;    var sumvalues = 0;     // loop on values    (var = 0; <= numrows - 1; i++) {      var dateofentry = dateofentryvalues[i][0];      if(dateofentry > threemonthsago && dateofentry !== "") {    // if it's within 3 months ago, add values.    sumvalues += values[i][0];    warning_count++;      }    } 

there lot more simple version. put destination cell formula

=arrayformula(average(if(datedif(c1:c;today();"d")<=90;e1:e)))

replacing c1:c column dates , e1:e column numbers.


Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -