I wrote a simple iterator to loop trough a JSON object and parse the object into a new form. This works as expected in almost any JS environment. (For example in the console( However, the function below returns an empty array when executed in AppScript.
returns : [[], [], [], [], []]
This problem seems to be AppScript specific. Before I submit a bug through Google Developer Group I like to understand if this might be an App Script specific and intended behavior.
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 {
// 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 ) {
// 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 ) {
// store the column data in the current row array, using the column header as the key
wrapper[fields[j]] = TableData[i][j];
Logger.log("Wrapper : " wrapper) // This returns "Wrapper : "
}
// add the current row to the new array of parsed data
newTableData.push(wrapper);
}
// return the new array of parsed data
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)
}
}
CodePudding user response:
did not try in App Script but works fine with browser JS console :
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);
Logs :
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
So my best guess, check Logger print args types, and maybe Logger.log("Wrapper : ", wrapper);
?
CodePudding user response:
In App Script TableData
is defined in the global scope. However the parameter TableData
passed to parseBQData
is in the function scope. If I try to run your script function parseBQData(TableData, request)
including TableData
in global scope I get TypeError: Cannot read property 'length' of undefined
because TableData
is undefined. You can fix this by simply:
function parseBQData(request) {
This is not a bug.
I also found this article JavaScript function parameter and scope
CodePudding user response:
Same output but much simpler
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