Last active
September 5, 2021 15:51
-
-
Save mogsdad/39db2995989110537868 to your computer and use it in GitHub Desktop.
Google Apps Script utility functions for persistent logging. See https://mogsdad.wordpress.com/?p=193.
This file contains 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
/** * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * | |
* AddonUtilities.gs | |
* | |
* A collection of general Google-Apps-Script utilities that are useful for | |
* development of Add-ons. | |
* | |
* From: 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 logSheetId = undefined; // undefined => log to container sheet. | |
// Set to spreadsheet ID to log to external sheet. | |
function startBetterLog_() { | |
if (!betterLogStarted) { | |
Logger = BetterLog.useSpreadsheet(logSheetId); | |
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/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 (!Logger.hasOwnProperty(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
Logger.hasOwnProperty(func) (Line 45) is throwing `TypeError: Cannot find function hasOwnProperty in object [object Object]. Replacing it by Object.prototype.hasOwnProperty.call(Logger, func) fix the problem.