Skip to content

Instantly share code, notes, and snippets.

@dekarrin
Created December 28, 2013 16:25
Show Gist options
  • Save dekarrin/8161241 to your computer and use it in GitHub Desktop.
Save dekarrin/8161241 to your computer and use it in GitHub Desktop.
For writing to a debug document in Google Apps Script
DEBUG_DOC_ID = "Replace this with the ID of document to write debug output to";
function print_r (array) {
var analyze = function(array) {
return format(array, 0);
}
var format = function(obj, depth) {
var str = "";
var padder = "";
var PAD = " ";
for (var i = 0; i < depth; i++) {
padder += PAD;
}
if (typeof obj === 'object') {
str += obj.constructor.name;
if (obj.constructor.name === "Array" || obj.constructor.name === "Object") {
var q = (obj.constructor.name === "Array") ? "" : "'";
str += "\n" + padder + "(\n";
for (var i in obj) {
str += padder + PAD + "[" + q + i + q + "] => " + format(obj[i], depth + 1) + "\n";
}
str += padder + ")\n";
}
} else if (typeof obj === 'string') {
str += "'" + obj.toString() + "'";
} else {
str += obj.toString();
}
return str;
}
debugln(analyze(array));
}
function debugln(msg) {
var doc = DocumentApp.openById(DEBUG_DOC_ID);
doc.getBody().setText(doc.getBody().getText() + msg + "\n");
}
function cleardebug(msg) {
var doc = DocumentApp.openById(DEBUG_DOC_ID);
doc.getBody().setText("");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment