Created
September 15, 2020 00:54
-
-
Save emmaly/d1787ea7b3ef0901910efe5e46361371 to your computer and use it in GitHub Desktop.
BOXARRAY
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function isEmpty_(value) { | |
if (value == undefined) return true; | |
if (value == null) return true; | |
if (typeof value == "string" && value == "") return true; | |
if (typeof value == "number") return false; | |
return false; | |
} | |
/** | |
* Expands the array to match lengths, and forces them into arrays. | |
* | |
* @param {...*} input Values of any type to length-match into same-sized arrays. Non-array values are converted into arrays with the value repeated for the full length of the new array. | |
* @return An array of arrays. | |
* @customfunction | |
*/ | |
function BOXARRAY(input) { | |
let output = new Array(arguments.length); | |
let length = 0; | |
// determine max length | |
for (let i=0; i<arguments.length; i++) { | |
if (typeof arguments[i] == "object") { | |
if (typeof arguments[i][0] == "object") arguments[i] = arguments[i].flat(1); | |
for (let j=0; j<arguments[i].length; j++) { | |
if (!isEmpty_(arguments[i][j]) && j >= length) length = j+1; | |
} | |
} | |
} | |
// set values | |
for (let i=0; i<arguments.length; i++) { | |
output[i] = new Array(length); | |
if (typeof arguments[i] == "object") { | |
for (let j=0; j<arguments[i].length; j++) { | |
if (j > length) continue; | |
output[i][j] = arguments[i][j]; | |
} | |
} else { | |
output[i].fill(arguments[i]); | |
} | |
} | |
// rotate the table | |
let o = new Array(length); | |
for (let x in output) { | |
for (let y in output[x]) { | |
if (x == 0) o[y] = new Array(arguments.length); | |
try { | |
o[y][x] = String(output[x][y]); | |
} catch(e) { | |
console.log(`${x}x${y}`); | |
} | |
} | |
} | |
return o; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment