{"id":2775,"date":"2022-07-21T12:40:14","date_gmt":"2022-07-21T03:40:14","guid":{"rendered":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/?p=2775"},"modified":"2022-07-21T12:40:14","modified_gmt":"2022-07-21T03:40:14","slug":"golang-tag","status":"publish","type":"post","link":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/blog\/2022\/07\/21\/golang-tag\/","title":{"rendered":"Go\u3067\u69cb\u9020\u4f53\u306e\u30bf\u30b0\u3092\u6d3b\u7528\u3057\u3066Excel\u306e\u30c7\u30fc\u30bf\u3092\u8aad\u307f\u8fbc\u3080"},"content":{"rendered":"

\u3053\u3093\u306b\u3061\u306f\u3002\u30a8\u30f3\u30b8\u30cb\u30a2\u306e\u7267\u7530\u3067\u3059\u3002
\n\u4ee5\u524d\u5b9f\u88c5\u3057\u305f\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u306e\u6a5f\u80fd\u3068\u3057\u3066\u3001Excel\u306e\u30c7\u30fc\u30bf\u3092\u8aad\u307f\u8fbc\u3080\u6a5f\u80fd\u304c\u3042\u3063\u305f\u306e\u3067\u3059\u304c\u3001\u305d\u306e\u6642\u306b\u884c\u3063\u305f\u624b\u6cd5\u3092\u7d39\u4ecb\u3057\u305f\u3044\u3068\u601d\u3044\u307e\u3059\u3002\uff08\u5b9f\u88c5\u306fGo\u8a00\u8a9e(v1.18)\u3067\u3059\uff09<\/p>\n

\u4eca\u56de\u7528\u3044\u3066\u3044\u308b\u30b5\u30f3\u30d7\u30eb\u30b3\u30fc\u30c9\u306f\u2193\u306b\u3042\u308a\u307e\u3059\u3002
\nhttps:\/\/github.com\/bbj14\/go-tag-example\/tree\/v1<\/a><\/p>\n

\u80cc\u666f<\/h2>\n

\u4f8b\u3068\u3057\u3066\u4ee5\u4e0b\u306e\u3088\u3046\u306aExcel\u306e\u60c5\u5831\u3092\u8aad\u307f\u8fbc\u3080\u3053\u3068\u3092\u8003\u3048\u307e\u3059\u3002<\/p>\n

\"\"<\/p>\n

\u3053\u306e\u5404\u4eba\u306e\u60c5\u5831\u3092\u8aad\u307f\u53d6\u308b\u305f\u3081\u306bPerson\u3068\u3044\u3046\u69cb\u9020\u4f53\u3092\u4f5c\u308a\u307e\u3059\u3002<\/p>\n

type Person struct {\n    Name      string\n    Age       int64\n    Weight    float64\n    BirthDate time.Time\n}<\/code><\/pre>\n

Excel\u306e\u8aad\u307f\u8fbc\u307f\u306f\u3001excelize<\/a>\u3068\u3044\u3046\u30e9\u30a4\u30d6\u30e9\u30ea\u3092\u4f7f\u3063\u3066\u884c\u3046\u3053\u3068\u306b\u3057\u307e\u3059\u3002
\nexcelize\u3067\u306fGetRows\u3068\u3044\u3046\u30e1\u30bd\u30c3\u30c9\u304c\u7528\u610f\u3055\u308c\u3066\u304a\u308a\u3001\u3053\u308c\u3092\u4f7f\u3046\u3053\u3068\u306b\u3088\u3063\u3066\u30b7\u30fc\u30c8\u306e\u30bb\u30eb\u5168\u4f53\u3092[][]string\u306b\u5909\u63db\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n

  f, err := excelize.OpenFile(".\/sample.xlsx")\n  rows, err := f.GetRows("Sheet1")<\/code><\/pre>\n

\u3042\u3068\u306f\u3001rows\u304b\u3089\u5404\u30bb\u30eb\u306e\u5024\u3092\u53d6\u5f97\u3059\u308c\u3070\u3044\u3044\u308f\u3051\u3067\u3059\u304c\u3001Excel\u306e\u5217\u540d\uff08A,B,C\u2026\uff09\u304b\u3089\u5024\u3092\u53d6\u5f97\u3059\u308b\u305f\u3081\u306b\u3001getCol\u3068\u3044\u3046\u95a2\u6570\u3092\u4f5c\u308a\u307e\u3057\u305f\u3002\uff08excelize.ColumnNameToNumber\u306f\u3001\u5217\u540d\u3092\u914d\u5217\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u306b\u5909\u63db\u3059\u308b\u95a2\u6570\u3067\u3059\u3002\uff09<\/p>\n

\/\/ \u884c\u304b\u3089\u7279\u5b9a\u306e\u5217\u306e\u5024\u3092\u53d6\u5f97\nfunc getCol(row []string, col string) (string, error) {\n    c, err := excelize.ColumnNameToNumber(col)\n    if err != nil {\n        return "", err\n    }\n    \/\/ \u884c\u306e\u6700\u5f8c\u306e\u7a7a\u6b04\u306fGetRows\u3067\u8aad\u307f\u8fbc\u307e\u308c\u306a\u3044\u305f\u3081\u3001\u305d\u3053\u3092\u53c2\u7167\u3057\u3066\u3044\u308b\u5834\u5408\u306f\u7a7a\u6587\u5b57\u3092\u8fd4\u3059\n    if len(row) < c {\n        return "", nil\n    }\n    return row[c-1], nil\n}<\/code><\/pre>\n

\u3053\u308c\u3092\u4f7f\u3046\u3068\u3001\u5404\u884c\uff08\u30e9\u30d9\u30eb\u3092\u9664\u304f2\u884c\u76ee\u4ee5\u964d\uff09\u306b\u5bfe\u3057\u3066\u69cb\u9020\u4f53\u306b\u5024\u3092\u30bb\u30c3\u30c8\u3059\u308b\u30b3\u30fc\u30c9\u306f\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u306a\u308b\u3068\u601d\u3044\u307e\u3059\u3002\uff08\u30a8\u30e9\u30fc\u51e6\u7406\u306f\u7701\u7565\uff09<\/p>\n

  for _, row := range rows[1:] {\n    name, err := getCol(row, "A")\n    age, err := getCol(row, "B")\n    weight, err := getCol(row, "C")\n    birthdate, err := getCol(row, "D")\n\n    p := Person{}\n\n    p.Name = name\n    p.Age, err = strconv.ParseInt(age, 10, 64)\n    p.Weight, err = strconv.ParseFloat(weight, 64)\n    p.Birthdate, err = dateparse.ParseAny(birthdate)\n\n    \/\/ (p\u3092\u4f7f\u3063\u305f\u51e6\u7406\u3092\u66f8\u304f)\n  }\n<\/code><\/pre>\n

\u3057\u304b\u3057\u3001\u3053\u306e\u5b9f\u88c5\u306b\u306f\u4ee5\u4e0b\u306e\u3088\u3046\u306a\u554f\u984c\u304c\u3042\u308a\u307e\u3059\u3002<\/p>\n