javascript - Multiple array inputs for Google Sheets custom function that divides one array by another -


building running log in google sheets , need write function returns pace (minutes per mile) in separate column adjacent 2 columns distance (miles) , duration (minute) entries.

duration data stored in column b.
distance data stored in column c.
pace data returned column d.

i can return array column d doubles duration value each row in column b:

function calculatepace(distance, duration){       if(distance.map){   return distance.map(calculatepace);   }   else{     var pace = duration*2;     return pace;   } } 

what need divide duration array distance array, such that

else{   var pace = duration/distance;   return pace; } 

but not know how recurse calculatepace function each row in distance.map , duration.map both passed each call.

to further clarify, =calculatepace(b1:b10, c1:c10) stored in cell d1.

you want google sheets function divides 1 array another. built-in arrayformula already:

=arrayformula(b1:b10/c1:c10) 

but if custom function required (perhaps it's part of larger computation), this:

function calculatepace(distance, duration){       if (distance.map && duration.map && distance.length == duration.length) {     return distance.map(function(a, i) {       return a[0]/duration[i][0];     });   }   else if (!distance.map && !duration.map) {     return distance/duration;   }   else {      throw new error('the arguments must have same number of rows');   } } 

the essential part division of arrays,

distance.map(function(a, i) {   return a[0]/duration[i][0]; }); 

here a row first range, a[0] accesses first element. matching element of duration array duration[i][0]. since ranges presumed columns, first column considered.

the if-statements check inputs either arrays of same length or scalars; otherwise error thrown.


Comments

Popular posts from this blog

routing - AngularJS State management ->load multiple states in one page -

python - GRASS parser() error -

Swift game error message -