コード例 #1
0
func NewDatabookWithData(sheetName map[string]string, data interface{}, headers ...map[string][]string) (*Databook, error) {
	s := len(sheetName)

	switch data.(type) {
	case map[string]*tablib.Dataset:
		dataModel1 := data.(map[string]*tablib.Dataset)
		d1 := len(dataModel1)
		if s != d1 {
			return nil, ErrParamsType
		}

		databook := tablib.NewDatabook()
		for k, _ := range dataModel1 {
			if _, ok := sheetName[k]; !ok {
				return nil, ErrParamsType
			}
			databook.AddSheet(sheetName[k], dataModel1[k])
		}
		db := &Databook{XDatabook: databook}
		return db, nil
	case map[string][]map[string]interface{}:
		dataModel2 := data.(map[string][]map[string]interface{})
		d2 := len(dataModel2)

		if len(headers) != 1 {
			return nil, ErrParamsType
		}

		h := len(headers[0])

		if s != h || s != d2 {
			return nil, ErrParamsType
		}
		databook := tablib.NewDatabook()
		for k, _ := range dataModel2 {
			if _, ok := sheetName[k]; !ok {
				return nil, ErrParamsType
			}
			if _, ok := headers[0][k]; !ok {
				return nil, ErrParamsType
			}

			dataset, err := NewDatasetWithData(headers[0][k], dataModel2[k])
			if err != nil {
				return nil, err
			}
			databook.AddSheet(sheetName[k], dataset)
		}
		db := &Databook{XDatabook: databook}
		return db, nil

	default:
		return nil, ErrParamsType
	}
}
コード例 #2
0
func NewDatabook() *Databook {
	db := &Databook{XDatabook: tablib.NewDatabook()}
	return db
}