Skip to content

Instantly share code, notes, and snippets.

@rubenrivera
Forked from mogsdad/Logger-AddonUtilities.gs
Last active August 1, 2018 15:54
Show Gist options
  • Save rubenrivera/89c16e85c2bb8556f95788d630dc4b7f to your computer and use it in GitHub Desktop.
Save rubenrivera/89c16e85c2bb8556f95788d630dc4b7f to your computer and use it in GitHub Desktop.
Google Apps Script utility functions for persistent logging. See https://mogsdad.wordpress.com/?p=193.
/** * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* AddonUtilities.gs
*
* A collection of general Google-Apps-Script utilities that are useful for
* development of Add-ons.
*
* From: gist.github.com/rubenrivera/89c16e85c2bb8556f95788d630dc4b7f
* Based on: gist.github.com/mogsdad/39db2995989110537868
*
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/**
* Add-ons must run onOpen() without requiring authorization, so instead of enabling
* BetterLog globally, it must be enabled specifically by code that is running outside
* of AuthMode.NONE. This function is called in only those cases.
*
* Uses BetterLog library, MYB7yzedMbnJaMKECt6Sm7FLDhaBgl_dE
*
* From: gist.github.com/mogsdad/39db2995989110537868
*
* @returns {Boolean} Current logging state
*/
var betterLogStarted = false; // Global to ensure we start just once per instance
var useSpreadsheet = false; // Global to use or not a spreadsheet
var logSheetId = undefined; // undefined => log to container sheet.
// Set to spreadsheet ID to log to external sheet.
function startBetterLog_() {
if (!betterLogStarted) {
if(useSpreadsheet){
Logger = BetterLog.useSpreadsheet(logSheetId);
} else {
Logger = BetterLog;
}
betterLogStarted = true;
}
return betterLogStarted;
}
/**
* Support client-side logs from HTML file javascript. First argument is
* expected to be the name of a Logger method.
*
* From: gist.github.com/rubenrivera/89c16e85c2bb8556f95788d630dc4b7f
* Based on: gist.github.com/mogsdad/39db2995989110537868
*
* @param {object} arguments All arguments from [1] are passed on
*/
function clientLog() {
if (!startBetterLog_()) return;
var args = Array.slice(arguments); // Convert arguments to array
var func = args.shift(); // Remove first argument, Logger method
if (!Object.prototype.hasOwnProperty.call(Logger, func)) // Validate Logger method
throw new Error( "Unknown Logger method: " + func );
args[0] = "CLIENT "+args[0]; // Prepend CLIENT tag
Logger[func].apply(null,args); // Pass all arguments to Logger method
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment