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

Reading cell value in the pivot table range #1228

Open
Guoaking opened this issue May 14, 2022 · 1 comment
Open

Reading cell value in the pivot table range #1228

Guoaking opened this issue May 14, 2022 · 1 comment

Comments

@Guoaking
Copy link

Description
我尝试写成一个数据透视表, 并把透视表的数据用二维柱状图展示
但是发现数据透视表的数据似乎读不到 , 并发现如果mac打开并手动保存一下的话就可以读到

Steps to reproduce the issue:

func main() {
	pic();
}

func pic(){
	categories := map[string]string{
		//"A2": "Small", "A3": "Normal", "A4": "Large",
		"A1":"类型","B1": "标题", "C1": "psm", "D1": "数量"}
	values := map[string]string{
		"A2": "default", "B2": " 索引延时 alert", "C2": "", "D2":"355",
		"A3": "infra-job-clickhouse", "B3": " Clickhouse 基础组件巡检 Alert", "C3": "", "D3":"47",
		"A4": "infra-job-redis", "B4": " Redis 基础组件巡检 Alert", "C4": "", "D4":"20",
		"A5": "vm", "B5": " CPU usage  alert", "C5": "", "D5":"17",
		"A6": "vm", "B6": " CPU usage  alert222", "C6": "", "D6":"17",
	}
	f := excelize.NewFile()
	for k, v := range categories {
		f.SetCellValue("Sheet1", k, v)
	}
	for k, v := range values {
		f.SetCellValue("Sheet1", k, v)
	}
	f.SetColWidth("Sheet1", "A", "D", 30)


	f.NewSheet("Sheet2")

	//添加分析
	if err := f.AddPivotTable(&excelize.PivotTableOption{
		DataRange:       "Sheet1!$A$1:$D$15",
		PivotTableRange: "Sheet2!$A$1:$A$2",
		//行 标题 类型
		Rows: []excelize.PivotTableField{
			{Data: "类型"}, {Data: "标题"}},
		Filter: []excelize.PivotTableField{},
		//	列
		Columns: []excelize.PivotTableField{},
		//	数据
		Data: []excelize.PivotTableField{
			{Data: "数量", Name: "总数量", Subtotal: "Sum"}},
		RowGrandTotals: true,
		ColGrandTotals: true,
		ShowDrill:      true,
		ShowRowHeaders: true,
		ShowColHeaders: true,
		ShowLastColumn: true,
	}); err != nil {
		fmt.Println(err)
	}


	//保存
	if err := f.SaveAs("Book1.xlsx"); err != nil {
		fmt.Println(err)
	}

	add()
}

func add(){
	f, err := excelize.OpenFile("Book1.xlsx")
	if err != nil {
		fmt.Println(err)
		return
	}
	defer func() {
		if err := f.Close(); err != nil {
			fmt.Println(err)
		}
	}()

        //在这里的输出不符合预期
	valeue,err:=f.GetCellValue("Sheet2","A1")
	fmt.Println(valeue)

	cols, err := f.GetCols("Sheet2")
	
	if err != nil {
		fmt.Println(err)
		return
	}
	fmt.Println(len(cols))
	for _, col := range cols {
		for _, rowCell := range col {
			fmt.Print(rowCell, "\t")
		}
		fmt.Println()
	}

	//categories  水平数据 y轴 分类标签?  应该用第一列 第二列  Sheet1!$A$2:$A$59 Sheet1!$A$2:$B$59
	//values x轴 ?                     第三列  Sheet1!$C$2:$C$59
	//legend 图例 关闭
	if err := f.AddChart("Sheet2", "G1", `{
      "type": "bar",
      "series": [
      {
          "categories": "Sheet1!$A$2:$B$8",
          "values": "Sheet2!$C$2:$C$8"
      }],
      "format":
      {
          "x_scale": 1.0,
          "y_scale": 1.0,
          "x_offset": 15,
          "y_offset": 10,
          "print_obj": true,
          "lock_aspect_ratio": false,
          "locked": false
      },
      "legend":
      {
			"none": true
      },
      "title":
      {
          "name": "汇总"
      },
      "plotarea":
      {
          "show_bubble_size": true,
          "show_cat_name": false,
          "show_leader_lines": true,
          "show_percent": true,
          "show_series_name": true,
          "show_val": true
      },
      "show_blanks_as": "zero"
    }`); err != nil {
		fmt.Println(err)
	}

	//保存
	if err := f.SaveAs("Book1.xlsx"); err != nil {
		fmt.Println(err)
	}
}

Describe the results you received:

实际上没有
Describe the results you expected:

应该可以打印出sheet2 A1 的value
Output of go version:

go1.17.2 Mac 

Excelize version or commit ID:

版本应该是v2.5.0

Environment details (OS, Microsoft Excel™ version, physical, etc.):

macos12.1   excel 16.61  inteli7
@xuri
Copy link
Member

xuri commented May 14, 2022

This library doesn't support generating pivot table cache, and won't set cell value in the pivot table range currently, so you only can read the cell value in the pivot table until the spreadsheets application generates these.

@xuri xuri changed the title 似乎有概率读不到数据透视表信息 Reading cell value in the pivot table range May 14, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants