Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

set and get row height,unit of measure are not uniform?!!!same with col width #279

Closed
MichaelChen666 opened this issue Oct 27, 2018 · 0 comments
Labels
confirmed This issue can be reproduced

Comments

@MichaelChen666
Copy link

the source code :

	xlsx := excelize.NewFile()
	// 创建一个工作表
	index := xlsx.NewSheet("Sheet2")
	// 设置单元格的值
	xlsx.SetCellValue("Sheet2", "A2", "Hello world.")
	rowHeight := xlsx.GetRowHeight("Sheet2", 2)
	fmt.Println("the 2 row height: ", rowHeight)

	xlsx.SetCellValue("Sheet2", "A3", "Hello world.")
	xlsx.SetRowHeight("Sheet2", 3, 20)
	rowHeight = xlsx.GetRowHeight("Sheet2", 3)
	fmt.Println("the 3 row height: ", rowHeight)

	// 设置工作簿的默认工作表
	xlsx.SetActiveSheet(index)
	// 根据指定路径保存文件
	err := xlsx.SaveAs("./text.xlsx")
	if err != nil {
		fmt.Println(err)
	}

and the output:

the 2 row height:  20
the 3 row height:  20

I get the row height, it's 20, but I set another row to 20, and get 20. OK!!! but the result is not the same.

image

the actual row height is not the same. When setting row height, the unit of measure is the pound(Because the default unit of measurement of row height for Excel is pound).
But if it is not set, the unit of return value is pixel.

See source code:

// GetRowHeight provides a function to get row height by given worksheet name
// and row index. For example, get the height of the first row in Sheet1:
//
//    xlsx.GetRowHeight("Sheet1", 1)
//
func (f *File) GetRowHeight(sheet string, row int) float64 {
	xlsx := f.workSheetReader(sheet)
	for _, v := range xlsx.SheetData.Row {
		if v.R == row && v.Ht != 0 {
			return v.Ht
		}
	}
	// Optimisation for when the row heights haven't changed.
	return defaultRowHeightPixels
}

// SetRowHeight provides a function to set the height of a single row. For
// example, set the height of the first row in Sheet1:
//
//    xlsx.SetRowHeight("Sheet1", 1, 50)
//
func (f *File) SetRowHeight(sheet string, row int, height float64) {
	xlsx := f.workSheetReader(sheet)
	cells := 0
	rowIdx := row - 1
	completeRow(xlsx, row, cells)
	xlsx.SheetData.Row[rowIdx].Ht = height
	xlsx.SheetData.Row[rowIdx].CustomHeight = true
}

So, there are two issues, one is:
In the function of GetRowHeight, the code should be if v.R == row && v.CustomHeight != false, It's better to write this way, but it doesn't matter.

the another one is:
the unit of measure of row height is not uniform。So when I insert images,you can see

image

source code is:

	xlsx := excelize.NewFile()

	file, err := ioutil.ReadFile("./photo.jpg")
	if err != nil {
		fmt.Println(err)
	}

	format := `{"x_scale": 0.1, "y_scale": 0.1, "lock_aspect_ratio": false, "locked": false, "positioning": "oneCell"}`

	xlsx.SetRowHeight("Sheet1", 2, 30)
	xlsx.AddPictureFromBytes("Sheet1", "A2", format, "Excel Logo", ".jpg", file)
	xlsx.SetRowHeight("Sheet1", 3, 30)
	xlsx.AddPictureFromBytes("Sheet1", "D2", format, "Excel Logo", ".jpg", file)
	xlsx.SetRowHeight("Sheet1", 4, 30)
	xlsx.AddPictureFromBytes("Sheet1", "G2", format, "Excel Logo", ".jpg", file)

	err = xlsx.SaveAs("./text.xlsx")
	if err != nil {
		fmt.Println(err)
	}

But, but, but,

image

source code:

	xlsx := excelize.NewFile()

	file, err := ioutil.ReadFile("./photo.jpg")
	if err != nil {
		fmt.Println(err)
	}

	format := `{"x_scale": 0.1, "y_scale": 0.1, "lock_aspect_ratio": false, "locked": false, "positioning": "oneCell"}`

	xlsx.SetRowHeight("Sheet1", 2, 30)
	xlsx.SetRowHeight("Sheet1", 3, 30)
	xlsx.SetRowHeight("Sheet1", 4, 30)
	xlsx.AddPictureFromBytes("Sheet1", "A2", format, "Excel Logo", ".jpg", file)
	xlsx.AddPictureFromBytes("Sheet1", "D2", format, "Excel Logo", ".jpg", file)
	xlsx.AddPictureFromBytes("Sheet1", "G2", format, "Excel Logo", ".jpg", file)

	err = xlsx.SaveAs("./text.xlsx")
	if err != nil {
		fmt.Println(err)
	}

it's so funny, ha!The logic of inserting pictures is problematic. When you insert the first picture, it’s ok! When you insert the second picture, you change the row height, and it's ok! But the first picture is not OK! The final effect should not be related to the order of setup. Do you think so?

@MichaelChen666 MichaelChen666 changed the title set and get row height,unit of measure are not uniform?!!! set and get row height,unit of measure are not uniform?!!!same with col width Oct 27, 2018
taomin597715379 pushed a commit to taomin597715379/excelize that referenced this issue Nov 24, 2018
Change-Id: I8a2c378af74a4070ab8e2bfb61eda31b03c0734a
@xuri xuri added the confirmed This issue can be reproduced label Dec 27, 2018
@xuri xuri closed this as completed in a246db6 May 19, 2023
xuri added a commit to JDavidVR/excelize that referenced this issue Jul 11, 2023
jenbonzhang pushed a commit to jenbonzhang/excelize that referenced this issue Oct 22, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
confirmed This issue can be reproduced
Projects
None yet
Development

No branches or pull requests

2 participants