Skip to content

Instantly share code, notes, and snippets.

@oshliaer
Last active April 18, 2019 15:49
Show Gist options
  • Save oshliaer/19930f93b0e015650af250a177e98745 to your computer and use it in GitHub Desktop.
Save oshliaer/19930f93b0e015650af250a177e98745 to your computer and use it in GitHub Desktop.
SUMGSM
{"scriptId":"1tAEuyUVI5o-8Gt44y9LDixLw6XEeI6uvLt3nfOpqTJ8GyaTSENrTz0hz"}
**/**
!appsscript.json
!Angle.js
!Code.js
!Tap.test.js
node_modules
package-lock.json
.eslintrc.json
.prettierrc
(function(self) {
function isNumeric(n) {
return !isNaN(parseFloat(n)) && isFinite(n);
}
function Angle(milliseconds) {
milliseconds = milliseconds || 0;
this.dec = milliseconds;
return this;
}
Angle.prototype.parse = function(val) {
if (val === '') {
this.dec = 0;
return this;
}
if (isNumeric(val)) {
val = (
'000000' +
val +
(/\./.test(val.toString()) ? '0000' : '.0000')
).replace(/.*?(\d{3})(\d{2})(\d{2})\.(\d{3}).*?$/, "$1,$2'$3.$4");
}
var m = val.match(/(\d+),(\d+)'(\d+)\.(\d+)/);
var sign = /^-/.test(val) ? -1 : 1;
this.dec =
sign *
(Number(m[1]) * 60 * 60 * 1000 +
Number(m[2]) * 60 * 1000 +
Number(m[3]) * 1000 +
Number(m[4]));
return this;
};
Angle.prototype.get = function() {
return this.dec;
};
Angle.prototype.add = function(angle) {
this.dec += angle.get();
return this;
};
Angle.prototype.toString = function() {
var _ = 0 + this.dec;
var milliseconds = _ % 1000;
_ = (_ - milliseconds) / 1000;
var seconds = _ % 60;
_ = (_ - seconds) / 60;
var minutes = _ % 60;
_ = (_ - minutes) / 60;
var degrees = _ % 360;
return Utilities.formatString(
"%s,%s'%s.%s''",
degrees,
minutes,
seconds,
milliseconds
);
};
Angle.prototype.toDigit = function() {
var _ = 0 + this.dec;
var milliseconds = _ % 1000;
_ = (_ - milliseconds) / 1000;
var seconds = _ % 60;
_ = (_ - seconds) / 60;
var minutes = _ % 60;
_ = (_ - minutes) / 60;
var degrees = _ % 360;
return degrees * 10000 + minutes * 100 + seconds + milliseconds / 1000;
};
self.Angle = Angle;
})(this);
{
"timeZone": "Africa/Nairobi",
"dependencies": {},
"exceptionLogging": "STACKDRIVER",
"oauthScopes": [
"https://www.googleapis.com/auth/script.external_request"
]
}
/* exported SUMGMS */
/* globals Angle */
/**
* Sums GMS-notation values
* @param {object[][]} range A single column range
* @returns {number} GMS-notation number
* @customfunction
*/
function SUMGMS(range) {
if (!Array.isArray(range)) range = [[range]];
return range
.map(function(row) {
return new Angle().parse(row[0]);
})
.reduce(function(p, angle) {
p.add(angle);
return p;
}, new Angle())
.toDigit();
}
{
"name": "19930f93b0e015650af250a177e98745",
"version": "1.0.0",
"description": "",
"main": "Code.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git+https://gist.github.com/19930f93b0e015650af250a177e98745.git"
},
"keywords": [],
"author": "",
"license": "ISC",
"bugs": {
"url": "https://gist.github.com/19930f93b0e015650af250a177e98745"
},
"homepage": "https://gist.github.com/19930f93b0e015650af250a177e98745",
"devDependencies": {
"@google/clasp": "^2.1.0",
"@types/google-apps-script": "0.0.44",
"eslint": "^5.16.0",
"eslint-config-google": "^0.12.0",
"eslint-config-prettier": "^4.1.0",
"eslint-plugin-googleappsscript": "^1.0.1",
"eslint-plugin-prettier": "^3.0.1",
"prettier": "^1.16.4"
}
}
/* exported runTests */
/* globals GasTap, Angle */
var jksadhfkajsdhfadjfs = this;
function runTests() {
if (typeof GasTap === 'undefined') {
var cs = CacheService.getScriptCache().get('gast');
if (!cs) {
cs = jksadhfkajsdhfadjfs['Url' + 'FetchApp']
.fetch(
'https://raw.githubusercontent.com/zixia/gast/master/src/gas-tap-lib.js'
)
.getContentText();
CacheService.getScriptCache().put('gast', cs, 21600);
}
eval(cs);
}
var test = new GasTap();
test('get test', function(t) {
var milliseconds = 11553001;
var a = new Angle(milliseconds).get();
t.equal(a, milliseconds, 'Correct milliseconds');
});
test('toDigit test', function(t) {
var milliseconds = 11553010;
var a = new Angle(milliseconds).toDigit();
t.equal(a, 31233.01, 'Correct digit');
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment