Gists
Description
When V8 is enabled, Array.apply has the limitation for the number of elements. When it is over the limitation, an error like RangeError: Maximum call stack size exceeded occurs, while the issue didn’t occur when V8 is disabled. In this case, this issue occurs at both Google Apps Script and Javascript. So please be careful this.
Sample situation
For example, when Array.prototype.push.apply is used for combining the arrays because the process cost of Array.prototype.push.apply is lowest of 3 patterns of Array.prototype.push.apply, Spread syntax and concat, please be careful above issue. Ref
Gists
This is a sample script for retrieving the values with and without duplicating from JSON object using Google Apps Script. Also this can be used by Javascript.
Sample script
var obj = [
{ key1: "value1a", key2: "value1b" },
{ key1: "value2a", key2: "value2b" },
{ key1: "value5a", key2: "value5b" },
{ key1: "value3a", key2: "value3b" },
{ key1: "value1a", key2: "value1b" },
{ key1: "value4a", key2: "value4b" },
{ key1: "value5a", key2: "value5b" },
{ key1: "value3a", key2: "value3b" }
];
var res = obj.reduce(
function(obj, e) {
if (
obj.withoutDuplicating.some(function(f) {
return f.key1 === e.key1 && f.key2 === e.key2;
})
) {
obj.withDuplicating.push(e);
} else {
obj.withoutDuplicating.push(e);
}
return obj;
},
{ withoutDuplicating: [], withDuplicating: [] }
);
Logger.log(res);
Result
{
"withoutDuplicating": [
{
"key1": "value1a",
"key2": "value1b"
},
{
"key1": "value2a",
"key2": "value2b"
},
{
"key1": "value5a",
"key2": "value5b"
},
{
"key1": "value3a",
"key2": "value3b"
},
{
"key1": "value4a",
"key2": "value4b"
}
],
"withDuplicating": [
{
"key1": "value1a",
"key2": "value1b"
},
{
"key1": "value5a",
"key2": "value5b"
},
{
"key1": "value3a",
"key2": "value3b"
}
]
}
Note
- As other situation, when
f.key1 === e.key1 && f.key2 === e.key2 is modified to f.key1 === e.key1, the duplication of key1 can be retrieved.
Reference
Gists
Overview
These are sample scripts for processing the duplicated rows of 2 dimensional arrays using Google Apps Script.
Description
When I use Google Spreadsheet and/or see Stackoverflow, I sometimes see the situation which is required to process the duplicated rows of 2 dimensional arrays. I thought that when the sample scripts for it have already prepared, they will be useful for other users including me. So I published this post. This sample scripts can be also used for Javascript. If this post is useful for you, I’m glad.
Gists
This is a sample script for splitting an array by n elements using Google Apps Script.
Sample script 1:
var limit = 3;
var ar = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
var res = [];
while (ar.length > 0) res.push(ar.splice(0, limit));
Logger.log(res); // [[1.0, 2.0, 3.0], [4.0, 5.0, 6.0], [7.0, 8.0, 9.0], [10.0]]
Above sample script is a simple. But at Google Apps Script, the process cost of “while” is higher than the for loop as shown in this report. So I recommend the following sample script for Google Apps Script.
Gists
This sample script retrieves the difference elements between 2 dimensional arrays using Google Apps Script. In Google Apps Script, 2 dimensional arrays are often used at Google Docs and Google APIs. And from my recent report, it has already found that the process cost of filter() is the lowest in the other loop methods. So I use the script like this.
var ar1 = [["a1", "b1", "c1"], ["a2", "b2", "c2"], ["a3", "b3", "c3"], ["a4", "b4", "c4"], ["a5", "b5", "c5"]];
var ar2 = [["a2", "b2", "c2"], ["a5", "b5", "c5"], ["a1", "b2", "c3"]];
var res = ar1.filter(function(e) {return ar2.filter(function(f) {return e.toString() == f.toString()}).length == 0});
Logger.log(res); // [["a1","b1","c1"],["a3","b3","c3"],["a4","b4","c4"]]
For above script, when it changes from == 0 to > 0, the duplication elements can be retrieved as follows.