Created
May 11, 2020 16:51
-
-
Save ndthanh/d9bd6eaf45b813f42da138e9463b5566 to your computer and use it in GitHub Desktop.
This file contains 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
/** | |
* Unpivots the given data | |
* More: https://stackoverflow.com/questions/24954722/how-do-you-create-a-reverse-pivot-in-google-sheets | |
* | |
* @return Unpivoted data from array | |
* @param {object[][]} arr 2D Input Array | |
* @param {object[][]=} headers [optional] Custom headers for output | |
* @customfunction | |
*/ | |
function unpivot(arr, headers) { | |
const custHeader = arr.shift(); | |
custHeader.shift(); | |
const out = arr.flatMap(([sale, ...qty]) => | |
qty.map((num, i) => [custHeader[i], sale, num]) | |
); | |
if (headers) out.unshift(headers[0]); | |
return out; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment