Set up metadata to cell

Posted by: davide.vago on 28 August 2019, 7:50 pm EST

  • Posted 28 August 2019, 7:50 pm EST

    Good morning/evening

    I was wondering about the best practice to inject extra data to cells.
    Given a source as an array of objects, I'm using the method setDataSource to inject data into an instance of the Sheet but I would like to insert more information within every cells.

    Let's say I would like to have something like the below array as per datasource:

    var people = [
    {name: "Albert", isAdult: false, country: "American", website: "albert.com", info: { value: 'Ranger', extras: { accomplishments: 10, awards: 5, ...}}},
    {name: "Alice", isAdult: true, country: "China", website: "alice.com", info: { value: 'Singer', extras: { accomplishments: 1, awards: 15, ...}}},
    {name: "Bob", isAdult: false, country: "Canada", website: "bob.com", info: { value: 'Athlete', extras: { accomplishments: 45, awards: 25, ...}}}
    ];


    The info column should display as value just the key value but when selected the cell I would also be able to retrieve more details contained from the extras key.

    What's the best approach? I wouldn't use infinite loops with the setCell method as it's too expensive as operation.

    Reference to your documentation:
    https://help.grapecity.com/spread/SpreadJSWeb/JavascriptLibrary~GcSpread.Sheets.Sheet~setDataSource.html
  • Marked as Answer

    Replied 29 August 2019, 8:09 pm EST

    Hi Davide,

    You may create a custom cell type that extends the GC.Spread.Sheets.CellTypes.Text and override its paint method to display value from the 'value' key. Please refer to the following sample which demonstrates the same and let us know if you face any issues:
    https://codesandbox.io/s/spread-js-starter-cis2b

    Regards
  • Replied 30 August 2019, 2:06 am EST

    it works fine, just a flag, the given snippet has CellTypes set to Text so if the cell contains numbers it breaks, I had to use CellTypes.Base.

    Other than the above, you solved my problem, thanks so much
Need extra support?

Upgrade your support plan and get personal unlimited phone support with our customer engagement team

Learn More

Forum Channels