1
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

GoogleSpreadSheetで和暦から西暦へ変換する

1
Last updated at Posted at 2021-04-28

西暦から和暦(YYYY-MM-DD)への変換にGoogleSpreadSheetを利用する際の関数を作ったので残しておきます(昭和・平成・令和のみ対応)。
例)
平成29年10月13日 => 2017-10-13
令和1年7月10日 => 2019-07-10

注:適当に参照セルは変更してください(変更3箇所:初期値A1)。

=SUBSTITUTE(REGEXREPLACE(REGEXREPLACE(REGEXREPLACE(SWITCH(REGEXEXTRACT(A1,"[^0-9]+"),"昭和",1925,"平成",1988,"令和",2018)+REGEXEXTRACT(A1,"[0-9]+")&REGEXEXTRACT(A1,"[0-9]+(.+)"), "年([0-9])月","年0$1月"), "月([0-9])日","月0$1日"), "[年月]", "-"), "日", "")

空白セルで「N/A」を返さないようにするバージョンは以下で(変更4箇所:初期値A1)。

=IF(ISTEXT(A1),SUBSTITUTE(REGEXREPLACE(REGEXREPLACE(REGEXREPLACE(SWITCH(REGEXEXTRACT(A1,"[^0-9]+"),"昭和",1925,"平成",1988,"令和",2018)+REGEXEXTRACT(A1,"[0-9]+")&REGEXEXTRACT(A1,"[0-9]+(.+)"), "年([0-9])月","年0$1月"), "月([0-9])日","月0$1日"), "[年月]", "-"), "日", ""),"")

なお、年号の変換はこちらの記事のコードをそのまま使わせていただきました。感謝。

以下、インデント版もついでに貼っておきます。

=IF(ISTEXT(A1),
  SUBSTITUTE(
    REGEXREPLACE(
      REGEXREPLACE(
        REGEXREPLACE(
          SWITCH(REGEXEXTRACT(A1,"[^0-9]+"),"昭和",1925,"平成",1988,"令和",2018)
            +REGEXEXTRACT(A1,"[0-9]+")
            &REGEXEXTRACT(A1,"[0-9]+(.+)")
        , "年([0-9])月","年0$1月") 
      , "月([0-9])日","月0$1日")
    , "[年月]", "-")
  , "日", "")
,"")
1
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?