Documents for Excel, .NET Edition Documentation
Customize User Interaction / Use Tags
In This Topic
    Use Tags
    In This Topic

    With GcExcel, you can configure tags for worksheets which allow you to store private data in a cell, row, column, range or spreadsheet. The tags can store any type of data and are not visible to the end user. Tags are retained while performing the import or export operations from or to JSON.

    Tags for worksheets can be configured using the Tag property of IWorksheet interface. Whereas for a cell or a range of cells, the tags can be configured using the Tag property of IRange interface. If the tag values are different in a range of cells, the tag value of the top-left cell of the range will be returned. The EntireColumn and EntireRow property of IRange interface, along with the Tag property can be used to get or set tags for columns and rows respectively.

    You can also configure custom tags by instantiating a class. A json serializer or deserializer should be provided to support json I/O by implementing IJsonSerializer.

    Using Code

    Refer to the following code to use tags:

    C#
    Copy Code
    public void Tags()
    {
        // Initialize workbook
        Workbook workbook = new Workbook();
        // Fetch default worksheet
        IWorksheet worksheet = workbook.Worksheets[0];
    
        //Add Tag for worksheet
        worksheet.Tag = "This is a Tag for sheet.";
        // Add Tag for Cell C1
        worksheet.Range["C1"].Tag = "This is a Tag for Cell C1";
        //Add Tag for Row 4
        worksheet.Range["A4"].EntireRow.Tag = "This is a Tag for Row 4";
        //Add Tag for Column F
        worksheet.Range["F5"].EntireColumn.Tag = "This is a Tag for Column F";
        //Add tag for Range A1:B2
        worksheet.Range["A1:B2"].Tag = "This is a Tag for A1:B2";
    
        // Exporting workbook to JSON stream
        var jsonstr = workbook.ToJson();
    
        // Initialize another workbook
        Workbook workbook2 = new Workbook();
    
        // Importing JSON stream in workbook
        workbook2.FromJson(jsonstr);
    
        // Get Tag of Range A1:B2
        object tag = workbook2.Worksheets[0].Range["A1:B2"].Tag;
    
        // Tags are preserved while exporting and importing json stream
        Console.WriteLine(" Tag for CellRange[A1:B2] is : " + tag);
    
    }


    Refer to the following code to use custom tags:

    C#
    Copy Code
    class CustomTags
    {
        public void CustomTag()
        {
            // Initialize workbook
            Workbook workbook = new Workbook();
            // Fetch default worksheet 
            IWorksheet worksheet = workbook.Worksheets[0];
    
            Workbook.TagJsonSerializer = new NetJsonSerializer();
    
            // Set Tag of "A1" as custom type "Student"
            worksheet.Range["A1"].Tag = new Student("Robin", 7);
    
            // Exporting workbook to JSON stream
            string json = workbook.ToJson();
    
            // Initialize another workbook
            Workbook workbook2 = new Workbook();
    
            // Importing JSON stream in workbook
            workbook2.FromJson(json);
    
            // Get Tag as JObject
            object tag = workbook2.Worksheets[0].Range["A1"].Tag;
    
            // Convert JObject to "Student"
            Student student = (tag as Newtonsoft.Json.Linq.JObject).ToObject<Student>();
    
            // Tags are preserved while exporting and importing json stream
            Console.WriteLine(" Tag for CellRange[A1] is of class: " + student);
            Console.WriteLine(" Tag for CellRange[A1] is: " + tag);
    
        }
    }
    
    internal class Student
    {
        public string name;
        public int age;
    
        public Student(string name, int age)
        {
            this.name = name;
            this.age = age;
        }
    }
    internal class NetJsonSerializer : IJsonSerializer
    {
        public object Deserialize(string json)
        {
            return Newtonsoft.Json.JsonConvert.DeserializeObject(json) as Newtonsoft.Json.Linq.JObject;
        }
    
        public string Serialize(object value)
        {
            return Newtonsoft.Json.JsonConvert.SerializeObject(value);
        }
    }