作者 唐旭辉

分红列表导出

要显示太多修改。

为保证性能只显示 23 of 23+ 个文件。

@@ -3,6 +3,7 @@ module gitlab.fjmaimaimai.com/mmm-go/partnermg @@ -3,6 +3,7 @@ module gitlab.fjmaimaimai.com/mmm-go/partnermg
3 go 1.14 3 go 1.14
4 4
5 require ( 5 require (
  6 + github.com/360EntSecGroup-Skylar/excelize/v2 v2.3.1
6 github.com/GeeTeam/gt3-golang-sdk v0.0.0-20200116043922-446ca8a507d2 7 github.com/GeeTeam/gt3-golang-sdk v0.0.0-20200116043922-446ca8a507d2
7 github.com/Shopify/sarama v1.23.1 8 github.com/Shopify/sarama v1.23.1
8 github.com/ajg/form v1.5.1 // indirect 9 github.com/ajg/form v1.5.1 // indirect
@@ -6,6 +6,7 @@ type ListOrderBonusQuery struct { @@ -6,6 +6,7 @@ type ListOrderBonusQuery struct {
6 // 查询限制 6 // 查询限制
7 Limit int `json:"limit"` 7 Limit int `json:"limit"`
8 CompanyId int64 `json:"companyId"` 8 CompanyId int64 `json:"companyId"`
  9 + PartnerCategory int `json:"partner_category"`
9 //订单类型 10 //订单类型
10 OrderType int `json:"orderType"` 11 OrderType int `json:"orderType"`
11 PartnerOrCode string `json:"partner_or_code"` 12 PartnerOrCode string `json:"partner_or_code"`
@@ -1178,3 +1178,81 @@ func (service OrderInfoService) UpdateOrderRemarkBonus(orderId int64, adminId in @@ -1178,3 +1178,81 @@ func (service OrderInfoService) UpdateOrderRemarkBonus(orderId int64, adminId in
1178 } 1178 }
1179 return nil 1179 return nil
1180 } 1180 }
  1181 +
  1182 +func (service OrderInfoService) ListOrderBonusForExcel(listOrderQuery query.ListOrderBonusQuery) ([]map[string]string, [][2]string, error) {
  1183 + transactionContext, err := factory.CreateTransactionContext(nil)
  1184 + if err != nil {
  1185 + return nil, nil, lib.ThrowError(lib.TRANSACTION_ERROR, err.Error())
  1186 + }
  1187 + if err = transactionContext.StartTransaction(); err != nil {
  1188 + return nil, nil, lib.ThrowError(lib.TRANSACTION_ERROR, err.Error())
  1189 + }
  1190 + defer func() {
  1191 + transactionContext.RollbackTransaction()
  1192 + }()
  1193 + var (
  1194 + orderBaseDao *dao.OrderBaseDao
  1195 + )
  1196 +
  1197 + if orderBaseDao, err = factory.CreateOrderBaseDao(map[string]interface{}{
  1198 + "transactionContext": transactionContext,
  1199 + }); err != nil {
  1200 + return nil, nil, lib.ThrowError(lib.TRANSACTION_ERROR, err.Error())
  1201 + }
  1202 + result, err := orderBaseDao.OrderBonusListForExcel(
  1203 + listOrderQuery.CompanyId,
  1204 + listOrderQuery.OrderType,
  1205 + listOrderQuery.PartnerOrCode,
  1206 + listOrderQuery.PartnerCategory,
  1207 + )
  1208 + if err != nil {
  1209 + return nil, nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
  1210 + }
  1211 +
  1212 + err = transactionContext.CommitTransaction()
  1213 + if err != nil {
  1214 + return nil, nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
  1215 + }
  1216 + var resultMaps []map[string]string
  1217 + for i := range result {
  1218 + m := map[string]string{
  1219 + "num": fmt.Sprint(i + 1),
  1220 + "order_type": domain.GetOrderBaseTypeName(result[i].OrderType),
  1221 + "order_code": result[i].OrderCode,
  1222 + "delivery_code": result[i].DeliveryCode,
  1223 + "partner_name": result[i].PartnerName,
  1224 + "bonus_status": "",
  1225 + "update_time": result[i].UpdateTime,
  1226 + "partner_bonus": fmt.Sprint(result[i].PartnerBonus),
  1227 + "partner_bonus_has": fmt.Sprint(result[i].PartnerBonusHas),
  1228 + "partner_bonus_not": fmt.Sprint(result[i].PartnerBonusNot),
  1229 + "partner_bonus_expense": fmt.Sprint(result[i].PartnerBonusExpense),
  1230 + }
  1231 + if result[i].HasBonusPercent == 0 {
  1232 + m["partner_bonus"] = "-"
  1233 + m["partner_bonus_has"] = "-"
  1234 + m["partner_bonus_not"] = "-"
  1235 + m["partner_bonus_expense"] = "-"
  1236 + }
  1237 + switch result[i].BonusStatus {
  1238 + case domain.OrderGoodWaitPay:
  1239 + m["bonus_status"] = "等待支付分红"
  1240 + case domain.OrderGoodHasPay:
  1241 + m["bonus_status"] = "已支付分红"
  1242 + }
  1243 + resultMaps = append(resultMaps, m)
  1244 + }
  1245 + column := [][2]string{
  1246 + [2]string{"num", "序号"},
  1247 + [2]string{"order_type", "订单类型"},
  1248 + [2]string{"order_code", "订单号"},
  1249 + [2]string{"partner_name", "合伙人"},
  1250 + [2]string{"bonus_status", "订单状态"},
  1251 + [2]string{"update_time", "最后操作时间"},
  1252 + [2]string{"partner_bonus", "应收分红"},
  1253 + [2]string{"partner_bonus_has", "已收分红"},
  1254 + [2]string{"partner_bonus_not", "未收分红"},
  1255 + [2]string{"partner_bonus_expense", "分红支出"},
  1256 + }
  1257 + return resultMaps, column, nil
  1258 +}
1 package service 1 package service
2 2
3 import ( 3 import (
4 - "errors"  
5 -  
6 "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/unifiedUserCenter/command" 4 "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/unifiedUserCenter/command"
7 ) 5 )
8 6
@@ -30,7 +28,7 @@ func NewSyncAction(cmd command.SyncCallbackCommand) error { @@ -30,7 +28,7 @@ func NewSyncAction(cmd command.SyncCallbackCommand) error {
30 ok bool 28 ok bool
31 ) 29 )
32 if action, ok = actionMap[cmd.Module]; !ok { 30 if action, ok = actionMap[cmd.Module]; !ok {
33 - return errors.New("module cannot found") 31 + return nil
34 } 32 }
35 return action.DoAction(cmd.Action, cmd.Data) 33 return action.DoAction(cmd.Action, cmd.Data)
36 } 34 }
@@ -14,7 +14,7 @@ var KafkaCfg KafkaConfig @@ -14,7 +14,7 @@ var KafkaCfg KafkaConfig
14 14
15 func init() { 15 func init() {
16 KafkaCfg = KafkaConfig{ 16 KafkaCfg = KafkaConfig{
17 - Servers: []string{"192.168.190.136:9092"}, 17 + Servers: []string{""},
18 ConsumerId: "partnermg_local", 18 ConsumerId: "partnermg_local",
19 } 19 }
20 if os.Getenv("KAFKA_HOST") != "" { 20 if os.Getenv("KAFKA_HOST") != "" {
@@ -114,6 +114,8 @@ type OrderBase struct { @@ -114,6 +114,8 @@ type OrderBase struct {
114 DataFrom OrderDataFrom `json:"dataFrom"` 114 DataFrom OrderDataFrom `json:"dataFrom"`
115 //备注 115 //备注
116 Remark OrderBaseRemark `json:"remark"` 116 Remark OrderBaseRemark `json:"remark"`
  117 +
  118 + PartnerCategory PartnerCategory `json:"partnerCategory"`
117 } 119 }
118 120
119 //GetCurrentPartnerBonus 获取当前合伙人应收分红 121 //GetCurrentPartnerBonus 获取当前合伙人应收分红
@@ -4,7 +4,6 @@ import ( @@ -4,7 +4,6 @@ import (
4 "fmt" 4 "fmt"
5 5
6 "github.com/go-pg/pg/v10/orm" 6 "github.com/go-pg/pg/v10/orm"
7 -  
8 "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/domain" 7 "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/domain"
9 "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/infrastructure/pg/models" 8 "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/infrastructure/pg/models"
10 "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/infrastructure/pg/transaction" 9 "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/infrastructure/pg/transaction"
@@ -74,3 +73,116 @@ func (dao OrderBaseDao) OrderBonusListByCondition(companyId int64, orderType int @@ -74,3 +73,116 @@ func (dao OrderBaseDao) OrderBonusListByCondition(companyId int64, orderType int
74 cnt, err := query.SelectAndCount() 73 cnt, err := query.SelectAndCount()
75 return orders, cnt, err 74 return orders, cnt, err
76 } 75 }
  76 +
  77 +//导出分红列表所用的sql
  78 +// SELECT
  79 +// ROW_NUMBER() over(ORDER BY t1.update_time DESC) AS num,
  80 +// t1.ID AS order_id,
  81 +// t1.order_type,
  82 +// t1.order_code,
  83 +// t1.bonus_status,
  84 +// to_char(t1.update_time at time zone 'CCT' ,'YYYY-MM-DD HH24:MI:SS') AS update_time,
  85 +// (CASE
  86 +// WHEN t1.use_partner_bonus>0
  87 +// THEN
  88 +// t1.use_partner_bonus
  89 +// ELSE
  90 +// t1.plan_partner_bonus
  91 +// END) AS partner_bonus,
  92 +// t1.partner_bonus_has,
  93 +// t1.partner_bonus_not,
  94 +// t1.partner_bonus_expense,
  95 +// tt1.has_bonus_percent,
  96 +// t2.partner_name
  97 +// FROM
  98 +// order_base AS t1
  99 +// LEFT JOIN partner_info AS t2 ON t1.partner_id = t2."id"
  100 +// LEFT JOIN (
  101 +// SELECT COUNT ( * ) AS has_bonus_percent, t3.order_id
  102 +// FROM "order_good" AS t3
  103 +// WHERE t3.partner_bonus_percent >= 0
  104 +// GROUP BY t3.order_id
  105 +// ) AS tt1 ON t1."id" = tt1.order_id
  106 +
  107 +type CustomOrderBonusForExcel struct {
  108 + Num int
  109 + OrderId int64
  110 + OrderType int
  111 + OrderCode string
  112 + BonusStatus int
  113 + DeliveryCode string
  114 + UpdateTime string
  115 + PartnerBonus float64
  116 + PartnerBonusHas float64
  117 + PartnerBonusNot float64
  118 + PartnerBonusExpense float64
  119 + HasBonusPercent int
  120 + PartnerName string
  121 +}
  122 +
  123 +//OrderBonusListForExcel 导出分红列表所用
  124 +//@param companyId 公司id
  125 +//@param orderType 订单类型
  126 +//@param partnerOrCode 合伙人姓名或订单号或发货单号
  127 +//@param partnerCategory 合伙人类型id
  128 +//@return result 处理后的数据,可直接用于导出数据到excel
  129 +//@return column 数据对应的键名,例 [0][英文键名,中文名称]
  130 +func (dao OrderBaseDao) OrderBonusListForExcel(companyId int64, orderType int, partnerOrCode string,
  131 + partnerCategory int) (result []CustomOrderBonusForExcel, err error) {
  132 +
  133 + sqlStr := `SELECT
  134 + t1.ID AS order_id,
  135 + t1.order_type,
  136 + t1.order_code,
  137 + t1.delivery_code,
  138 + t1.bonus_status,
  139 + to_char(t1.update_time AT TIME ZONE 'CCT' ,'YYYY-MM-DD HH24:MI:SS') AS update_time,
  140 + (CASE
  141 + WHEN t1.use_partner_bonus>0
  142 + THEN
  143 + t1.use_partner_bonus
  144 + ELSE
  145 + t1.plan_partner_bonus
  146 + END) AS partner_bonus,
  147 + t1.partner_bonus_has,
  148 + t1.partner_bonus_not,
  149 + t1.partner_bonus_expense,
  150 + tt1.has_bonus_percent,
  151 + t2.partner_name
  152 + FROM
  153 + order_base AS t1
  154 + LEFT JOIN partner_info AS t2 ON t1.partner_id = t2."id"
  155 + LEFT JOIN (
  156 + SELECT COUNT ( * ) AS has_bonus_percent, t3.order_id
  157 + FROM "order_good" AS t3
  158 + WHERE t3.partner_bonus_percent >= 0
  159 + GROUP BY t3.order_id
  160 + ) AS tt1 ON t1."id" = tt1.order_id
  161 + WHERE 1=1 `
  162 + sqlStr += ` AND t1.company_id = ? `
  163 + param := []interface{}{companyId}
  164 + if orderType > 0 {
  165 + param = append(param, orderType)
  166 + sqlStr += ` AND t1.order_type=? `
  167 + } else {
  168 + param = append(param, domain.OrderIntention)
  169 + sqlStr += ` AND t1.order_type<>? `
  170 + }
  171 + if len(partnerOrCode) > 0 {
  172 + sqlStr += ` AND (t1.order_code like ? OR t1.delivery_code like ? OR t2.partner_name like ? )`
  173 + likeParam := "%" + partnerOrCode + "%"
  174 + param = append(param, likeParam, likeParam, likeParam)
  175 + }
  176 + if partnerCategory > 0 {
  177 + sqlStr += ` AND t1.partner_category @>'{"id":?}'`
  178 + param = append(param, partnerCategory)
  179 + }
  180 + sqlStr += ` ORDER BY t1.update_time DESC limit 10 `
  181 + tx := dao.transactionContext.GetDB()
  182 + _, err = tx.Query(&result, sqlStr, param...)
  183 +
  184 + if err != nil {
  185 + return result, err
  186 + }
  187 + return result, nil
  188 +}
@@ -67,6 +67,8 @@ type OrderBase struct { @@ -67,6 +67,8 @@ type OrderBase struct {
67 DataFrom domain.OrderDataFrom `` 67 DataFrom domain.OrderDataFrom ``
68 //备注 68 //备注
69 Remark domain.OrderBaseRemark `` 69 Remark domain.OrderBaseRemark ``
  70 + //合伙人类型
  71 + PartnerCategory domain.PartnerCategory ``
70 } 72 }
71 73
72 var _ pg.BeforeUpdateHook = (*OrderBase)(nil) 74 var _ pg.BeforeUpdateHook = (*OrderBase)(nil)
  1 +package exceltool
  2 +
  3 +import (
  4 + "errors"
  5 + "fmt"
  6 + "math/rand"
  7 + "time"
  8 +
  9 + excelize "github.com/360EntSecGroup-Skylar/excelize/v2"
  10 +)
  11 +
  12 +type ExcelHead struct {
  13 + Name string
  14 + Key string
  15 +}
  16 +
  17 +//ExcelMaker 构建excel文档
  18 +type ExcelMaker struct {
  19 + Xlsx *excelize.File
  20 + fileName string
  21 + header []ExcelHead
  22 +}
  23 +
  24 +//NewExcelMaker ....
  25 +func NewExcelMaker() *ExcelMaker {
  26 + return &ExcelMaker{
  27 + Xlsx: excelize.NewFile(),
  28 + }
  29 +}
  30 +
  31 +func (e *ExcelMaker) SetListHead(h []ExcelHead) {
  32 + e.header = h
  33 +}
  34 +
  35 +func (e *ExcelMaker) SetFileName(n string) {
  36 + e.fileName = n
  37 +}
  38 +
  39 +func (e *ExcelMaker) GetFileName() string {
  40 + return e.fileName
  41 +}
  42 +
  43 +//MakeListExcel 根据列表形式的数据创建excel文档
  44 +//@sourData []map[string]string; 原始数据,要输入excel文档的数据
  45 +func (e *ExcelMaker) MakeListExcel(sourData []map[string]string) (err error) {
  46 + if len(e.header) == 0 {
  47 + return errors.New("xlsHeader 数据格式错误")
  48 + }
  49 + headEn := []string{} //数据字段英文名
  50 + headCn := []string{} //excel字段中文描述
  51 + alphaSlice := []string{} //excel列字母索引
  52 + for key, val := range e.header {
  53 + headEn = append(headEn, val.Key)
  54 + headCn = append(headCn, val.Name)
  55 + alpha, _ := excelize.ColumnNumberToName(key + 1)
  56 + // alpha := ToAlphaString(key)
  57 + alphaSlice = append(alphaSlice, alpha)
  58 + }
  59 +
  60 + //设置excel文档第一行的字段中文描述
  61 + for index := range headCn {
  62 + //索引转列名,索引从0开始
  63 + cellAlpha := fmt.Sprintf("%s%d", alphaSlice[index], 1) // 单元格行坐标从1开始,如:a1,指第一行a列。
  64 + e.Xlsx.SetCellStr("Sheet1", cellAlpha, headCn[index])
  65 +
  66 + }
  67 + //从excel第二行开始设置实际数据的值
  68 + for key1 := range sourData {
  69 + for i := 0; i < len(headEn); i++ {
  70 + cellAlpha := fmt.Sprintf("%s%d", alphaSlice[i], key1+2) // 单元格行坐标从1开始,如:a1,指第一行a列。
  71 + e.Xlsx.SetCellStr("Sheet1", cellAlpha, sourData[key1][headEn[i]])
  72 + }
  73 + }
  74 + e.SetFileName(GetRandomString(8) + ".xlsx")
  75 + return nil
  76 +}
  77 +
  78 +//GetRandomString 生成随机字符串
  79 +func GetRandomString(lenght int) string {
  80 + str := "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
  81 + bytes := []byte(str)
  82 + result := []byte{}
  83 + r := rand.New(rand.NewSource(time.Now().UnixNano()))
  84 + for i := 0; i < lenght; i++ {
  85 + result = append(result, bytes[r.Intn(len(bytes))])
  86 + }
  87 + return string(result)
  88 +}
  89 +
  90 +func ToAlphaString(value int) string {
  91 + if value < 0 {
  92 + return ""
  93 + }
  94 + var ans string
  95 + i := value + 1
  96 + for i > 0 {
  97 + ans = string((i-1)%26+65) + ans
  98 + i = (i - 1) / 26
  99 + }
  100 + return ans
  101 +}
@@ -8,12 +8,15 @@ import ( @@ -8,12 +8,15 @@ import (
8 "strconv" 8 "strconv"
9 "strings" 9 "strings"
10 10
  11 + "github.com/astaxie/beego/context"
  12 +
11 "github.com/astaxie/beego" 13 "github.com/astaxie/beego"
12 "github.com/astaxie/beego/logs" 14 "github.com/astaxie/beego/logs"
13 15
14 userQuery "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/users/query" 16 userQuery "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/users/query"
15 userService "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/users/service" 17 userService "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/users/service"
16 "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/lib" 18 "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/lib"
  19 + "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/lib/exceltool"
17 "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/port/beego/protocol" 20 "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/port/beego/protocol"
18 ) 21 )
19 22
@@ -206,3 +209,17 @@ func (controller *BaseController) GetUserCompany() int64 { @@ -206,3 +209,17 @@ func (controller *BaseController) GetUserCompany() int64 {
206 } 209 }
207 return uid 210 return uid
208 } 211 }
  212 +
  213 +func (controller *BaseController) ResponseExcelByFile(ctx *context.Context, excelMaker *exceltool.ExcelMaker) error {
  214 + fname := excelMaker.GetFileName()
  215 + ctx.Output.Header("Content-Disposition", "attachment; filename="+fname)
  216 + ctx.Output.Header("Content-Description", "File Transfer")
  217 + ctx.Output.Header("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
  218 + ctx.Output.Header("Content-Transfer-Encoding", "binary")
  219 + ctx.Output.Header("Expires", "0")
  220 + ctx.Output.Header("Cache-Control", "must-revalidate")
  221 + ctx.Output.Header("Pragma", "public")
  222 + //跳过保存文件,直接写入ctx.ResponseWriter
  223 + excelMaker.Xlsx.Write(ctx.ResponseWriter)
  224 + return nil
  225 +}
@@ -36,12 +36,20 @@ func (c *CommonController) GetPartnerList() { @@ -36,12 +36,20 @@ func (c *CommonController) GetPartnerList() {
36 } 36 }
37 resp := []map[string]interface{}{} 37 resp := []map[string]interface{}{}
38 for i := range partners { 38 for i := range partners {
  39 + categorys := []map[string]interface{}{}
  40 + for _, vv := range partners[i].PartnerCategoryInfos {
  41 + c := map[string]interface{}{
  42 + "id": vv.Id,
  43 + "name": vv.Name,
  44 + }
  45 + categorys = append(categorys, c)
  46 + }
39 m := map[string]interface{}{ 47 m := map[string]interface{}{
40 "id": partners[i].Partner.Id, 48 "id": partners[i].Partner.Id,
41 "account": partners[i].Partner.Account, 49 "account": partners[i].Partner.Account,
42 "partnerName": partners[i].Partner.PartnerName, 50 "partnerName": partners[i].Partner.PartnerName,
  51 + "categorys": categorys,
43 } 52 }
44 -  
45 resp = append(resp, m) 53 resp = append(resp, m)
46 } 54 }
47 c.ResponseData(resp) 55 c.ResponseData(resp)
@@ -10,6 +10,7 @@ import ( @@ -10,6 +10,7 @@ import (
10 orderQuery "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/orderinfo/query" 10 orderQuery "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/orderinfo/query"
11 orderService "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/orderinfo/service" 11 orderService "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/orderinfo/service"
12 "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/domain" 12 "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/domain"
  13 + "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/lib/exceltool"
13 ) 14 )
14 15
15 //OrderDividendController 订单分红管理 16 //OrderDividendController 订单分红管理
@@ -399,3 +400,52 @@ func (c *OrderDividendController) EditOrderRemarkBonusForBestshop() { @@ -399,3 +400,52 @@ func (c *OrderDividendController) EditOrderRemarkBonusForBestshop() {
399 c.ResponseData(nil) 400 c.ResponseData(nil)
400 return 401 return
401 } 402 }
  403 +
  404 +func (c *OrderDividendController) ListOrderBonusForExcel() {
  405 + type Parameter struct {
  406 + SearchWord string `json:"searchWord"`
  407 + OrderType int `json:"orderType"`
  408 + PageSize int `json:"pageSize"`
  409 + PageNumber int `json:"pageNumber"`
  410 + }
  411 + var (
  412 + param Parameter
  413 + err error
  414 + )
  415 + if err = c.BindJsonData(&param); err != nil {
  416 + logs.Error(err)
  417 + c.ResponseError(errors.New("json数据解析失败"))
  418 + return
  419 + }
  420 + if !(param.OrderType == 0 ||
  421 + param.OrderType == domain.OrderReal ||
  422 + param.OrderType == domain.OrderTypeBestShop) {
  423 + c.ResponseError(errors.New("参数异常"))
  424 + return
  425 + }
  426 +
  427 + companyId := c.GetUserCompany()
  428 + orderSrv := orderService.NewOrderInfoService(nil)
  429 + dataResult, column, err := orderSrv.ListOrderBonusForExcel(orderQuery.ListOrderBonusQuery{
  430 + OrderType: param.OrderType,
  431 + PartnerOrCode: param.SearchWord,
  432 + CompanyId: companyId,
  433 + })
  434 + if err != nil {
  435 + c.ResponseError(err)
  436 + return
  437 + }
  438 + var excelHeaders []exceltool.ExcelHead
  439 + for i := range column {
  440 + h := exceltool.ExcelHead{
  441 + Key: column[i][0],
  442 + Name: column[i][1],
  443 + }
  444 + excelHeaders = append(excelHeaders, h)
  445 + }
  446 + excelMaker := exceltool.NewExcelMaker()
  447 + excelMaker.SetListHead(excelHeaders)
  448 + excelMaker.MakeListExcel(dataResult)
  449 + c.ResponseExcelByFile(c.Ctx, excelMaker)
  450 + return
  451 +}
@@ -33,10 +33,10 @@ func init() { @@ -33,10 +33,10 @@ func init() {
33 beego.NSRouter("/mini-program/modify", &controllers.OrderDividendController{}, "POST:EditOrderDividendForBestshop"), 33 beego.NSRouter("/mini-program/modify", &controllers.OrderDividendController{}, "POST:EditOrderDividendForBestshop"),
34 beego.NSRouter("/mini-program/payDividends", &controllers.OrderDividendController{}, "POST:PayOrderGoodBonusForBestshop"), 34 beego.NSRouter("/mini-program/payDividends", &controllers.OrderDividendController{}, "POST:PayOrderGoodBonusForBestshop"),
35 beego.NSRouter("/mini-program/remarks", &controllers.OrderDividendController{}, "POST:EditOrderRemarkBonusForBestshop"), 35 beego.NSRouter("/mini-program/remarks", &controllers.OrderDividendController{}, "POST:EditOrderRemarkBonusForBestshop"),
36 -  
37 - beego.NSRouter("/business/detail", &controllers.BusinessBonusController{}, "POST:GetBusinessBonus"),  
38 - beego.NSRouter("/business/edit", &controllers.BusinessBonusController{}, "POST:UpdateBusinessBonus"),  
39 - beego.NSRouter("/business/list", &controllers.BusinessBonusController{}, "POST:ListBusinessBonus"), 36 + beego.NSRouter("/list/excel", &controllers.OrderDividendController{}, "POST:ListOrderBonusForExcel"),
  37 + // beego.NSRouter("/business/detail", &controllers.BusinessBonusController{}, "POST:GetBusinessBonus"),
  38 + // beego.NSRouter("/business/edit", &controllers.BusinessBonusController{}, "POST:UpdateBusinessBonus"),
  39 + // beego.NSRouter("/business/list", &controllers.BusinessBonusController{}, "POST:ListBusinessBonus"),
40 ), 40 ),
41 beego.NSNamespace("/order", 41 beego.NSNamespace("/order",
42 beego.NSRouter("/purpose/list", &controllers.OrderInfoController{}, "POST:PageListOrderPurpose"), 42 beego.NSRouter("/purpose/list", &controllers.OrderInfoController{}, "POST:PageListOrderPurpose"),
  1 +~$*.xlsx
  2 +test/Test*.xlsx
  3 +*.out
  4 +*.test
  5 +.idea
  1 +language: go
  2 +
  3 +install:
  4 + - go get -d -t -v ./... && go build -v ./...
  5 +
  6 +go:
  7 + - 1.11.x
  8 + - 1.12.x
  9 + - 1.13.x
  10 + - 1.14.x
  11 + - 1.15.x
  12 +
  13 +os:
  14 + - linux
  15 + - osx
  16 +
  17 +env:
  18 + jobs:
  19 + - GOARCH=amd64
  20 + - GOARCH=386
  21 +
  22 +script:
  23 + - env GO111MODULE=on go vet ./...
  24 + - env GO111MODULE=on go test -v -race ./... -coverprofile=coverage.txt -covermode=atomic
  25 +
  26 +after_success:
  27 + - bash <(curl -s https://codecov.io/bash)
  1 +# Contributor Covenant Code of Conduct
  2 +
  3 +## Our Pledge
  4 +
  5 +In the interest of fostering an open and welcoming environment, we as contributors and maintainers pledge to making participation in our project and our community a harassment-free experience for everyone, regardless of age, body size, disability, ethnicity, gender identity and expression, level of experience, nationality, personal appearance, race, religion, or sexual identity and orientation.
  6 +
  7 +## Our Standards
  8 +
  9 +Examples of behavior that contributes to creating a positive environment include:
  10 +
  11 +* Using welcoming and inclusive language
  12 +* Being respectful of differing viewpoints and experiences
  13 +* Gracefully accepting constructive criticism
  14 +* Focusing on what is best for the community
  15 +* Showing empathy towards other community members
  16 +
  17 +Examples of unacceptable behavior by participants include:
  18 +
  19 +* The use of sexualized language or imagery and unwelcome sexual attention or advances
  20 +* Trolling, insulting/derogatory comments, and personal or political attacks
  21 +* Public or private harassment
  22 +* Publishing others' private information, such as a physical or electronic address, without explicit permission
  23 +* Other conduct which could reasonably be considered inappropriate in a professional setting
  24 +
  25 +## Our Responsibilities
  26 +
  27 +Project maintainers are responsible for clarifying the standards of acceptable behavior and are expected to take appropriate and fair corrective action in response to any instances of unacceptable behavior.
  28 +
  29 +Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, or to ban temporarily or permanently any contributor for other behaviors that they deem inappropriate, threatening, offensive, or harmful.
  30 +
  31 +## Scope
  32 +
  33 +This Code of Conduct applies both within project spaces and in public spaces when an individual is representing the project or its community. Examples of representing a project or community include using an official project e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event. Representation of a project may be further defined and clarified by project maintainers.
  34 +
  35 +## Enforcement
  36 +
  37 +Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by contacting the project team at [xuri.me](https://xuri.me). The project team will review and investigate all complaints, and will respond in a way that it deems appropriate to the circumstances. The project team is obligated to maintain confidentiality with regard to the reporter of an incident. Further details of specific enforcement policies may be posted separately.
  38 +
  39 +Project maintainers who do not follow or enforce the Code of Conduct in good faith may face temporary or permanent repercussions as determined by other members of the project's leadership.
  40 +
  41 +## Attribution
  42 +
  43 +This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0, available at [https://www.contributor-covenant.org/version/2/0/code_of_conduct][version]
  44 +
  45 +[homepage]: https://www.contributor-covenant.org
  46 +[version]: https://www.contributor-covenant.org/version/2/0/code_of_conduct
  1 +# Contributing to excelize
  2 +
  3 +Want to hack on excelize? Awesome! This page contains information about reporting issues as well as some tips and
  4 +guidelines useful to experienced open source contributors. Finally, make sure
  5 +you read our [community guidelines](#community-guidelines) before you
  6 +start participating.
  7 +
  8 +## Topics
  9 +
  10 +* [Reporting Security Issues](#reporting-security-issues)
  11 +* [Design and Cleanup Proposals](#design-and-cleanup-proposals)
  12 +* [Reporting Issues](#reporting-other-issues)
  13 +* [Quick Contribution Tips and Guidelines](#quick-contribution-tips-and-guidelines)
  14 +* [Community Guidelines](#community-guidelines)
  15 +
  16 +## Reporting security issues
  17 +
  18 +The excelize maintainers take security seriously. If you discover a security
  19 +issue, please bring it to their attention right away!
  20 +
  21 +Please **DO NOT** file a public issue, instead send your report privately to
  22 +[xuri.me](https://xuri.me).
  23 +
  24 +Security reports are greatly appreciated and we will publicly thank you for it.
  25 +We currently do not offer a paid security bounty program, but are not
  26 +ruling it out in the future.
  27 +
  28 +## Reporting other issues
  29 +
  30 +A great way to contribute to the project is to send a detailed report when you
  31 +encounter an issue. We always appreciate a well-written, thorough bug report,
  32 +and will thank you for it!
  33 +
  34 +Check that [our issue database](https://github.com/360EntSecGroup-Skylar/excelize/issues)
  35 +doesn't already include that problem or suggestion before submitting an issue.
  36 +If you find a match, you can use the "subscribe" button to get notified on
  37 +updates. Do *not* leave random "+1" or "I have this too" comments, as they
  38 +only clutter the discussion, and don't help resolving it. However, if you
  39 +have ways to reproduce the issue or have additional information that may help
  40 +resolving the issue, please leave a comment.
  41 +
  42 +When reporting issues, always include the output of `go env`.
  43 +
  44 +Also include the steps required to reproduce the problem if possible and
  45 +applicable. This information will help us review and fix your issue faster.
  46 +When sending lengthy log-files, consider posting them as a gist [https://gist.github.com](https://gist.github.com).
  47 +Don't forget to remove sensitive data from your logfiles before posting (you can
  48 +replace those parts with "REDACTED").
  49 +
  50 +## Quick contribution tips and guidelines
  51 +
  52 +This section gives the experienced contributor some tips and guidelines.
  53 +
  54 +### Pull requests are always welcome
  55 +
  56 +Not sure if that typo is worth a pull request? Found a bug and know how to fix
  57 +it? Do it! We will appreciate it. Any significant improvement should be
  58 +documented as [a GitHub issue](https://github.com/360EntSecGroup-Skylar/excelize/issues) before
  59 +anybody starts working on it.
  60 +
  61 +We are always thrilled to receive pull requests. We do our best to process them
  62 +quickly. If your pull request is not accepted on the first try,
  63 +don't get discouraged!
  64 +
  65 +### Design and cleanup proposals
  66 +
  67 +You can propose new designs for existing excelize features. You can also design
  68 +entirely new features. We really appreciate contributors who want to refactor or
  69 +otherwise cleanup our project.
  70 +
  71 +We try hard to keep excelize lean and focused. Excelize can't do everything for
  72 +everybody. This means that we might decide against incorporating a new feature.
  73 +However, there might be a way to implement that feature *on top of* excelize.
  74 +
  75 +### Conventions
  76 +
  77 +Fork the repository and make changes on your fork in a feature branch:
  78 +
  79 +* If it's a bug fix branch, name it XXXX-something where XXXX is the number of
  80 + the issue.
  81 +* If it's a feature branch, create an enhancement issue to announce
  82 + your intentions, and name it XXXX-something where XXXX is the number of the
  83 + issue.
  84 +
  85 +Submit unit tests for your changes. Go has a great test framework built in; use
  86 +it! Take a look at existing tests for inspiration. Run the full test on your branch before
  87 +submitting a pull request.
  88 +
  89 +Update the documentation when creating or modifying features. Test your
  90 +documentation changes for clarity, concision, and correctness, as well as a
  91 +clean documentation build.
  92 +
  93 +Write clean code. Universally formatted code promotes ease of writing, reading,
  94 +and maintenance. Always run `gofmt -s -w file.go` on each changed file before
  95 +committing your changes. Most editors have plug-ins that do this automatically.
  96 +
  97 +Pull request descriptions should be as clear as possible and include a reference
  98 +to all the issues that they address.
  99 +
  100 +### Successful Changes
  101 +
  102 +Before contributing large or high impact changes, make the effort to coordinate
  103 +with the maintainers of the project before submitting a pull request. This
  104 +prevents you from doing extra work that may or may not be merged.
  105 +
  106 +Large PRs that are just submitted without any prior communication are unlikely
  107 +to be successful.
  108 +
  109 +While pull requests are the methodology for submitting changes to code, changes
  110 +are much more likely to be accepted if they are accompanied by additional
  111 +engineering work. While we don't define this explicitly, most of these goals
  112 +are accomplished through communication of the design goals and subsequent
  113 +solutions. Often times, it helps to first state the problem before presenting
  114 +solutions.
  115 +
  116 +Typically, the best methods of accomplishing this are to submit an issue,
  117 +stating the problem. This issue can include a problem statement and a
  118 +checklist with requirements. If solutions are proposed, alternatives should be
  119 +listed and eliminated. Even if the criteria for elimination of a solution is
  120 +frivolous, say so.
  121 +
  122 +Larger changes typically work best with design documents. These are focused on
  123 +providing context to the design at the time the feature was conceived and can
  124 +inform future documentation contributions.
  125 +
  126 +### Commit Messages
  127 +
  128 +Commit messages must start with a capitalized and short summary
  129 +written in the imperative, followed by an optional, more detailed explanatory
  130 +text which is separated from the summary by an empty line.
  131 +
  132 +Commit messages should follow best practices, including explaining the context
  133 +of the problem and how it was solved, including in caveats or follow up changes
  134 +required. They should tell the story of the change and provide readers
  135 +understanding of what led to it.
  136 +
  137 +In practice, the best approach to maintaining a nice commit message is to
  138 +leverage a `git add -p` and `git commit --amend` to formulate a solid
  139 +changeset. This allows one to piece together a change, as information becomes
  140 +available.
  141 +
  142 +If you squash a series of commits, don't just submit that. Re-write the commit
  143 +message, as if the series of commits was a single stroke of brilliance.
  144 +
  145 +That said, there is no requirement to have a single commit for a PR, as long as
  146 +each commit tells the story. For example, if there is a feature that requires a
  147 +package, it might make sense to have the package in a separate commit then have
  148 +a subsequent commit that uses it.
  149 +
  150 +Remember, you're telling part of the story with the commit message. Don't make
  151 +your chapter weird.
  152 +
  153 +### Review
  154 +
  155 +Code review comments may be added to your pull request. Discuss, then make the
  156 +suggested modifications and push additional commits to your feature branch. Post
  157 +a comment after pushing. New commits show up in the pull request automatically,
  158 +but the reviewers are notified only when you comment.
  159 +
  160 +Pull requests must be cleanly rebased on top of master without multiple branches
  161 +mixed into the PR.
  162 +
  163 +**Git tip**: If your PR no longer merges cleanly, use `rebase master` in your
  164 +feature branch to update your pull request rather than `merge master`.
  165 +
  166 +Before you make a pull request, squash your commits into logical units of work
  167 +using `git rebase -i` and `git push -f`. A logical unit of work is a consistent
  168 +set of patches that should be reviewed together: for example, upgrading the
  169 +version of a vendored dependency and taking advantage of its now available new
  170 +feature constitute two separate units of work. Implementing a new function and
  171 +calling it in another file constitute a single logical unit of work. The very
  172 +high majority of submissions should have a single commit, so if in doubt: squash
  173 +down to one.
  174 +
  175 +After every commit, make sure the test passes. Include documentation
  176 +changes in the same pull request so that a revert would remove all traces of
  177 +the feature or fix.
  178 +
  179 +Include an issue reference like `Closes #XXXX` or `Fixes #XXXX` in commits that
  180 +close an issue. Including references automatically closes the issue on a merge.
  181 +
  182 +Please see the [Coding Style](#coding-style) for further guidelines.
  183 +
  184 +### Merge approval
  185 +
  186 +The excelize maintainers use LGTM (Looks Good To Me) in comments on the code review to
  187 +indicate acceptance.
  188 +
  189 +### Sign your work
  190 +
  191 +The sign-off is a simple line at the end of the explanation for the patch. Your
  192 +signature certifies that you wrote the patch or otherwise have the right to pass
  193 +it on as an open-source patch. The rules are pretty simple: if you can certify
  194 +the below (from [developercertificate.org](http://developercertificate.org/)):
  195 +
  196 +```text
  197 +Developer Certificate of Origin
  198 +Version 1.1
  199 +
  200 +Copyright (C) 2004, 2006 The Linux Foundation and its contributors.
  201 +1 Letterman Drive
  202 +Suite D4700
  203 +San Francisco, CA, 94129
  204 +
  205 +Everyone is permitted to copy and distribute verbatim copies of this
  206 +license document, but changing it is not allowed.
  207 +
  208 +Developer's Certificate of Origin 1.1
  209 +
  210 +By making a contribution to this project, I certify that:
  211 +
  212 +(a) The contribution was created in whole or in part by me and I
  213 + have the right to submit it under the open source license
  214 + indicated in the file; or
  215 +
  216 +(b) The contribution is based upon previous work that, to the best
  217 + of my knowledge, is covered under an appropriate open source
  218 + license and I have the right under that license to submit that
  219 + work with modifications, whether created in whole or in part
  220 + by me, under the same open source license (unless I am
  221 + permitted to submit under a different license), as indicated
  222 + in the file; or
  223 +
  224 +(c) The contribution was provided directly to me by some other
  225 + person who certified (a), (b) or (c) and I have not modified
  226 + it.
  227 +
  228 +(d) I understand and agree that this project and the contribution
  229 + are public and that a record of the contribution (including all
  230 + personal information I submit with it, including my sign-off) is
  231 + maintained indefinitely and may be redistributed consistent with
  232 + this project or the open source license(s) involved.
  233 +```
  234 +
  235 +Then you just add a line to every git commit message:
  236 +
  237 +```text
  238 +Signed-off-by: Ri Xu https://xuri.me
  239 +```
  240 +
  241 +Use your real name (sorry, no pseudonyms or anonymous contributions.)
  242 +
  243 +If you set your `user.name` and `user.email` git configs, you can sign your
  244 +commit automatically with `git commit -s`.
  245 +
  246 +### How can I become a maintainer
  247 +
  248 +First, all maintainers have 3 things
  249 +
  250 +* They share responsibility in the project's success.
  251 +* They have made a long-term, recurring time investment to improve the project.
  252 +* They spend that time doing whatever needs to be done, not necessarily what
  253 + is the most interesting or fun.
  254 +
  255 +Maintainers are often under-appreciated, because their work is harder to appreciate.
  256 +It's easy to appreciate a really cool and technically advanced feature. It's harder
  257 +to appreciate the absence of bugs, the slow but steady improvement in stability,
  258 +or the reliability of a release process. But those things distinguish a good
  259 +project from a great one.
  260 +
  261 +Don't forget: being a maintainer is a time investment. Make sure you
  262 +will have time to make yourself available. You don't have to be a
  263 +maintainer to make a difference on the project!
  264 +
  265 +If you want to become a meintainer, contact [xuri.me](https://xuri.me) and given a introduction of you.
  266 +
  267 +## Community guidelines
  268 +
  269 +We want to keep the community awesome, growing and collaborative. We need
  270 +your help to keep it that way. To help with this we've come up with some general
  271 +guidelines for the community as a whole:
  272 +
  273 +* Be nice: Be courteous, respectful and polite to fellow community members:
  274 + no regional, racial, gender, or other abuse will be tolerated. We like
  275 + nice people way better than mean ones!
  276 +
  277 +* Encourage diversity and participation: Make everyone in our community feel
  278 + welcome, regardless of their background and the extent of their
  279 + contributions, and do everything possible to encourage participation in
  280 + our community.
  281 +
  282 +* Keep it legal: Basically, don't get us in trouble. Share only content that
  283 + you own, do not share private or sensitive information, and don't break
  284 + the law.
  285 +
  286 +* Stay on topic: Make sure that you are posting to the correct channel and
  287 + avoid off-topic discussions. Remember when you update an issue or respond
  288 + to an email you are potentially sending to a large number of people. Please
  289 + consider this before you update. Also remember that nobody likes spam.
  290 +
  291 +* Don't send email to the maintainers: There's no need to send email to the
  292 + maintainers to ask them to investigate an issue or to take a look at a
  293 + pull request. Instead of sending an email, GitHub mentions should be
  294 + used to ping maintainers to review a pull request, a proposal or an
  295 + issue.
  296 +
  297 +### Guideline violations — 3 strikes method
  298 +
  299 +The point of this section is not to find opportunities to punish people, but we
  300 +do need a fair way to deal with people who are making our community suck.
  301 +
  302 +1. First occurrence: We'll give you a friendly, but public reminder that the
  303 + behavior is inappropriate according to our guidelines.
  304 +
  305 +2. Second occurrence: We will send you a private message with a warning that
  306 + any additional violations will result in removal from the community.
  307 +
  308 +3. Third occurrence: Depending on the violation, we may need to delete or ban
  309 + your account.
  310 +
  311 +**Notes:**
  312 +
  313 +* Obvious spammers are banned on first occurrence. If we don't do this, we'll
  314 + have spam all over the place.
  315 +
  316 +* Violations are forgiven after 6 months of good behavior, and we won't hold a
  317 + grudge.
  318 +
  319 +* People who commit minor infractions will get some education, rather than
  320 + hammering them in the 3 strikes process.
  321 +
  322 +* The rules apply equally to everyone in the community, no matter how much
  323 + you've contributed.
  324 +
  325 +* Extreme violations of a threatening, abusive, destructive or illegal nature
  326 + will be addressed immediately and are not subject to 3 strikes or forgiveness.
  327 +
  328 +* Contact [xuri.me](https://xuri.me) to report abuse or appeal violations. In the case of
  329 + appeals, we know that mistakes happen, and we'll work with you to come up with a
  330 + fair solution if there has been a misunderstanding.
  331 +
  332 +## Coding Style
  333 +
  334 +Unless explicitly stated, we follow all coding guidelines from the Go
  335 +community. While some of these standards may seem arbitrary, they somehow seem
  336 +to result in a solid, consistent codebase.
  337 +
  338 +It is possible that the code base does not currently comply with these
  339 +guidelines. We are not looking for a massive PR that fixes this, since that
  340 +goes against the spirit of the guidelines. All new contributions should make a
  341 +best effort to clean up and make the code base better than they left it.
  342 +Obviously, apply your best judgement. Remember, the goal here is to make the
  343 +code base easier for humans to navigate and understand. Always keep that in
  344 +mind when nudging others to comply.
  345 +
  346 +The rules:
  347 +
  348 +1. All code should be formatted with `gofmt -s`.
  349 +2. All code should pass the default levels of
  350 + [`golint`](https://github.com/golang/lint).
  351 +3. All code should follow the guidelines covered in [Effective
  352 + Go](http://golang.org/doc/effective_go.html) and [Go Code Review
  353 + Comments](https://github.com/golang/go/wiki/CodeReviewComments).
  354 +4. Comment the code. Tell us the why, the history and the context.
  355 +5. Document _all_ declarations and methods, even private ones. Declare
  356 + expectations, caveats and anything else that may be important. If a type
  357 + gets exported, having the comments already there will ensure it's ready.
  358 +6. Variable name length should be proportional to its context and no longer.
  359 + `noCommaALongVariableNameLikeThisIsNotMoreClearWhenASimpleCommentWouldDo`.
  360 + In practice, short methods will have short variable names and globals will
  361 + have longer names.
  362 +7. No underscores in package names. If you need a compound name, step back,
  363 + and re-examine why you need a compound name. If you still think you need a
  364 + compound name, lose the underscore.
  365 +8. No utils or helpers packages. If a function is not general enough to
  366 + warrant its own package, it has not been written generally enough to be a
  367 + part of a util package. Just leave it unexported and well-documented.
  368 +9. All tests should run with `go test` and outside tooling should not be
  369 + required. No, we don't need another unit testing framework. Assertion
  370 + packages are acceptable if they provide _real_ incremental value.
  371 +10. Even though we call these "rules" above, they are actually just
  372 + guidelines. Since you've read all the rules, you now know that.
  373 +
  374 +If you are having trouble getting into the mood of idiomatic Go, we recommend
  375 +reading through [Effective Go](https://golang.org/doc/effective_go.html). The
  376 +[Go Blog](https://blog.golang.org) is also a great resource. Drinking the
  377 +kool-aid is a lot easier than going thirsty.
  378 +
  379 +## Code Review Comments and Effective Go Guidelines
  380 +
  381 +[CodeLingo](https://codelingo.io) automatically checks every pull request against the following guidelines from [Effective Go](https://golang.org/doc/effective_go.html) and [Code Review Comments](https://github.com/golang/go/wiki/CodeReviewComments).
  382 +
  383 +### Package Comment
  384 +
  385 +Every package should have a package comment, a block comment preceding the package clause.
  386 +For multi-file packages, the package comment only needs to be present in one file, and any one will do.
  387 +The package comment should introduce the package and provide information relevant to the package as a
  388 +whole. It will appear first on the godoc page and should set up the detailed documentation that follows.
  389 +
  390 +### Single Method Interface Name
  391 +
  392 +By convention, one-method interfaces are named by the method name plus an -er suffix
  393 +or similar modification to construct an agent noun: Reader, Writer, Formatter, CloseNotifier etc.
  394 +
  395 +There are a number of such names and it's productive to honor them and the function names they capture.
  396 +Read, Write, Close, Flush, String and so on have canonical signatures and meanings. To avoid confusion,
  397 +don't give your method one of those names unless it has the same signature and meaning. Conversely,
  398 +if your type implements a method with the same meaning as a method on a well-known type, give it the
  399 +same name and signature; call your string-converter method String not ToString.
  400 +
  401 +### Avoid Annotations in Comments
  402 +
  403 +Comments do not need extra formatting such as banners of stars. The generated output
  404 +may not even be presented in a fixed-width font, so don't depend on spacing for alignment—godoc,
  405 +like gofmt, takes care of that. The comments are uninterpreted plain text, so HTML and other
  406 +annotations such as _this_ will reproduce verbatim and should not be used. One adjustment godoc
  407 +does do is to display indented text in a fixed-width font, suitable for program snippets.
  408 +The package comment for the fmt package uses this to good effect.
  409 +
  410 +### Comment First Word as Subject
  411 +
  412 +Doc comments work best as complete sentences, which allow a wide variety of automated presentations.
  413 +The first sentence should be a one-sentence summary that starts with the name being declared.
  414 +
  415 +### Good Package Name
  416 +
  417 +It's helpful if everyone using the package can use the same name
  418 +to refer to its contents, which implies that the package name should
  419 +be good: short, concise, evocative. By convention, packages are
  420 +given lower case, single-word names; there should be no need for
  421 +underscores or mixedCaps. Err on the side of brevity, since everyone
  422 +using your package will be typing that name. And don't worry about
  423 +collisions a priori. The package name is only the default name for
  424 +imports; it need not be unique across all source code, and in the
  425 +rare case of a collision the importing package can choose a different
  426 +name to use locally. In any case, confusion is rare because the file
  427 +name in the import determines just which package is being used.
  428 +
  429 +### Avoid Renaming Imports
  430 +
  431 +Avoid renaming imports except to avoid a name collision; good package names
  432 +should not require renaming. In the event of collision, prefer to rename the
  433 +most local or project-specific import.
  434 +
  435 +### Context as First Argument
  436 +
  437 +Values of the context.Context type carry security credentials, tracing information,
  438 +deadlines, and cancellation signals across API and process boundaries. Go programs
  439 +pass Contexts explicitly along the entire function call chain from incoming RPCs
  440 +and HTTP requests to outgoing requests.
  441 +
  442 +Most functions that use a Context should accept it as their first parameter.
  443 +
  444 +### Do Not Discard Errors
  445 +
  446 +Do not discard errors using _ variables. If a function returns an error,
  447 +check it to make sure the function succeeded. Handle the error, return it, or,
  448 +in truly exceptional situations, panic.
  449 +
  450 +### Go Error Format
  451 +
  452 +Error strings should not be capitalized (unless beginning with proper nouns
  453 +or acronyms) or end with punctuation, since they are usually printed following
  454 +other context. That is, use fmt.Errorf("something bad") not fmt.Errorf("Something bad"),
  455 +so that log.Printf("Reading %s: %v", filename, err) formats without a spurious
  456 +capital letter mid-message. This does not apply to logging, which is implicitly
  457 +line-oriented and not combined inside other messages.
  458 +
  459 +### Use Crypto Rand
  460 +
  461 +Do not use package math/rand to generate keys, even
  462 +throwaway ones. Unseeded, the generator is completely predictable.
  463 +Seeded with time.Nanoseconds(), there are just a few bits of entropy.
  464 +Instead, use crypto/rand's Reader, and if you need text, print to
  465 +hexadecimal or base64.
  1 +BSD 3-Clause License
  2 +
  3 +Copyright (c) 2016-2020 The excelize Authors.
  4 +All rights reserved.
  5 +
  6 +Redistribution and use in source and binary forms, with or without
  7 +modification, are permitted provided that the following conditions are met:
  8 +
  9 +* Redistributions of source code must retain the above copyright notice, this
  10 + list of conditions and the following disclaimer.
  11 +
  12 +* Redistributions in binary form must reproduce the above copyright notice,
  13 + this list of conditions and the following disclaimer in the documentation
  14 + and/or other materials provided with the distribution.
  15 +
  16 +* Neither the name of the copyright holder nor the names of its
  17 + contributors may be used to endorse or promote products derived from
  18 + this software without specific prior written permission.
  19 +
  20 +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  21 +AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  22 +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  23 +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  24 +FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  25 +DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  26 +SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  27 +CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  28 +OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29 +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  1 +# PR Details
  2 +
  3 +<!--- Provide a general summary of your changes in the Title above -->
  4 +
  5 +## Description
  6 +
  7 +<!--- Describe your changes in detail -->
  8 +
  9 +## Related Issue
  10 +
  11 +<!--- This project only accepts pull requests related to open issues -->
  12 +<!--- If suggesting a new feature or change, please discuss it in an issue first -->
  13 +<!--- If fixing a bug, there should be an issue describing it with steps to reproduce -->
  14 +<!--- Please link to the issue here: -->
  15 +
  16 +## Motivation and Context
  17 +
  18 +<!--- Why is this change required? What problem does it solve? -->
  19 +
  20 +## How Has This Been Tested
  21 +
  22 +<!--- Please describe in detail how you tested your changes. -->
  23 +<!--- Include details of your testing environment, and the tests you ran to -->
  24 +<!--- see how your change affects other areas of the code, etc. -->
  25 +
  26 +## Types of changes
  27 +
  28 +<!--- What types of changes does your code introduce? Put an `x` in all the boxes that apply: -->
  29 +
  30 +- [ ] Docs change / refactoring / dependency upgrade
  31 +- [ ] Bug fix (non-breaking change which fixes an issue)
  32 +- [ ] New feature (non-breaking change which adds functionality)
  33 +- [ ] Breaking change (fix or feature that would cause existing functionality to change)
  34 +
  35 +## Checklist
  36 +
  37 +<!--- Go over all the following points, and put an `x` in all the boxes that apply. -->
  38 +<!--- If you're unsure about any of these, don't hesitate to ask. We're here to help! -->
  39 +
  40 +- [ ] My code follows the code style of this project.
  41 +- [ ] My change requires a change to the documentation.
  42 +- [ ] I have updated the documentation accordingly.
  43 +- [ ] I have read the **CONTRIBUTING** document.
  44 +- [ ] I have added tests to cover my changes.
  45 +- [ ] All new and existing tests passed.
  1 +<p align="center"><img width="650" src="./excelize.svg" alt="Excelize logo"></p>
  2 +
  3 +<p align="center">
  4 + <a href="https://travis-ci.org/360EntSecGroup-Skylar/excelize"><img src="https://travis-ci.org/360EntSecGroup-Skylar/excelize.svg?branch=master" alt="Build Status"></a>
  5 + <a href="https://codecov.io/gh/360EntSecGroup-Skylar/excelize"><img src="https://codecov.io/gh/360EntSecGroup-Skylar/excelize/branch/master/graph/badge.svg" alt="Code Coverage"></a>
  6 + <a href="https://goreportcard.com/report/github.com/360EntSecGroup-Skylar/excelize"><img src="https://goreportcard.com/badge/github.com/360EntSecGroup-Skylar/excelize" alt="Go Report Card"></a>
  7 + <a href="https://pkg.go.dev/github.com/360EntSecGroup-Skylar/excelize/v2?tab=doc"><img src="https://img.shields.io/badge/go.dev-reference-007d9c?logo=go&logoColor=white" alt="go.dev"></a>
  8 + <a href="https://opensource.org/licenses/BSD-3-Clause"><img src="https://img.shields.io/badge/license-bsd-orange.svg" alt="Licenses"></a>
  9 + <a href="https://www.paypal.me/xuri"><img src="https://img.shields.io/badge/Donate-PayPal-green.svg" alt="Donate"></a>
  10 +</p>
  11 +
  12 +# Excelize
  13 +
  14 +## Introduction
  15 +
  16 +Excelize is a library written in pure Go providing a set of functions that allow you to write to and read from XLSX / XLSM / XLTM files. Supports reading and writing spreadsheet documents generated by Microsoft Excel&trade; 2007 and later. Supports complex components by high compatibility, and provided streaming API for generating or reading data from a worksheet with huge amounts of data. This library needs Go version 1.10 or later. The full API docs can be seen using go's built-in documentation tool, or online at [go.dev](https://pkg.go.dev/github.com/360EntSecGroup-Skylar/excelize/v2?tab=doc) and [docs reference](https://xuri.me/excelize/).
  17 +
  18 +## Basic Usage
  19 +
  20 +### Installation
  21 +
  22 +```bash
  23 +go get github.com/360EntSecGroup-Skylar/excelize
  24 +```
  25 +
  26 +- If your package management with [Go Modules](https://blog.golang.org/using-go-modules), please install with following command.
  27 +
  28 +```bash
  29 +go get github.com/360EntSecGroup-Skylar/excelize/v2
  30 +```
  31 +
  32 +### Create spreadsheet
  33 +
  34 +Here is a minimal example usage that will create spreadsheet file.
  35 +
  36 +```go
  37 +package main
  38 +
  39 +import (
  40 + "fmt"
  41 +
  42 + "github.com/360EntSecGroup-Skylar/excelize"
  43 +)
  44 +
  45 +func main() {
  46 + f := excelize.NewFile()
  47 + // Create a new sheet.
  48 + index := f.NewSheet("Sheet2")
  49 + // Set value of a cell.
  50 + f.SetCellValue("Sheet2", "A2", "Hello world.")
  51 + f.SetCellValue("Sheet1", "B2", 100)
  52 + // Set active sheet of the workbook.
  53 + f.SetActiveSheet(index)
  54 + // Save xlsx file by the given path.
  55 + if err := f.SaveAs("Book1.xlsx"); err != nil {
  56 + fmt.Println(err)
  57 + }
  58 +}
  59 +```
  60 +
  61 +### Reading spreadsheet
  62 +
  63 +The following constitutes the bare to read a spreadsheet document.
  64 +
  65 +```go
  66 +package main
  67 +
  68 +import (
  69 + "fmt"
  70 +
  71 + "github.com/360EntSecGroup-Skylar/excelize"
  72 +)
  73 +
  74 +func main() {
  75 + f, err := excelize.OpenFile("Book1.xlsx")
  76 + if err != nil {
  77 + fmt.Println(err)
  78 + return
  79 + }
  80 + // Get value from cell by given worksheet name and axis.
  81 + cell, err := f.GetCellValue("Sheet1", "B2")
  82 + if err != nil {
  83 + fmt.Println(err)
  84 + return
  85 + }
  86 + fmt.Println(cell)
  87 + // Get all the rows in the Sheet1.
  88 + rows, err := f.GetRows("Sheet1")
  89 + for _, row := range rows {
  90 + for _, colCell := range row {
  91 + fmt.Print(colCell, "\t")
  92 + }
  93 + fmt.Println()
  94 + }
  95 +}
  96 +```
  97 +
  98 +### Add chart to spreadsheet file
  99 +
  100 +With Excelize chart generation and management is as easy as a few lines of code. You can build charts based off data in your worksheet or generate charts without any data in your worksheet at all.
  101 +
  102 +<p align="center"><img width="650" src="./test/images/chart.png" alt="Excelize"></p>
  103 +
  104 +```go
  105 +package main
  106 +
  107 +import (
  108 + "fmt"
  109 +
  110 + "github.com/360EntSecGroup-Skylar/excelize"
  111 +)
  112 +
  113 +func main() {
  114 + categories := map[string]string{"A2": "Small", "A3": "Normal", "A4": "Large", "B1": "Apple", "C1": "Orange", "D1": "Pear"}
  115 + values := map[string]int{"B2": 2, "C2": 3, "D2": 3, "B3": 5, "C3": 2, "D3": 4, "B4": 6, "C4": 7, "D4": 8}
  116 + f := excelize.NewFile()
  117 + for k, v := range categories {
  118 + f.SetCellValue("Sheet1", k, v)
  119 + }
  120 + for k, v := range values {
  121 + f.SetCellValue("Sheet1", k, v)
  122 + }
  123 + if err := f.AddChart("Sheet1", "E1", `{"type":"col3DClustered","series":[{"name":"Sheet1!$A$2","categories":"Sheet1!$B$1:$D$1","values":"Sheet1!$B$2:$D$2"},{"name":"Sheet1!$A$3","categories":"Sheet1!$B$1:$D$1","values":"Sheet1!$B$3:$D$3"},{"name":"Sheet1!$A$4","categories":"Sheet1!$B$1:$D$1","values":"Sheet1!$B$4:$D$4"}],"title":{"name":"Fruit 3D Clustered Column Chart"}}`); err != nil {
  124 + fmt.Println(err)
  125 + return
  126 + }
  127 + // Save xlsx file by the given path.
  128 + if err := f.SaveAs("Book1.xlsx"); err != nil {
  129 + fmt.Println(err)
  130 + }
  131 +}
  132 +```
  133 +
  134 +### Add picture to spreadsheet file
  135 +
  136 +```go
  137 +package main
  138 +
  139 +import (
  140 + "fmt"
  141 + _ "image/gif"
  142 + _ "image/jpeg"
  143 + _ "image/png"
  144 +
  145 + "github.com/360EntSecGroup-Skylar/excelize"
  146 +)
  147 +
  148 +func main() {
  149 + f, err := excelize.OpenFile("Book1.xlsx")
  150 + if err != nil {
  151 + fmt.Println(err)
  152 + return
  153 + }
  154 + // Insert a picture.
  155 + if err := f.AddPicture("Sheet1", "A2", "image.png", ""); err != nil {
  156 + fmt.Println(err)
  157 + }
  158 + // Insert a picture to worksheet with scaling.
  159 + if err := f.AddPicture("Sheet1", "D2", "image.jpg", `{"x_scale": 0.5, "y_scale": 0.5}`); err != nil {
  160 + fmt.Println(err)
  161 + }
  162 + // Insert a picture offset in the cell with printing support.
  163 + if err := f.AddPicture("Sheet1", "H2", "image.gif", `{"x_offset": 15, "y_offset": 10, "print_obj": true, "lock_aspect_ratio": false, "locked": false}`); err != nil {
  164 + fmt.Println(err)
  165 + }
  166 + // Save the xlsx file with the origin path.
  167 + if err = f.Save(); err != nil {
  168 + fmt.Println(err)
  169 + }
  170 +}
  171 +```
  172 +
  173 +## Contributing
  174 +
  175 +Contributions are welcome! Open a pull request to fix a bug, or open an issue to discuss a new feature or change. XML is compliant with [part 1 of the 5th edition of the ECMA-376 Standard for Office Open XML](http://www.ecma-international.org/publications/standards/Ecma-376.htm).
  176 +
  177 +## Licenses
  178 +
  179 +This program is under the terms of the BSD 3-Clause License. See [https://opensource.org/licenses/BSD-3-Clause](https://opensource.org/licenses/BSD-3-Clause).
  180 +
  181 +The Excel logo is a trademark of [Microsoft Corporation](https://aka.ms/trademarks-usage). This artwork is an adaptation.
  182 +
  183 +gopher.{ai,svg,png} was created by [Takuya Ueda](https://twitter.com/tenntenn). Licensed under the [Creative Commons 3.0 Attributions license](http://creativecommons.org/licenses/by/3.0/).
  1 +<p align="center"><img width="650" src="./excelize.svg" alt="Excelize logo"></p>
  2 +
  3 +<p align="center">
  4 + <a href="https://travis-ci.org/360EntSecGroup-Skylar/excelize"><img src="https://travis-ci.org/360EntSecGroup-Skylar/excelize.svg?branch=master" alt="Build Status"></a>
  5 + <a href="https://codecov.io/gh/360EntSecGroup-Skylar/excelize"><img src="https://codecov.io/gh/360EntSecGroup-Skylar/excelize/branch/master/graph/badge.svg" alt="Code Coverage"></a>
  6 + <a href="https://goreportcard.com/report/github.com/360EntSecGroup-Skylar/excelize"><img src="https://goreportcard.com/badge/github.com/360EntSecGroup-Skylar/excelize" alt="Go Report Card"></a>
  7 + <a href="https://pkg.go.dev/github.com/360EntSecGroup-Skylar/excelize/v2?tab=doc"><img src="https://img.shields.io/badge/go.dev-reference-007d9c?logo=go&logoColor=white" alt="go.dev"></a>
  8 + <a href="https://opensource.org/licenses/BSD-3-Clause"><img src="https://img.shields.io/badge/license-bsd-orange.svg" alt="Licenses"></a>
  9 + <a href="https://www.paypal.me/xuri"><img src="https://img.shields.io/badge/Donate-PayPal-green.svg" alt="Donate"></a>
  10 +</p>
  11 +
  12 +# Excelize
  13 +
  14 +## 简介
  15 +
  16 +Excelize 是 Go 语言编写的用于操作 Office Excel 文档基础库,基于 ECMA-376,ISO/IEC 29500 国际标准。可以使用它来读取、写入由 Microsoft Excel&trade; 2007 及以上版本创建的电子表格文档。支持 XLSX / XLSM / XLTM 等多种文档格式,高度兼容带有样式、图片(表)、透视表、切片器等复杂组件的文档,并提供流式读写 API,用于处理包含大规模数据的工作簿。可应用于各类报表平台、云计算、边缘计算等系统。使用本类库要求使用的 Go 语言为 1.10 或更高版本,完整的 API 使用文档请访问 [go.dev](https://pkg.go.dev/github.com/360EntSecGroup-Skylar/excelize/v2?tab=doc) 或查看 [参考文档](https://xuri.me/excelize/)
  17 +
  18 +## 快速上手
  19 +
  20 +### 安装
  21 +
  22 +```bash
  23 +go get github.com/360EntSecGroup-Skylar/excelize
  24 +```
  25 +
  26 +- 如果您使用 [Go Modules](https://blog.golang.org/using-go-modules) 管理软件包,请使用下面的命令来安装最新版本。
  27 +
  28 +```bash
  29 +go get github.com/360EntSecGroup-Skylar/excelize/v2
  30 +```
  31 +
  32 +### 创建 Excel 文档
  33 +
  34 +下面是一个创建 Excel 文档的简单例子:
  35 +
  36 +```go
  37 +package main
  38 +
  39 +import (
  40 + "fmt"
  41 +
  42 + "github.com/360EntSecGroup-Skylar/excelize"
  43 +)
  44 +
  45 +func main() {
  46 + f := excelize.NewFile()
  47 + // 创建一个工作表
  48 + index := f.NewSheet("Sheet2")
  49 + // 设置单元格的值
  50 + f.SetCellValue("Sheet2", "A2", "Hello world.")
  51 + f.SetCellValue("Sheet1", "B2", 100)
  52 + // 设置工作簿的默认工作表
  53 + f.SetActiveSheet(index)
  54 + // 根据指定路径保存文件
  55 + if err := f.SaveAs("Book1.xlsx"); err != nil {
  56 + fmt.Println(err)
  57 + }
  58 +}
  59 +```
  60 +
  61 +### 读取 Excel 文档
  62 +
  63 +下面是读取 Excel 文档的例子:
  64 +
  65 +```go
  66 +package main
  67 +
  68 +import (
  69 + "fmt"
  70 +
  71 + "github.com/360EntSecGroup-Skylar/excelize"
  72 +)
  73 +
  74 +func main() {
  75 + f, err := excelize.OpenFile("Book1.xlsx")
  76 + if err != nil {
  77 + fmt.Println(err)
  78 + return
  79 + }
  80 + // 获取工作表中指定单元格的值
  81 + cell, err := f.GetCellValue("Sheet1", "B2")
  82 + if err != nil {
  83 + fmt.Println(err)
  84 + return
  85 + }
  86 + fmt.Println(cell)
  87 + // 获取 Sheet1 上所有单元格
  88 + rows, err := f.GetRows("Sheet1")
  89 + for _, row := range rows {
  90 + for _, colCell := range row {
  91 + fmt.Print(colCell, "\t")
  92 + }
  93 + fmt.Println()
  94 + }
  95 +}
  96 +```
  97 +
  98 +### 在 Excel 文档中创建图表
  99 +
  100 +使用 Excelize 生成图表十分简单,仅需几行代码。您可以根据工作表中的已有数据构建图表,或向工作表中添加数据并创建图表。
  101 +
  102 +<p align="center"><img width="650" src="./test/images/chart.png" alt="Excelize"></p>
  103 +
  104 +```go
  105 +package main
  106 +
  107 +import (
  108 + "fmt"
  109 +
  110 + "github.com/360EntSecGroup-Skylar/excelize"
  111 +)
  112 +
  113 +func main() {
  114 + categories := map[string]string{"A2": "Small", "A3": "Normal", "A4": "Large", "B1": "Apple", "C1": "Orange", "D1": "Pear"}
  115 + values := map[string]int{"B2": 2, "C2": 3, "D2": 3, "B3": 5, "C3": 2, "D3": 4, "B4": 6, "C4": 7, "D4": 8}
  116 + f := excelize.NewFile()
  117 + for k, v := range categories {
  118 + f.SetCellValue("Sheet1", k, v)
  119 + }
  120 + for k, v := range values {
  121 + f.SetCellValue("Sheet1", k, v)
  122 + }
  123 + if err := f.AddChart("Sheet1", "E1", `{"type":"col3DClustered","series":[{"name":"Sheet1!$A$2","categories":"Sheet1!$B$1:$D$1","values":"Sheet1!$B$2:$D$2"},{"name":"Sheet1!$A$3","categories":"Sheet1!$B$1:$D$1","values":"Sheet1!$B$3:$D$3"},{"name":"Sheet1!$A$4","categories":"Sheet1!$B$1:$D$1","values":"Sheet1!$B$4:$D$4"}],"title":{"name":"Fruit 3D Clustered Column Chart"}}`); err != nil {
  124 + fmt.Println(err)
  125 + return
  126 + }
  127 + // 根据指定路径保存文件
  128 + if err := f.SaveAs("Book1.xlsx"); err != nil {
  129 + fmt.Println(err)
  130 + }
  131 +}
  132 +```
  133 +
  134 +### 向 Excel 文档中插入图片
  135 +
  136 +```go
  137 +package main
  138 +
  139 +import (
  140 + "fmt"
  141 + _ "image/gif"
  142 + _ "image/jpeg"
  143 + _ "image/png"
  144 +
  145 + "github.com/360EntSecGroup-Skylar/excelize"
  146 +)
  147 +
  148 +func main() {
  149 + f, err := excelize.OpenFile("Book1.xlsx")
  150 + if err != nil {
  151 + fmt.Println(err)
  152 + return
  153 + }
  154 + // 插入图片
  155 + if err := f.AddPicture("Sheet1", "A2", "image.png", ""); err != nil {
  156 + fmt.Println(err)
  157 + }
  158 + // 在工作表中插入图片,并设置图片的缩放比例
  159 + if err := f.AddPicture("Sheet1", "D2", "image.jpg", `{"x_scale": 0.5, "y_scale": 0.5}`); err != nil {
  160 + fmt.Println(err)
  161 + }
  162 + // 在工作表中插入图片,并设置图片的打印属性
  163 + if err := f.AddPicture("Sheet1", "H2", "image.gif", `{"x_offset": 15, "y_offset": 10, "print_obj": true, "lock_aspect_ratio": false, "locked": false}`); err != nil {
  164 + fmt.Println(err)
  165 + }
  166 + // 保存文件
  167 + if err = f.Save(); err != nil {
  168 + fmt.Println(err)
  169 + }
  170 +}
  171 +```
  172 +
  173 +## 社区合作
  174 +
  175 +欢迎您为此项目贡献代码,提出建议或问题、修复 Bug 以及参与讨论对新功能的想法。 XML 符合标准: [part 1 of the 5th edition of the ECMA-376 Standard for Office Open XML](http://www.ecma-international.org/publications/standards/Ecma-376.htm)
  176 +
  177 +## 开源许可
  178 +
  179 +本项目遵循 BSD 3-Clause 开源许可协议,访问 [https://opensource.org/licenses/BSD-3-Clause](https://opensource.org/licenses/BSD-3-Clause) 查看许可协议文件。
  180 +
  181 +Excel 徽标是 [Microsoft Corporation](https://aka.ms/trademarks-usage) 的商标,项目的图片是一种改编。
  182 +
  183 +gopher.{ai,svg,png} 由 [Takuya Ueda](https://twitter.com/tenntenn) 创作,遵循 [Creative Commons 3.0 Attributions license](http://creativecommons.org/licenses/by/3.0/) 创作共用授权条款。
  1 +# Security Policy
  2 +
  3 +## Supported Versions
  4 +
  5 +We will dive into any security-related issue as long as your Excelize version is still supported by us. When reporting an issue, include as much information as possible, but no need to fill fancy forms or answer tedious questions. Just tell us what you found, how to reproduce it, and any concerns you have about it. We will respond as soon as possible and follow up with any missing information.
  6 +
  7 +## Reporting a Vulnerability
  8 +
  9 +Please e-mail us directly at `xuri.me@gmail.com` or use the security issue template on GitHub. In general, public disclosure is made after the issue has been fully identified and a patch is ready to be released. A security issue gets the highest priority assigned and a reply regarding the vulnerability is given within a typical 24 hours. Thank you!
  1 +// Copyright 2016 - 2020 The excelize Authors. All rights reserved. Use of
  2 +// this source code is governed by a BSD-style license that can be found in
  3 +// the LICENSE file.
  4 +//
  5 +// Package excelize providing a set of functions that allow you to write to
  6 +// and read from XLSX / XLSM / XLTM files. Supports reading and writing
  7 +// spreadsheet documents generated by Microsoft Exce™ 2007 and later. Supports
  8 +// complex components by high compatibility, and provided streaming API for
  9 +// generating or reading data from a worksheet with huge amounts of data. This
  10 +// library needs Go version 1.10 or later.
  11 +
  12 +package excelize
  13 +
  14 +import (
  15 + "errors"
  16 + "strings"
  17 +)
  18 +
  19 +type adjustDirection bool
  20 +
  21 +const (
  22 + columns adjustDirection = false
  23 + rows adjustDirection = true
  24 +)
  25 +
  26 +// adjustHelper provides a function to adjust rows and columns dimensions,
  27 +// hyperlinks, merged cells and auto filter when inserting or deleting rows or
  28 +// columns.
  29 +//
  30 +// sheet: Worksheet name that we're editing
  31 +// column: Index number of the column we're inserting/deleting before
  32 +// row: Index number of the row we're inserting/deleting before
  33 +// offset: Number of rows/column to insert/delete negative values indicate deletion
  34 +//
  35 +// TODO: adjustPageBreaks, adjustComments, adjustDataValidations, adjustProtectedCells
  36 +//
  37 +func (f *File) adjustHelper(sheet string, dir adjustDirection, num, offset int) error {
  38 + xlsx, err := f.workSheetReader(sheet)
  39 + if err != nil {
  40 + return err
  41 + }
  42 + if dir == rows {
  43 + f.adjustRowDimensions(xlsx, num, offset)
  44 + } else {
  45 + f.adjustColDimensions(xlsx, num, offset)
  46 + }
  47 + f.adjustHyperlinks(xlsx, sheet, dir, num, offset)
  48 + if err = f.adjustMergeCells(xlsx, dir, num, offset); err != nil {
  49 + return err
  50 + }
  51 + if err = f.adjustAutoFilter(xlsx, dir, num, offset); err != nil {
  52 + return err
  53 + }
  54 + if err = f.adjustCalcChain(dir, num, offset); err != nil {
  55 + return err
  56 + }
  57 + checkSheet(xlsx)
  58 + _ = checkRow(xlsx)
  59 +
  60 + if xlsx.MergeCells != nil && len(xlsx.MergeCells.Cells) == 0 {
  61 + xlsx.MergeCells = nil
  62 + }
  63 +
  64 + return nil
  65 +}
  66 +
  67 +// adjustColDimensions provides a function to update column dimensions when
  68 +// inserting or deleting rows or columns.
  69 +func (f *File) adjustColDimensions(xlsx *xlsxWorksheet, col, offset int) {
  70 + for rowIdx := range xlsx.SheetData.Row {
  71 + for colIdx, v := range xlsx.SheetData.Row[rowIdx].C {
  72 + cellCol, cellRow, _ := CellNameToCoordinates(v.R)
  73 + if col <= cellCol {
  74 + if newCol := cellCol + offset; newCol > 0 {
  75 + xlsx.SheetData.Row[rowIdx].C[colIdx].R, _ = CoordinatesToCellName(newCol, cellRow)
  76 + }
  77 + }
  78 + }
  79 + }
  80 +}
  81 +
  82 +// adjustRowDimensions provides a function to update row dimensions when
  83 +// inserting or deleting rows or columns.
  84 +func (f *File) adjustRowDimensions(xlsx *xlsxWorksheet, row, offset int) {
  85 + for i := range xlsx.SheetData.Row {
  86 + r := &xlsx.SheetData.Row[i]
  87 + if newRow := r.R + offset; r.R >= row && newRow > 0 {
  88 + f.ajustSingleRowDimensions(r, newRow)
  89 + }
  90 + }
  91 +}
  92 +
  93 +// ajustSingleRowDimensions provides a function to ajust single row dimensions.
  94 +func (f *File) ajustSingleRowDimensions(r *xlsxRow, num int) {
  95 + r.R = num
  96 + for i, col := range r.C {
  97 + colName, _, _ := SplitCellName(col.R)
  98 + r.C[i].R, _ = JoinCellName(colName, num)
  99 + }
  100 +}
  101 +
  102 +// adjustHyperlinks provides a function to update hyperlinks when inserting or
  103 +// deleting rows or columns.
  104 +func (f *File) adjustHyperlinks(xlsx *xlsxWorksheet, sheet string, dir adjustDirection, num, offset int) {
  105 + // short path
  106 + if xlsx.Hyperlinks == nil || len(xlsx.Hyperlinks.Hyperlink) == 0 {
  107 + return
  108 + }
  109 +
  110 + // order is important
  111 + if offset < 0 {
  112 + for i := len(xlsx.Hyperlinks.Hyperlink) - 1; i >= 0; i-- {
  113 + linkData := xlsx.Hyperlinks.Hyperlink[i]
  114 + colNum, rowNum, _ := CellNameToCoordinates(linkData.Ref)
  115 +
  116 + if (dir == rows && num == rowNum) || (dir == columns && num == colNum) {
  117 + f.deleteSheetRelationships(sheet, linkData.RID)
  118 + if len(xlsx.Hyperlinks.Hyperlink) > 1 {
  119 + xlsx.Hyperlinks.Hyperlink = append(xlsx.Hyperlinks.Hyperlink[:i],
  120 + xlsx.Hyperlinks.Hyperlink[i+1:]...)
  121 + } else {
  122 + xlsx.Hyperlinks = nil
  123 + }
  124 + }
  125 + }
  126 + }
  127 +
  128 + if xlsx.Hyperlinks == nil {
  129 + return
  130 + }
  131 +
  132 + for i := range xlsx.Hyperlinks.Hyperlink {
  133 + link := &xlsx.Hyperlinks.Hyperlink[i] // get reference
  134 + colNum, rowNum, _ := CellNameToCoordinates(link.Ref)
  135 +
  136 + if dir == rows {
  137 + if rowNum >= num {
  138 + link.Ref, _ = CoordinatesToCellName(colNum, rowNum+offset)
  139 + }
  140 + } else {
  141 + if colNum >= num {
  142 + link.Ref, _ = CoordinatesToCellName(colNum+offset, rowNum)
  143 + }
  144 + }
  145 + }
  146 +}
  147 +
  148 +// adjustAutoFilter provides a function to update the auto filter when
  149 +// inserting or deleting rows or columns.
  150 +func (f *File) adjustAutoFilter(xlsx *xlsxWorksheet, dir adjustDirection, num, offset int) error {
  151 + if xlsx.AutoFilter == nil {
  152 + return nil
  153 + }
  154 +
  155 + coordinates, err := f.areaRefToCoordinates(xlsx.AutoFilter.Ref)
  156 + if err != nil {
  157 + return err
  158 + }
  159 + x1, y1, x2, y2 := coordinates[0], coordinates[1], coordinates[2], coordinates[3]
  160 +
  161 + if (dir == rows && y1 == num && offset < 0) || (dir == columns && x1 == num && x2 == num) {
  162 + xlsx.AutoFilter = nil
  163 + for rowIdx := range xlsx.SheetData.Row {
  164 + rowData := &xlsx.SheetData.Row[rowIdx]
  165 + if rowData.R > y1 && rowData.R <= y2 {
  166 + rowData.Hidden = false
  167 + }
  168 + }
  169 + return nil
  170 + }
  171 +
  172 + coordinates = f.adjustAutoFilterHelper(dir, coordinates, num, offset)
  173 + x1, y1, x2, y2 = coordinates[0], coordinates[1], coordinates[2], coordinates[3]
  174 +
  175 + if xlsx.AutoFilter.Ref, err = f.coordinatesToAreaRef([]int{x1, y1, x2, y2}); err != nil {
  176 + return err
  177 + }
  178 + return nil
  179 +}
  180 +
  181 +// adjustAutoFilterHelper provides a function for adjusting auto filter to
  182 +// compare and calculate cell axis by the given adjust direction, operation
  183 +// axis and offset.
  184 +func (f *File) adjustAutoFilterHelper(dir adjustDirection, coordinates []int, num, offset int) []int {
  185 + if dir == rows {
  186 + if coordinates[1] >= num {
  187 + coordinates[1] += offset
  188 + }
  189 + if coordinates[3] >= num {
  190 + coordinates[3] += offset
  191 + }
  192 + } else {
  193 + if coordinates[2] >= num {
  194 + coordinates[2] += offset
  195 + }
  196 + }
  197 + return coordinates
  198 +}
  199 +
  200 +// areaRefToCoordinates provides a function to convert area reference to a
  201 +// pair of coordinates.
  202 +func (f *File) areaRefToCoordinates(ref string) ([]int, error) {
  203 + rng := strings.Split(ref, ":")
  204 + return areaRangeToCoordinates(rng[0], rng[1])
  205 +}
  206 +
  207 +// areaRangeToCoordinates provides a function to convert cell range to a
  208 +// pair of coordinates.
  209 +func areaRangeToCoordinates(firstCell, lastCell string) ([]int, error) {
  210 + coordinates := make([]int, 4)
  211 + var err error
  212 + coordinates[0], coordinates[1], err = CellNameToCoordinates(firstCell)
  213 + if err != nil {
  214 + return coordinates, err
  215 + }
  216 + coordinates[2], coordinates[3], err = CellNameToCoordinates(lastCell)
  217 + return coordinates, err
  218 +}
  219 +
  220 +// sortCoordinates provides a function to correct the coordinate area, such
  221 +// correct C1:B3 to B1:C3.
  222 +func sortCoordinates(coordinates []int) error {
  223 + if len(coordinates) != 4 {
  224 + return errors.New("coordinates length must be 4")
  225 + }
  226 + if coordinates[2] < coordinates[0] {
  227 + coordinates[2], coordinates[0] = coordinates[0], coordinates[2]
  228 + }
  229 + if coordinates[3] < coordinates[1] {
  230 + coordinates[3], coordinates[1] = coordinates[1], coordinates[3]
  231 + }
  232 + return nil
  233 +}
  234 +
  235 +// coordinatesToAreaRef provides a function to convert a pair of coordinates
  236 +// to area reference.
  237 +func (f *File) coordinatesToAreaRef(coordinates []int) (string, error) {
  238 + if len(coordinates) != 4 {
  239 + return "", errors.New("coordinates length must be 4")
  240 + }
  241 + firstCell, err := CoordinatesToCellName(coordinates[0], coordinates[1])
  242 + if err != nil {
  243 + return "", err
  244 + }
  245 + lastCell, err := CoordinatesToCellName(coordinates[2], coordinates[3])
  246 + if err != nil {
  247 + return "", err
  248 + }
  249 + return firstCell + ":" + lastCell, err
  250 +}
  251 +
  252 +// adjustMergeCells provides a function to update merged cells when inserting
  253 +// or deleting rows or columns.
  254 +func (f *File) adjustMergeCells(xlsx *xlsxWorksheet, dir adjustDirection, num, offset int) error {
  255 + if xlsx.MergeCells == nil {
  256 + return nil
  257 + }
  258 +
  259 + for i := 0; i < len(xlsx.MergeCells.Cells); i++ {
  260 + areaData := xlsx.MergeCells.Cells[i]
  261 + coordinates, err := f.areaRefToCoordinates(areaData.Ref)
  262 + if err != nil {
  263 + return err
  264 + }
  265 + x1, y1, x2, y2 := coordinates[0], coordinates[1], coordinates[2], coordinates[3]
  266 + if dir == rows {
  267 + if y1 == num && y2 == num && offset < 0 {
  268 + f.deleteMergeCell(xlsx, i)
  269 + i--
  270 + }
  271 + y1 = f.adjustMergeCellsHelper(y1, num, offset)
  272 + y2 = f.adjustMergeCellsHelper(y2, num, offset)
  273 + } else {
  274 + if x1 == num && x2 == num && offset < 0 {
  275 + f.deleteMergeCell(xlsx, i)
  276 + i--
  277 + }
  278 + x1 = f.adjustMergeCellsHelper(x1, num, offset)
  279 + x2 = f.adjustMergeCellsHelper(x2, num, offset)
  280 + }
  281 + if x1 == x2 && y1 == y2 {
  282 + f.deleteMergeCell(xlsx, i)
  283 + i--
  284 + }
  285 + if areaData.Ref, err = f.coordinatesToAreaRef([]int{x1, y1, x2, y2}); err != nil {
  286 + return err
  287 + }
  288 + }
  289 + return nil
  290 +}
  291 +
  292 +// adjustMergeCellsHelper provides a function for adjusting merge cells to
  293 +// compare and calculate cell axis by the given pivot, operation axis and
  294 +// offset.
  295 +func (f *File) adjustMergeCellsHelper(pivot, num, offset int) int {
  296 + if pivot >= num {
  297 + pivot += offset
  298 + if pivot < 1 {
  299 + return 1
  300 + }
  301 + return pivot
  302 + }
  303 + return pivot
  304 +}
  305 +
  306 +// deleteMergeCell provides a function to delete merged cell by given index.
  307 +func (f *File) deleteMergeCell(sheet *xlsxWorksheet, idx int) {
  308 + if len(sheet.MergeCells.Cells) > idx {
  309 + sheet.MergeCells.Cells = append(sheet.MergeCells.Cells[:idx], sheet.MergeCells.Cells[idx+1:]...)
  310 + sheet.MergeCells.Count = len(sheet.MergeCells.Cells)
  311 + }
  312 +}
  313 +
  314 +// adjustCalcChain provides a function to update the calculation chain when
  315 +// inserting or deleting rows or columns.
  316 +func (f *File) adjustCalcChain(dir adjustDirection, num, offset int) error {
  317 + if f.CalcChain == nil {
  318 + return nil
  319 + }
  320 + for index, c := range f.CalcChain.C {
  321 + colNum, rowNum, err := CellNameToCoordinates(c.R)
  322 + if err != nil {
  323 + return err
  324 + }
  325 + if dir == rows && num <= rowNum {
  326 + if newRow := rowNum + offset; newRow > 0 {
  327 + f.CalcChain.C[index].R, _ = CoordinatesToCellName(colNum, newRow)
  328 + }
  329 + }
  330 + if dir == columns && num <= colNum {
  331 + if newCol := colNum + offset; newCol > 0 {
  332 + f.CalcChain.C[index].R, _ = CoordinatesToCellName(newCol, rowNum)
  333 + }
  334 + }
  335 + }
  336 + return nil
  337 +}