Skip to content

Instantly share code, notes, and snippets.

@naosim
Created November 23, 2019 07:03
Show Gist options
  • Save naosim/452419e262a6fbdd9a773716acaf709f to your computer and use it in GitHub Desktop.
Save naosim/452419e262a6fbdd9a773716acaf709f to your computer and use it in GitHub Desktop.
予算管理で必要な日付への変換をする。スプレッドシートから呼び出すカスタム関数用
function comDate(dates, typesCsv) {
var convert = function(d, type) {
if(type == '年月') {
return d.getYear() + ('0' + (d.getMonth() + 1)).slice(-2);
}
var nendo = 'FY' + (d.getMonth() + 1 < 4 ? d.getYear() - 1: d.getYear());
if(type == '年度') {
return nendo;
}
if(type == '半期') {
return nendo + (['下期', '上期', '上期', '下期'][Math.floor(d.getMonth() / 3)])
}
if(type == '四半期') {
return nendo + '_' + (['4Q', '1Q', '2Q', '3Q'][Math.floor(d.getMonth() / 3)]);
}
var quartor = ['4Q', '1Q', '2Q', '3Q'][Math.floor(d.getMonth() / 3)];
}
// ['年月', '四半期', '半期', '年度']
// datesは2次元配列またはStringなので、それぞれ1次元配列にする
dates = Array.isArray(dates) ? dates.map(function(row){ return row[0]; }) : [dates]
var types = typesCsv.split(',');
dates = dates.map(function(date){
if(!date) {
return '';
}
var d = new Date(date);
if(isNaN(d)) {
return types.map(function(type) { return date + '_' + type});
}
return types.map(function(type) { return convert(d, type)});
})
if(dates.length == 1) {
if(dates[0].length == 1) {
return dates[0][0]
}
return dates[0];
}
return dates;
}
@naosim
Copy link
Author

naosim commented Jan 19, 2023

消す

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment