有沒有辦法在Google應用程序腳本中使用Logger.log記錄對象的內容?將Javascript對象記錄爲字符串Google App腳本
如果我有Logger.log(data)
,日誌是'DataTableBuilder'或'對象'或類似的東西,並且令人難以置信地無用。
我想看看是否有可能這些對象的JSON字符串...
有沒有辦法在Google應用程序腳本中使用Logger.log記錄對象的內容?將Javascript對象記錄爲字符串Google App腳本
如果我有Logger.log(data)
,日誌是'DataTableBuilder'或'對象'或類似的東西,並且令人難以置信地無用。
我想看看是否有可能這些對象的JSON字符串...
我是從another SO answer修改的功能:
var Log = {
// Thanks to Amos Batto - https://stackoverflow.com/questions/603987/what-is-the-javascript-equivalent-of-var-dump-or-print-r-in-php
/*
dump() displays the contents of a variable like var_dump() does in PHP. dump() is
better than typeof, because it can distinguish between array, null and object.
Parameters:
v: The variable
howDisplay: "none", "body", "alert" (default)
recursionLevel: Number of times the function has recursed when entering nested
objects or arrays. Each level of recursion adds extra space to the
output to indicate level. Set to 0 by default.
Return Value:
A string of the variable's contents
Limitations:
Can't pass an undefined variable to dump().
dump() can't distinguish between int and float.
dump() can't tell the original variable type of a member variable of an object.
These limitations can't be fixed because these are *features* of JS. However, dump()
*/
dump: function(functionName, v, recursionLevel) {
recursionLevel = (typeof recursionLevel !== 'number') ? 0 : recursionLevel;
var vType = typeof v;
var out = vType;
switch (vType) {
case "number":
/* there is absolutely no way in JS to distinguish 2 from 2.0
so 'number' is the best that you can do. The following doesn't work:
var er = /^[0-9]+$/;
if (!isNaN(v) && v % 1 === 0 && er.test(3.0))
out = 'int';*/
case "boolean":
out += ": " + v;
break;
case "string":
out += "(" + v.length + '): "' + v + '"';
break;
case "object":
//check if null
if (v === null) {
out = "null";
}
//If using jQuery: if ($.isArray(v))
//If using IE: if (isArray(v))
//this should work for all browsers according to the ECMAScript standard:
else if (Object.prototype.toString.call(v) === '[object Array]') {
out = 'array(' + v.length + '): {\n';
for (var i = 0; i < v.length; i++) {
out += repeatString(' ', recursionLevel) + " [" + i + "]: " +
Log.dump(functionName, v[i], recursionLevel + 1) + "\n";
}
out += repeatString(' ', recursionLevel) + "}";
}
else { //if object
sContents = "{\n";
cnt = 0;
for (var member in v) {
//No way to know the original data type of member, since JS
//always converts it to a string and no other way to parse objects.
sContents += repeatString(' ', recursionLevel) + " " + member +
": " + Log.dump(functionName, v[member], recursionLevel + 1) + "\n";
cnt++;
}
sContents += repeatString(' ', recursionLevel) + "}";
out += "(" + cnt + "): " + sContents;
}
break;
}
Logger.log(functionName + ' - ' + out);
return;
// Private Functions
// -----------------
/* repeatString() returns a string which has been repeated a set number of times */
function repeatString(str, num) {
out = '';
for (var i = 0; i < num; i++) {
out += str;
}
return out;
} // Log.dump.repeatString()
}, // Log.dump()
}
與JSON.stringify(嘗試)在這裏你可以找到的文檔:https://developers.google.com/apps-script/guides/services/external#working_with_json
非常新的谷歌腳本,但它看起來像你缺少一個功能。當我運行這個時,我得到:「TypeError:無法找到對象[對象對象]中的函數更精細(第80行)」 –
這是我的個人日誌記錄功能,我忘了拿出來。我用Logger.log替換了它 –