Skip to content

3. Attribute Configuration

Jos de Weger edited this page Dec 23, 2018 · 1 revision

Another way to map configuration to model is by adding DataAtributes to your model:

[SheetToObjectConfig(sheetHasHeaders:true)]
public class ToModel
{

	[MappingByHeader("StringValue")]
	[IsRequired]
	[Regex(@"/^[a-z]+[0-9_\/\s,.-]+$", true)]
	public string StringProperty { get; set; }
}

At the moment there is only one class attribute: SheetToObjectConfig. With this attribute u can set the default settings to map this model.

The following default validation attributes are available as attributes; [IsRequired] [Regex]

The model attributes can be overwritten by configuring a config for the model on the used SheetMapper

How to map a column to a model property?

There are multiple ways to map a column in the datasource to the model property

By Index

Use the .WithColumnIndex or [MappingByIndex] attribute to map the property based on Index. The index is 0-based

By Letter

Use the .WithColumnLetter() or [MappingByLetter] attribute to map the attribute base on "Excel-style" column naming. column "A" is the first column and "D" the fourth.

By ColumnName

When the datasource contains a first row with headers it's possible to map by name. Use the .WithHeader() or [MappingByHeader] to map by the name that is used on the first row

AutoMapping property

It's also possible to automap the properties based on their name without configuring anything. A header row is required for this feature, which name needs to match the name of the property you need that column to be mapped to. When u don't want the property to be mapped use the [IgnorePropertyMapping] attribute on the property.

For more information, check out the tests: https://github.com/josdeweger/SheetToObjects/blob/dev/src/SheetToObjects.Specs