google spreadsheet - Dividing a value between non-equal rows in order to balance them -


i have spreadsheet that's structured like:

section        total       incoming     new total ak             56,445      2,655        59,100 al             58,304      796          59,100 b              55,524      3,576        59,100 c              54,272      4,828        59,100 d              53,956      5,144        59,100 s              59,161      0            59,161 - generated pts  16,999 

i'm trying automate "incoming" column. goal of sheet balance totals closely possible distributing generated pts between each row until no more points remain, ensuring lowest totals increased first higher values aren't increased while lower values exist.

is possible in spreadsheet? suggestions on how done?

i made attempt @ custom function. 2 parameters passed: range corresponding total column, , cell containing generated pts. incoming array returned.

function distribute(range, value) {   var indexedrange = range.map(function (e, index) {return [e[0], e[0], index];});   indexedrange.sort(function (a, b) {return a[0] < b[0] ? -1 : a[0] > b[0] ? 1 : 0;});   var count = 0, = 0, limit = indexedrange.length - 1;   while (count < value) {     indexedrange[i][0] ++;     = == limit || indexedrange[i][0] <= indexedrange[i + 1][0] ? 0 : + 1;     count++;   }   indexedrange.sort(function (a, b) {return a[2] < b[2] ? -1 : a[2] > b[2] ? 1 : 0;});   return indexedrange.map(function (e) {return [e[0] - e[1]];});     } 

it matches expected results, might want try out on different data check logic ok.


Comments

Popular posts from this blog

python - pip install -U PySide error -

arrays - C++ error: a brace-enclosed initializer is not allowed here before ‘{’ token -

cytoscape.js - How to add nodes to Dagre layout with Cytoscape -