Created
April 8, 2019 17:11
-
-
Save lukeramsden/3bf43449117fe5f69ffc0f272140f599 to your computer and use it in GitHub Desktop.
EMS Shipping Calculator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Calculates shipping cost to ship X grams with EMS. | |
* @param {Range} range Range or cell containing the shipping weights. | |
* @param {Number} firstCost Cost of first interval. (Default 20.91) | |
* @param {Number} additionalCost Cost of additional intervals. (Default 6.31) | |
* @param {Number} interval Interval size. (Default 500) | |
* @return Shipping cost in USD. | |
* @customfunction | |
*/ | |
function EMS_SHIPPING(range, firstCost, additionalCost, interval) { | |
firstCost = Number(firstCost); | |
if(isNaN(firstCost)) { | |
firstCost = 20.91; | |
} | |
additionalCost = Number(additionalCost); | |
if(isNaN(additionalCost)) { | |
additionalCost = 6.31; | |
} | |
interval = Number(interval); | |
if(isNaN(interval)) { | |
interval = 500; | |
} | |
var totalWeight = 0; | |
if(typeof range == 'number') { | |
totalWeight = range; | |
} else { | |
for(var i = 0; i < range.length; i++) { | |
totalWeight += parseFloat(range[i]); | |
} | |
} | |
var totalCost = 0; | |
var doneFirst = false; | |
while(totalWeight > 0) { | |
if(!doneFirst) | |
totalCost += firstCost; | |
else | |
totalCost += additionalCost; | |
totalWeight -= interval; | |
doneFirst = true; | |
} | |
return totalCost; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment