瀏覽代碼

update:set row height

animeic 1 年之前
父節點
當前提交
dcc69eb86a
共有 3 個文件被更改,包括 74 次插入14 次删除
  1. 48 7
      boxcost/api/bill-product-excel.go
  2. 9 7
      boxcost/api/print.go
  3. 17 0
      boxcost/api/utils.go

+ 48 - 7
boxcost/api/bill-product-excel.go

@@ -4,6 +4,7 @@ import (
 	"box-cost/db/model"
 	"fmt"
 	"regexp"
+	"strings"
 
 	_ "image/gif"
 	_ "image/jpeg"
@@ -28,7 +29,40 @@ type ProductBillExcel struct {
 
 	Signatures []*model.Signature
 
-	IsPdf string
+	IsPdf           string
+	RowMap          map[string]int
+	RowWidthArray   []float64
+	RowsHeightArray []map[int]float64
+}
+
+// 批量设置行高
+func (b *ProductBillExcel) setRowsHeight() {
+	for _, rowHeight := range b.RowsHeightArray {
+		for row, height := range rowHeight {
+			b.Excel.SetRowHeight(b.SheetName, row, height)
+		}
+	}
+
+}
+
+// 获取范围内单元格的宽度 A:F
+func (b *ProductBillExcel) getRangeWidth(r string) float64 {
+	rg := strings.Split(r, ":")
+
+	if len(rg) == 1 {
+		start := b.RowMap[rg[0]]
+		return b.RowWidthArray[start]
+	} else if len(rg) == 2 {
+		start := b.RowMap[rg[0]]
+		end := b.RowMap[rg[1]]
+		rowr := b.RowWidthArray[start : end+1]
+		width := 0.0
+		for _, v := range rowr {
+			width += v
+		}
+		return width
+	}
+	return 0.0
 }
 
 func (b *ProductBillExcel) drawTitle() error {
@@ -120,6 +154,7 @@ func (b *ProductBillExcel) drawSubTitles() error {
 			return err
 		}
 		b.Excel.SetCellValue(b.SheetName, left1Cell, value)
+
 		return nil
 	}
 
@@ -129,10 +164,13 @@ func (b *ProductBillExcel) drawSubTitles() error {
 	b.Excel.SetRowHeight(b.SheetName, b.Row, 21)
 
 	//第二行
-	drawLeft(b.Row+1, "供应商名称:"+b.Content.Supplier)
+	supplierContent := fmt.Sprintf("供应商名称:%s", b.Content.Supplier)
+	drawLeft(b.Row+1, supplierContent)
+	// 设置行高
+	b.RowsHeightArray = append(b.RowsHeightArray, map[int]float64{b.Row + 1: getRowHeight(supplierContent, b.getRangeWidth("A:F"), 21)})
+
 	timeformat := b.Content.CreateTime.Local().Format("2006年01月02号 15:04:05")
 	drawRight(b.Row+1, "下单时间:"+timeformat)
-	b.Excel.SetRowHeight(b.SheetName, b.Row+1, 21)
 
 	//第三行
 	drawLeft(b.Row+2, "产品名称:"+b.Content.ProductName)
@@ -147,6 +185,8 @@ func (b *ProductBillExcel) drawSubTitles() error {
 	b.Excel.SetRowHeight(b.SheetName, b.Row+2, 21)
 	// 第四行
 	drawLall(b.Row+3, "包含工序:"+b.Content.CompProduceName)
+	// 设置行高
+	b.RowsHeightArray = append(b.RowsHeightArray, map[int]float64{b.Row + 3: getRowHeight("包含工序:"+b.Content.CompProduceName, b.getRangeWidth("A:F"), 21)})
 
 	return nil
 }
@@ -386,6 +426,8 @@ func (b *ProductBillExcel) Draws() {
 	}
 	b.drawRemark()
 	b.drawTableSignature()
+	// 设置行高
+	b.setRowsHeight()
 }
 
 func NewProductBill(f *excelize.File) *ProductBillExcel {
@@ -410,12 +452,11 @@ func NewProductBill(f *excelize.File) *ProductBillExcel {
 		Offset:           0,
 		AlignCenterStyle: styleLeft,
 		Signatures:       make([]*model.Signature, 0),
+		RowMap:           map[string]int{"A": 0, "B": 1, "C": 2, "D": 3, "E": 4, "F": 5, "G": 6, "H": 7, "I": 8},
+		RowWidthArray:    []float64{16, 12, 12, 12, 12, 12, 12, 12, 18},
+		RowsHeightArray:  make([]map[int]float64, 0),
 	}
 
-	// f.SetColWidth(b.SheetName, "A", "A", 17)
-	// f.SetColWidth(b.SheetName, "B", "B", 12)
-	// f.SetColWidth(b.SheetName, "C", "H", 10.5)
-	// f.SetColWidth(b.SheetName, "I", "J", 12)
 	f.SetPageMargins(b.SheetName, excelize.PageMarginTop(1), excelize.PageMarginLeft(0.25), excelize.PageMarginRight(0))
 	return b
 }

+ 9 - 7
boxcost/api/print.go

@@ -75,12 +75,14 @@ func Printr(c *gin.Context, apictx *ApiSession) (interface{}, error) {
 	// !如果有omitempty存在,对应字段零值将被忽略
 	// !该字段设置为引用类型
 	// !创建该字段时设置默认值
-	type RegCode struct {
-		Id    primitive.ObjectID `bson:"_id,omitempty" json:"_id"`
-		Roles *[]string          `bson:"roles,omitempty" json:"roles"`
-		Key   string             `bson:"key,omitempty" json:"key"`
-	}
-	roles := []string{}
+
+	return nil, nil
+	// type RegCode struct {
+	// 	Id    primitive.ObjectID `bson:"_id,omitempty" json:"_id"`
+	// 	Roles *[]string          `bson:"roles,omitempty" json:"roles"`
+	// 	Key   string             `bson:"key,omitempty" json:"key"`
+	// }
+	// roles := []string{}
 
 	// ?接收客户端传参数,更新
 	// {"_id":"642e2e97243b5c5107d70204","roles":false}
@@ -91,7 +93,7 @@ func Printr(c *gin.Context, apictx *ApiSession) (interface{}, error) {
 	// }
 
 	// return repo.RepoUpdateSetDoc(apictx.CreateRepoCtx(), "reg-code", form.Id.Hex(), &RegCode{Roles: form.Roles})
-	return repo.RepoUpdateSetDoc(apictx.CreateRepoCtx(), "reg-code", "642e2e97243b5c5107d70204", &RegCode{Roles: &roles})
+	// return repo.RepoUpdateSetDoc(apictx.CreateRepoCtx(), "reg-code", "642e2e97243b5c5107d70204", &RegCode{Roles: &roles})
 
 }
 

+ 17 - 0
boxcost/api/utils.go

@@ -6,6 +6,7 @@ import (
 	"box-cost/log"
 	"bytes"
 	"fmt"
+	"math"
 	"math/rand"
 	"net/http"
 	"os"
@@ -19,6 +20,22 @@ import (
 
 var SignatureDir string = "https://www.3dqueen.cloud/box/v1/boxcost/public/"
 
+// func getRowHeight(content string, width float64,lineHeight float64) float64 {
+// 第一个参数为 行宽 第二个参数为 行高
+func getRowHeight(content string, params ...float64) float64 {
+	var perHeight float64 = 15
+	if len(params) == 2 {
+		perHeight = params[1]
+	}
+	num := float64(len([]rune(content)))
+	// 一行能容纳多少字
+	rowNum := params[0] / 1.8
+	// 向上取整获取行数 * 每行高度
+	rowHeight := math.Ceil(num/rowNum) * perHeight
+	return rowHeight
+
+}
+
 // 保存文件为pdf
 func savePdfToTmp(saveTmpDir, fileName string, data []byte) error {