Enum

This guide demonstrates different features of excel enum type.

Use predefined enum type

The basic enum guide, please go to read Enum →

For example, enum type FruitType in common.proto is defined as:

enum FruitType {
  FRUIT_TYPE_UNKNOWN = 0 [(tableau.evalue).name = "Unknown"];
  FRUIT_TYPE_APPLE   = 1 [(tableau.evalue).name = "Apple"];
  FRUIT_TYPE_ORANGE  = 2 [(tableau.evalue).name = "Orange"];
  FRUIT_TYPE_BANANA  = 4 [(tableau.evalue).name = "Banana"];
}

A worksheet ItemConf in HelloWorld.xlsx:

IDType
map<uint32, Item>enum<.FruitType>
Item’s IDFruit’s type
11
2Orange
3FRUIT_TYPE_BANANA

Generated:

hello_world.proto
// --snip--
import "common.proto";
option (tableau.workbook) = {name:"HelloWorld.xlsx" namerow:1 typerow:2 noterow:3 datarow:4};

message ItemConf {
  option (tableau.worksheet) = {name:"ItemConf"};

  map<uint32, Item> item_map = 1 [(tableau.field) = {key:"ID" layout:LAYOUT_VERTICAL}];
  message Item {
    uint32 id = 1 [(tableau.field) = {name:"ID"}];
    FruitType type = 2 [(tableau.field) = {name:"Type"}];
  }
}
ItemConf.json
{
    "itemMap":  {
        "1":  {
            "id":  1,
            "type":  "FRUIT_TYPE_APPLE"
        },
        "2":  {
            "id":  3,
            "type":  "FRUIT_TYPE_ORANGE"
        }
        "3":  {
            "id":  2,
            "type":  "FRUIT_TYPE_BANANA"
        },
    }
}

Define enum type in sheet

There are two kinds of Mode (in metasheet @TABLEAU) to define enum types in a sheet:

  • MODE_ENUM_TYPE: define single enum type in a sheet.
  • MODE_ENUM_TYPE_MULTI: define multiple enum types in a sheet.

Single enum type in sheet

You should specify Mode option to MODE_ENUM_TYPE in metasheet @TABLEAU.

For example, a worksheet ItemType in HelloWorld.xlsx:

NameAlias
ITEM_TYPE_FRUITFruit
ITEM_TYPE_EQUIPEquip
ITEM_TYPE_BOXBox
SheetMode
ItemTypeMODE_ENUM_TYPE

Generated:

hello_world.proto
// --snip--
option (tableau.workbook) = {name:"HelloWorld.xlsx"};

// Generated from sheet: ItemType.
enum ItemType {
  ITEM_TYPE_INVALID = 0;
  ITEM_TYPE_FRUIT = 1 [(tableau.evalue).name = "Fruit"];
  ITEM_TYPE_EQUIP = 2 [(tableau.evalue).name = "Equip"];
  ITEM_TYPE_BOX = 3 [(tableau.evalue).name = "Box"];
}

Multiple enum types in sheet

A block defines an enum type, and it is a series of contiguous non-empty rows. So different blocks are seperated by one or more empty rows.

You should specify Mode option to MODE_ENUM_TYPE_MULTI in metasheet @TABLEAU.

For example, a worksheet ItemType in HelloWorld.xlsx:

CatTypeCatType note
NumberNameAlias
1CAT_TYPE_RAGDOLLRagdoll
2CAT_TYPE_PERSIANPersian
3CAT_TYPE_SPHYNXSphynx
DogTypeDogType note
NumberNameAlias
1DOG_TYPE_POODLEPoodle
2DOG_TYPE_BULLDOGBulldog
3DOG_TYPE_DACHSHUNDDachshund
BirdTypeBirdType note
NumberNameAlias
1CANARYCanary
2WOODPECKERWoodpecker
3OWLOwl
SheetMode
ItemTypeMODE_ENUM_TYPE_MULTI

Generated:

hello_world.proto
// --snip--
option (tableau.workbook) = {name:"HelloWorld.xlsx"};

// CatType note
enum CatType {
  option (tableau.etype) = {name:"EnumType" note:"CatType note"};

  CAT_TYPE_INVALID = 0;
  CAT_TYPE_RAGDOLL = 1 [(tableau.evalue).name = "Ragdoll"]; // Ragdoll
  CAT_TYPE_PERSIAN = 2 [(tableau.evalue).name = "Persian"]; // Persian
  CAT_TYPE_SPHYNX = 3 [(tableau.evalue).name = "Sphynx"]; // Sphynx
}

// DogType note
enum DogType {
  option (tableau.etype) = {name:"EnumType" note:"DogType note"};

  DOG_TYPE_INVALID = 0;
  DOG_TYPE_POODLE = 1 [(tableau.evalue).name = "Poodle"]; // Poodle
  DOG_TYPE_BULLDOG = 2 [(tableau.evalue).name = "Bulldog"]; // Bulldog
  DOG_TYPE_DACHSHUND = 3 [(tableau.evalue).name = "Dachshund"]; // Dachshund
}

// BirdType note
enum BirdType {
  option (tableau.etype) = {name:"EnumType" note:"BirdType note"};

  BIRD_TYPE_INVALID = 0;
  BIRD_TYPE_CANARY = 1 [(tableau.evalue).name = "Canary"]; // Canary
  BIRD_TYPE_WOODPECKER = 2 [(tableau.evalue).name = "Woodpecker"]; // Woodpecker
  BIRD_TYPE_OWL = 3 [(tableau.evalue).name = "Owl"]; // Owl
}

Specify Number column

In Number column, you can specify custom unique enum value number.

For example, a worksheet ItemType in HelloWorld.xlsx:

NumberNameAlias
0ITEM_TYPE_UNKNOWNUnknown
10ITEM_TYPE_FRUITFruit
20ITEM_TYPE_EQUIPEquip
30ITEM_TYPE_BOXBox
SheetMode
ItemTypeMODE_ENUM_TYPE

Generated:

hello_world.proto
// --snip--
option (tableau.workbook) = {name:"HelloWorld.xlsx"};

// Generated from sheet: ItemType.
enum ItemType {
  ITEM_TYPE_UNKNOWN = 0 [(tableau.evalue).name = "Unknown"];
  ITEM_TYPE_FRUIT = 10 [(tableau.evalue).name = "Fruit"];
  ITEM_TYPE_EQUIP = 20 [(tableau.evalue).name = "Equip"];
  ITEM_TYPE_BOX = 30 [(tableau.evalue).name = "Box"];
}

Define and use enum type in sheet

For example, two worksheets ItemType and ItemConf in HelloWorld.xlsx:

NumberNameAlias
1ITEM_TYPE_FRUITFruit
2ITEM_TYPE_EQUIPEquip
3ITEM_TYPE_BOXBox
IDTypeNamePrice
map<int32, Item>enum<.ItemType>stringint32
Item’s IDItem’s typeItem’s nameItem’s price
1FruitApple40
2FruitOrange20
3EquipSword10
SheetMode
ItemTypeMODE_ENUM_TYPE
ItemConf

Generated:

hello_world.proto
// --snip--
import "common.proto";
option (tableau.workbook) = {name:"HelloWorld.xlsx" namerow:1 typerow:2 noterow:3 datarow:4};

// Generated from sheet: ItemType.
enum ItemType {
  ITEM_TYPE_INVALID = 0;
  ITEM_TYPE_FRUIT = 1 [(tableau.evalue).name = "Fruit"];
  ITEM_TYPE_EQUIP = 2 [(tableau.evalue).name = "Equip"];
  ITEM_TYPE_BOX = 3 [(tableau.evalue).name = "Box"];
}

message ItemConf {
  option (tableau.worksheet) = {name:"ItemConf"};

  map<int32, Item> item_map = 1 [(tableau.field) = {key:"ID" layout:LAYOUT_VERTICAL}];
  message Item {
    int32 id = 1 [(tableau.field) = {name:"ID"}];
    protoconf.ItemType type = 2 [(tableau.field) = {name:"Type"}];
    string name = 3 [(tableau.field) = {name:"Name"}];
    int32 price = 4 [(tableau.field) = {name:"Price"}];
  }
}
ItemConf.json
{
    "itemMap": {
        "1": {
            "id": 1,
            "type": "ITEM_TYPE_FRUIT",
            "name": "Apple",
            "price": 40
        },
        "2": {
            "id": 2,
            "type": "ITEM_TYPE_FRUIT",
            "name": "Orange",
            "price": 20
        },
        "3": {
            "id": 3,
            "type": "ITEM_TYPE_EQUIP",
            "name": "Sword",
            "price": 10
        }
    }
}