Skip to content
This repository has been archived by the owner on Oct 10, 2023. It is now read-only.

Commit

Permalink
add excel key config
Browse files Browse the repository at this point in the history
  • Loading branch information
I-Info committed Aug 10, 2021
1 parent 518e241 commit b2ecac3
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 40 deletions.
18 changes: 9 additions & 9 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,16 @@ jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v2

- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: 1.16
- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: 1.16

- name: Build
run: |
mkdir dist
go build -o dist/info
- name: Build
run: |
mkdir dist
go build -o dist/info

34 changes: 17 additions & 17 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,22 @@ jobs:
tagged-release:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v2

- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: 1.16
- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: 1.16

- name: Build
run: |
mkdir dist
go build -o dist/info
- name: Publish release
uses: "marvinpinto/action-automatic-releases@latest"
with:
repo_token: "${{ secrets.GITHUB_TOKEN }}"
prerelease: false
files: |
dist/info
- name: Build
run: |
mkdir dist
go build -o dist/info
- name: Publish release
uses: "marvinpinto/action-automatic-releases@latest"
with:
repo_token: "${{ secrets.GITHUB_TOKEN }}"
prerelease: false
files: |
dist/info
26 changes: 23 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

ZJUT新生信息查询后端

[![Go](https://github.com/zjutjh/info-backend/actions/workflows/go.yml/badge.svg)](https://github.com/zjutjh/info-backend/actions/workflows/go.yml)[![GitHub release (latest SemVer)](https://img.shields.io/github/v/release/zjutjh/info-backend)](https://github.com/zjutjh/info-backend/releases)[![GitHub](https://img.shields.io/github/license/zjutjh/info-backend)](https://github.com/zjutjh/info-backend/blob/main/LICENSE)
[![Go](https://github.com/zjutjh/info-backend/actions/workflows/go.yml/badge.svg)](https://github.com/zjutjh/info-backend/actions/workflows/go.yml)[![GitHub
release (latest
SemVer)](https://img.shields.io/github/v/release/zjutjh/info-backend)](https://github.com/zjutjh/info-backend/releases)[![GitHub](https://img.shields.io/github/license/zjutjh/info-backend)](https://github.com/zjutjh/info-backend/blob/main/LICENSE)

## 如何编译

Expand Down Expand Up @@ -36,18 +38,36 @@ ZJUT新生信息查询后端
# example config file
# server config >>>
server-port: ":8080"
proxy: true
trusted-proxies:
- "127.0.0.0/8"
- "192.168.0.0/16"
- "172.16.0.0/12"
- "10.0.0.0/8"
# database config >>>
db-username: "root"
db-password: "passwd"
db-database: "info_test"
# "db-hostname: 127.0.0.1" & "port: 3306" can be omitted
# db-hostname: 127.0.0.1
# db-port: 3306
# excel keys >>>
excel:
name: "姓名"
id: "证件号"
campus: "校区"
faculty: "学院"
class: "班级"
uid: "学号"
house: "寝室楼"
room: "寝室号"
bed: "床号"
```

2. 从Excel载入数据到MySQL

> 注:Excel数据表格式很可能不通用。(导入已写的尽量智能,但是仍然不能保证适配每一年学校提供的数据,出了问题可能还是需要手动修改一下源码`read_excel.go`
> 注:Excel数据表格式很可能不通用, 可以通过修改`config.yaml`中的`excel`配置来匹配表头(第一行)。
> 导入已写的尽量智能了一些,但是仍然不能保证适配每一年学校提供的数据,出了问题可能还是需要手动修改一下源码`read_excel.go`

```
./info-backend -l [excel文件路径]
Expand Down Expand Up @@ -118,7 +138,7 @@ ZJUT新生信息查询后端
{"code":200,"data":{"name":"jxh01","campus":"朝晖","house":"#7","room":"101","bed":1,"friends":[{"name":"jxh02","class":"CS02","bed":2},{"name":"jxh03","class":"CS03","bed":3},{"name":"jxh04","class":"CS04","bed":4}]}}
```

3. 异常情况 (例)

```http
Expand Down
19 changes: 18 additions & 1 deletion config.yaml
Original file line number Diff line number Diff line change
@@ -1,10 +1,27 @@
# example config file
# server config >>>
server-port: ":8080"
proxy: true
trusted-proxies:
- "127.0.0.0/8"
- "192.168.0.0/16"
- "172.16.0.0/12"
- "10.0.0.0/8"
# database config >>>
db-username: "root"
db-password: "passwd"
db-database: "info_test"
# "db-hostname: 127.0.0.1" & "port: 3306" can be omitted
# db-hostname: 127.0.0.1
# db-port: 3306
# db-port: 3306
# excel keys >>>
excel:
name: "姓名"
id: "证件号"
campus: "校区"
faculty: "学院"
class: "班级"
uid: "学号"
house: "寝室楼"
room: "寝室号"
bed: "床号"
37 changes: 27 additions & 10 deletions handler/read_excel.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package handler

import (
"fmt"
"github.com/spf13/viper"
"github.com/xuri/excelize/v2"
"github.com/zjutjh/info-backend/data"
"github.com/zjutjh/info-backend/model"
Expand Down Expand Up @@ -34,41 +35,57 @@ func ReadInfo(path string, passwd string, sheet string, update bool) {
fmt.Println(err)
return
}
// index of column

// find index of column
var index = make(map[string]int)
var keys map[string]string
if !viper.IsSet("excel") {
keys = make(map[string]string)
keys["name"] = "姓名"
keys["id"] = "证件号"
keys["campus"] = "校区"
keys["faculty"] = "学院"
keys["class"] = "班级"
keys["uid"] = "学号"
keys["house"] = "寝室楼"
keys["room"] = "寝室号"
keys["bed"] = "床号"
} else {
keys = viper.GetStringMapString("excel")
}
row := rows[0]
{
indexSum1 := 0
indexSum2 := 0
for j, col := range row {
if strings.Index(col, "姓名") != -1 {
if strings.Index(col, keys["name"]) != -1 {
index["name"] = j
indexSum1++
indexSum2++
} else if strings.Index(col, "证件号") != -1 {
} else if strings.Index(col, keys["id"]) != -1 {
index["id"] = j
indexSum1++
indexSum2++
} else if strings.Index(col, "校区") != -1 {
} else if strings.Index(col, keys["campus"]) != -1 {
index["campus"] = j
indexSum1++
} else if strings.Index(col, "学院") != -1 {
} else if strings.Index(col, keys["faculty"]) != -1 {
index["faculty"] = j
indexSum1++
} else if strings.Index(col, "班级") != -1 {
} else if strings.Index(col, keys["class"]) != -1 {
index["class"] = j
indexSum1++
} else if strings.Index(col, "学号") != -1 {
} else if strings.Index(col, keys["uid"]) != -1 {
index["uid"] = j
indexSum1++
indexSum2++
} else if strings.Index(col, "寝室楼") != -1 {
} else if strings.Index(col, keys["house"]) != -1 {
index["house"] = j
indexSum2++
} else if strings.Index(col, "寝室号") != -1 {
} else if strings.Index(col, keys["room"]) != -1 {
index["room"] = j
indexSum2++
} else if strings.Index(col, "床号") != -1 {
} else if strings.Index(col, keys["bed"]) != -1 {
index["bed"] = j
indexSum2++
}
Expand Down

0 comments on commit b2ecac3

Please sign in to comment.