GetDataSource returns null after importing json

Posted by: lokesh0809 on 18 August 2019, 12:35 pm EST

  • Posted 18 August 2019, 12:35 pm EST

    I save the json wiith includebindingsource property set to true, now after loading this json into Spread.Sheets and calling getDataSource for sheet returns null. Please let me know how I can get the data source after export and import json?

  • Posted 19 August 2019, 6:04 am EST

    Hi Lokesh,

    The includeBindingSource property is supported with toJSON() method to export the data if the Spread Sheet is bounded to the DataSource.

    While importing the Excel, it set cell values one by one, hence the getDataSource() returns null.

    You may fetch the values by iterating the cells using the getCell() method.

    https://help.grapecity.com/spread/SpreadSheets12/webframe.html#SpreadJS~GC.Spread.Sheets.Worksheet~getCell.html

    Hope it helps!

    Regards,

    Manish Gupta

  • Posted 19 August 2019, 7:33 am EST

    Hi Manish,

    I never said that I am importing an Excel file. I am only saving and loading the JSON string.

    If binding source can not be fetched after loading the json, can you please tell me what is the use of includebindingsource argument during serialization???

  • Posted 20 August 2019, 4:12 am EST

    Hi Lokesh,

    As mentioned in the Documentation,

    SpreadJS serializes the sheet’s data source and the table’s data source as a cell value if the includeBindingSource argument of the toJSON method is true.

    https://help.grapecity.com/spread/SpreadSheets12/webframe.html#SpreadJS~GC.Spread.Sheets.Workbook~toJSON.html

    That is why when the JSON is imported using the fromJSON() method from JSON string retrieved from toJSON() method, the cells show values but the getDataSource() returns null.

    Hope it clears!

    Regards,

    Manish Gupta

  • Posted 20 August 2019, 1:44 pm EST

    Does it mean that there is no way to retrieve the data source after the json is reloaded?

    If not can it be added as a feature?

  • Posted 22 August 2019, 3:18 am EST

    Hi Lokesh,

    As mentioned earlier SpreadJS serializes the sheet’s data source and the table’s data source as a cell value if the includeBindingSource argument of the toJSON method is true. So we cannot retrieve the data source after reloading the json.

    However, if you have a requirement to store the spread template JSON and data source as separate values then you could simply set the includeBindingSource flag to false and save the data source separately, then when reloading the JSON, bind the saved dataSource again after loading the JSON template. Please refer to the following code snippet:

    // save spread JSON
    var savedJSON = spread.toJSON();
    // saving data source separately
    var source = spread.getSheetFromName("Sheet1").getDataSource();
    
    // reloading JSON
    // load json template
    spread.fromJSON(savedJSON);
    // rebind the data source
    spread.getSheetFromName("Sheet1").getDataSource(source, false); // pass second argument as false to skip resetting the sheet
    

    API reference:

    • setDataSource() method: http://help.grapecity.com/spread/SpreadSheets12/webframe.html#SpreadJS~GC.Spread.Sheets.Worksheet~setDataSource.html

    Please let us know if this doesn’t work for you.

    Regards

  • Posted 25 August 2019, 3:07 pm EST

    Hi,

    I am loading a previously saved JSON file in to Spread.Sheets. Your solution might work when I am saving and loading json within same application.

    Please suggest, what else can be done.

  • Posted 26 August 2019, 2:08 am EST

    Hi Lokesh,

    We are sorry but there isn’t any way to recover the data source from the spread JSON. One workaround could be to iterate cells and use the getValue() method to retrieve cell value and build the data source manually.

    Regards

Need extra support?

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

Learn More

Forum Channels