我编写了一个简单的迭代器来遍历JSON对象并将该对象解析为新形式。这在几乎任何JS环境中都能正常工作。(例如在控制台中(但是,下面的函数在AppScript中执行时返回一个空数组。
返回:[[],[],[],[],[]]
这个问题似乎是AppScript特有的。在我通过谷歌开发者小组提交bug之前,我想了解这是否可能是应用脚本特定的和预期的行为。
function parseBQData(/*tableData, request*/) {
var tableData = [["20220301","(none)","(direct)","3","1","1"],["20220301","organic","google","3","1","1"],["20220302","(none)","(direct)","4","2","2"],["20220302","organic","bing","1","1","1"],["20220303","(none)","(direct)","1","1","1"]]
try {
// store the array of dimensions and metrics in a variable called 'fields'
var fields = ["date", "medium", "source", "pageviews", "sessions", "users"]
// create a new empty array to store the parsed data
var newtableData = new Array();
// loop through each row in the tableData
for (var i = 0; i < tableData.length; i++) {
Logger.log(tableData[i]) // This returns: [20220301, (none), (direct), 3, 1, 1], [2022]
// create a new empty array to store the current row
var wrapper = new Array();
// loop through each column in the row
for (var j = 0; j < fields.length; j++) {
wrapper[fields[j]] = tableData[i][j]; /// <-is this not working?
Logger.log("Test Log:")
Logger.log("This is the updated field stored in the wrapper:"+wrapper[fields[j]]) // Returns : "20220301"
Logger.log("Lets check the wrapper if has the date/first value : " + wrapper.date ) // Returns "20220301"
// the wrapper does not dissapear but appears as empty when accessed as in root, the assignment abovew worked and the key value pair is accessible
Logger.log("Wrapper : " + JSON.stringify(wrapper)) // This returns always "Wrapper : []" Stringify is solely used to see that atleast something is returned and the wrapper is accesible
Logger.log("This is the current cell: "+fields[j]+" : "+tableData[i][j]) // This returns : "This is the current cell: date : 20220301" ... This is the current cell: medium : (none)
// So in conclusion All values and Arrays are accessible
// store the column data in the current row array, using the column header as the key
}
// add the current row to the new array of parsed data
newtableData.push(wrapper);
}
// return the new array of parsed data
Logger.log(newtableData) //This returns: "[[], [], [], [], []]""
return newtableData;
// if there is an error parsing the data, print the error to the log
} catch (e) {
Logger.log("Error parsing data")
Logger.log(e)
}
}
#编辑:添加一些日志和注释
在App Script中没有尝试,但在浏览器JS控制台中工作良好:
var TableData = [["20220301","(none)","(direct)","3","1","1"],["20220301","organic","google","3","1","1"],["20220302","(none)","(direct)","4","2","2"],["20220302","organic","bing","1","1","1"],["20220303","(none)","(direct)","1","1","1"]]
function parseBQData(TableData, request) {
try {
var fields = ["date", "medium", "source", "pageviews", "sessions", "users"];
var newTableData = new Array();
for (var i = 0; i < TableData.length; i++) {
var wrapper = new Array();
for (var j = 0; j < fields.length; j++) {
wrapper[fields[j]] = TableData[i][j];
console.log("Wrapper : " + wrapper)
}
newTableData.push(wrapper);
}
return newTableData;
} catch (e) {
console.log("Error parsing data")
console.log(e)
}
}
parseBQData(TableData, 0);
日志:
Wrapper :
(5) [Array(0), Array(0), Array(0), Array(0), Array(0)]
0: [date: '20220301', medium: '(none)', source: '(direct)', pageviews: '3', sessions: '1', …]
1: [date: '20220301', medium: 'organic', source: 'google', pageviews: '3', sessions: '1', …]
2: [date: '20220302', medium: '(none)', source: '(direct)', pageviews: '4', sessions: '2', …]
3: [date: '20220302', medium: 'organic', source: 'bing', pageviews: '1', sessions: '1', …]
4: [date: '20220303', medium: '(none)', source: '(direct)', pageviews: '1', sessions: '1', …]
length: 5
所以我最好的猜测,检查Logger打印参数类型,也许Logger.log("Wrapper : ", wrapper);
?
在App Script中,TableData
是在全局作用域中定义的。而传递给parseBQData
的参数TableData
在函数范围内。如果我尝试在全局范围内运行脚本function parseBQData(TableData, request)
,包括TableData
,我会得到TypeError: Cannot read property 'length' of undefined
,因为TableData
是未定义的。你可以解决这个问题通过简单:
function parseBQData(request) {
这不是一个bug。
我还发现了这篇JavaScript函数的参数和作用域
相同的输出,但更简单
function parseBQData() {
const iA = [["20220301", "(none)", "(direct)", "3", "1", "1"], ["20220301", "organic", "google", "3", "1", "1"], ["20220302", "(none)", "(direct)", "4", "2", "2"], ["20220302", "organic", "bing", "1", "1", "1"], ["20220303", "(none)", "(direct)", "1", "1", "1"]]
const fields = ["date", "medium", "source", "pageviews", "sessions", "users"];
iA.unshift(fields)
Logger.log(JSON.stringify(iA));
}
Execution log
10:25:24 AM Notice Execution started
10:25:25 AM Info [["date","medium","source","pageviews","sessions","users"],["20220301","(none)","(direct)","3","1","1"],["20220301","organic","google","3","1","1"],["20220302","(none)","(direct)","4","2","2"],["20220302","organic","bing","1","1","1"],["20220303","(none)","(direct)","1","1","1"]]
10:25:26 AM Notice Execution completed