I don't like typing
Logger.log()
constantly. I'd rather just type ll()
And I don't want to type:
JSON.stringify() every time I want to log an object
Here is the code I use to do that.
/*{version:"1/22/20"}
1/22/20 - var f
7 8 19 proc essValForLog
*/
function ll(a,b) {
var f;
//Logger.log('ll ran: ' + a + " " + b)
if (ll.arguments.length === 2) {//two arguments were passed in
b = processValForLog(b);
f = new Function('a','b','Logger.log(a + ":" + b)');//Create new fnk
f(a,b);//call fnk
} else {
f = new Function('a','Logger.log(a)');
f(a);
}
}
function processValForLog(val) {
try{
var typeOfThisVal;
typeOfThisVal = typeof val;
if (val) {
if (typeOfThisVal === 'object') {//The variable val can be an object and still be null
if (Array.isArray(val)) {//The very first test should be for an array because that is easy to check for
//console.log('it is an array')
if (val.toString().indexOf("[object Object]") !== -1) {
val = JSON.stringify(val);
} else {
val = val.toString();
}
//Logger.log('val: ' + val)
} else {//It is an object but not an array
//Logger.log('its NOT an array')
try{
val = JSON.stringify(val);//Test for whether it is a date
}catch(e) {//If this is an invalid date then JSON stringify will fail
//console.log('Error stringifying')
}
}
//Logger.log('typeof val: ' + typeof val)
if (typeof val !== 'string') {
try{
val = val.toString();
}catch(e) {
val = e.message;
}
}
if (val.indexOf("{") !== 0 && typeof val !== 'string') {
val = val.toString();
}
//continue;
}
}
if (typeOfThisVal === 'number') {
val = '"' + val.toString();
}
if (val === undefined) {//Avoid having all three cells undefined
val = 'UNDEFINEDDD';
} else if (val === null) {
val = 'NULLLL';
} else if (val === false) {
val = 'FALZZE';
} else if (val === true) {
val = 'TREWWW';
}
return val;
}catch(e) {
var f = new Function('e','Logger.log(e.message + ":\n\n" + e.stack)');
f(e);
}
}