Google Script Sort Json Nested Arrays
im trying to sort a nested array before copying back to Gsheet, i was helped on a previous part here, however when i save the script i get an error 'illegal Character', i have copi
Solution 1:
See if this works for you. I've transpiled it using Babel and added polyfills for Object.keys
and Object.entries
. For some reason Google Scripts sometimes choke on code that uses newer syntax:
if (!Object.keys) {
Object.keys = (function() {
'use strict';
var hasOwnProperty = Object.prototype.hasOwnProperty,
hasDontEnumBug = !({ toString: null }).propertyIsEnumerable('toString'),
dontEnums = [
'toString',
'toLocaleString',
'valueOf',
'hasOwnProperty',
'isPrototypeOf',
'propertyIsEnumerable',
'constructor'
],
dontEnumsLength = dontEnums.length;
return function(obj) {
if (typeof obj !== 'function' && (typeof obj !== 'object' || obj === null)) {
throw new TypeError('Object.keys called on non-object');
}
var result = [], prop, i;
for (prop in obj) {
if (hasOwnProperty.call(obj, prop)) {
result.push(prop);
}
}
if (hasDontEnumBug) {
for (i = 0; i < dontEnumsLength; i++) {
if (hasOwnProperty.call(obj, dontEnums[i])) {
result.push(dontEnums[i]);
}
}
}
return result;
};
}());
}
if (!Object.entries) {
Object.entries = function( obj ){
var ownProps = Object.keys( obj ),
i = ownProps.length,
resArray = new Array(i); // preallocate the Array
while (i--)
resArray[i] = [ownProps[i], obj[ownProps[i]]];
return resArray;
};
}
rows.push([data.campaignId, data.conversionCounting.floodlightActivityConfigs ? "[" + data.conversionCounting.floodlightActivityConfigs.map(function (e) {
return "{" + Object.entries(e).map(function (_ref) {
var k = _ref[0],
v = _ref[1];
return typeof v == "number" ? "\"" + k + "\":" + v : "\"" + k + "\":\"" + v + "\"";
}).sort().join(",") + "}";
}).join(",") + "]" : ""]);
Post a Comment for "Google Script Sort Json Nested Arrays"