Created
September 6, 2022 11:35
-
-
Save UskeS/63b30aa46a7e8d164b7aa15c75a12d56 to your computer and use it in GitHub Desktop.
[GAS]特定のセルから数値を拾い上げ中央値(メディアン)を取る関数
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
function getMedianByMonth(tagetYear, targetMonth) { | |
const currentSS = SpreadsheetApp.getActiveSpreadsheet(); | |
const targetSheet = currentSS.getSheetByName(tagetYear); | |
const targetRange = targetSheet.getDataRange(); | |
const tagetValues = targetRange.getValues(); | |
const result = tagetValues.filter(el => el[2] == targetMonth).map(x => x[9]); | |
const median = (ary) => { | |
if (ary.length === 0) { | |
return 0; | |
} | |
ary.sort((a,b) => { | |
return a - b; | |
}); | |
const half = Math.floor(ary.length / 2); | |
if (ary.length % 2) { | |
return ary[half]; | |
} else { | |
return (ary[half - 1] + ary[half]) / 2; | |
} | |
} | |
return median(result); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment