Example #1
0
// 解析指定列的字段定义
func parse_field(sheet *xlsx.Sheet, cfg *Cfg_t, col int, field *Field_t, errvec *tool.Errvec_t) bool {
	// 取出字段英文名
	field.En_name = strings.Trim(tool.Get_cell_str(sheet, cfg.row_fields_begin+int(field_row_en_name), col), " \t")
	if field.En_name == "" {
		// 碰到空字段则停止解析
		return true
	}

	// 取出字段类型
	var fieldtype_cell string = strings.Trim(tool.Get_cell_str(sheet, cfg.row_fields_begin+int(field_row_data_type), col), " \t")
	field.Fieldtype = type_name_2_enum(&fieldtype_cell)

	// 取出字段属性
	var fieldattr_cell string = strings.Trim(tool.Get_cell_str(sheet, cfg.row_fields_begin+int(field_row_is_unique), col), " \t")

	field.Fieldattr = Fieldattr_none
	switch {
	case is_unique_key_by_str(&fieldattr_cell):
		field.Fieldattr = Fieldattr_1_key
	case is_primary_key_by_str(&fieldattr_cell):
		field.Fieldattr = Fieldattr_n_key
	case is_array_by_str(&fieldattr_cell):
		field.Fieldattr = Fieldattr_array
	case is_set_by_str(&fieldattr_cell):
		field.Fieldattr = Fieldattr_set
	}

	field.Cn_name = strings.Trim(tool.Get_cell_str(sheet, cfg.row_data_begin, col), " \t")
	field.Comment = field.Cn_name

	if field.Cn_name == "" {
		*errvec = append(*errvec, tool.GetErrMsg("解析第<%s>行时发生错误:中文名不允许为空", col))
		return false
	}

	if fieldtype_cell == "" {
		*errvec = append(*errvec, tool.GetErrMsg("解析<%s>字段时发生错误:该字段的类型为空", field.Cn_name))
		return false
	}

	if Fieldtype_none == field.Fieldtype {
		*errvec = append(*errvec, tool.GetErrMsg("解析<%s>字段时发生错误:不支持<%s>类型的字段", field.Cn_name, fieldtype_cell))
		return false
	}

	return true
}
Example #2
0
// 解析文件定义区
func parse_cfg(sheet *xlsx.Sheet, row_cnt int, cfg *Cfg_t, errvec *tool.Errvec_t) bool {
	// 文件名称
	cfg.En_name = tool.Get_cell_str(sheet, cfg.row_file_cfg_begin+int(cfg_row_1), int(cfg_col_1))

	// 最大数据条数
	cfg.Maxcnt = tool.Get_cell_int(sheet, cfg.row_file_cfg_begin+int(cfg_row_1), int(cfg_col_2))

	if cfg.En_name == "" {
		*errvec = append(*errvec, tool.GetErrMsg("分析<%s>失败:必须给表填上英文名", cfg.En_name))
		return false
	}

	return true
}
Example #3
0
// 解析配置数据区
func parse_data(sheet *xlsx.Sheet, row_cnt int, col_cnt int, cfg *Cfg_t, errvec *tool.Errvec_t) bool {
	var is_empty bool = (cfg.row_data_begin+1 > row_cnt)
	if is_empty {
		return true
	}

	is_ok := true

	var n_field int = len(cfg.Fields)

	cfg.Table = make(table_t, 0, row_cnt-cfg.row_data_begin)

	// 将数据区的行和列存入对应的数组
	for row := int(cfg.row_data_begin + 1); row <= row_cnt; row++ {
		var is_empty_row bool = false
		var r row_t = make(row_t, n_field)

		for col := 1; col <= n_field; col++ {
			r[col-1] = tool.Get_cell_str(sheet, row, col)

			// 一旦出现某行的第一个单元格为空,则停止解析
			if 1 == col {
				if r[col-1] == "" {
					is_empty_row = true
					break
				}
			}
		}

		if is_empty_row {
			break
		}

		cfg.Table = append(cfg.Table, r)
	}

	return is_ok
}