Clean up workBook without breaking the instance

Posted by: davide.vago on 12 November 2020, 2:28 am EST

  • Posted 12 November 2020, 2:28 am EST

    Good morning/afternoon

    I'm currently trying to clean up a workbook from the existing sheets. It seems the only method available from the documentation is: removeSheet(sheetIndex)

    The removeSheet, however, does keep the indexes of the previous sheets so once the event triggers a new default sheet gets generated with a progressive number (looking at the sandbox below you will see Sheet4 after clicking the button at the bottom of the workBook).

    Checkout my sandbox:
    https://codesandbox.io/s/spreadjs-workbook-remove-multiple-sheets-rehdb

    The above seems like the workBook keeps previous things cached so I'm wondering what's the best practice and what could I use to have a less hacky solution.

  • Marked as Answer

    Replied 12 November 2020, 11:09 pm EST

    Hi,

    This is the expected behavior from the SJS. Excel also does the same. Further, you may use the clearSheets method instead of removeSheet . For changing the name of the sheet You may Sheet Changed event. Please refer to the following code snippet and the attached sample that demonstrates the same.


    spread.bind(GC.Spread.Sheets.Events.SheetChanged, (e, args) => {
    if (args.propertyName === "insertSheet") {
    spread.getSheet(args.sheetIndex).name("Sheet" + spread.getSheetCount());
    }
    });


    Sample: https://codesandbox.io/s/smoosh-browser-z29mm?file=/src/index.js:380-582

    API References:
    clearSheets: https://www.grapecity.com/spreadjs/docs/v13/online/SpreadJS~GC.Spread.Sheets.Workbook~clearSheets.html

    SheetChanged:https://www.grapecity.com/spreadjs/docs/v13/online/SpreadJS~GC.Spread.Sheets.Events~SheetChanged_EV.html

    Regards
    Avinash
  • Replied 15 November 2020, 8:53 pm EST

    Thanks Avinash,

    This solution did work, however I would flag the fact that the instance keeps old indexes once clearSheets gets triggered to the developers as it seems something that might create underlying issues.

    In general this snippet from your sample (see below) does seems an hack to get the right index. It does work but this is something the library should really do automatically under the hood.

    spread.bind(GC.Spread.Sheets.Events.SheetChanged, (e, args) => {
    if (args.propertyName === "insertSheet") {
    spread.getSheet(args.sheetIndex).name("Sheet" + spread.getSheetCount());
    }
    });
  • Replied 16 November 2020, 7:18 pm EST

    Hi Davide,

    We are glad to know that the solution works for you. Further, We have made an enhancement request on your behalf. We will update you regarding this as soon as we get any information. The internal ID for this issue will be SJS-6504.

    Regards
    Avinash
  • Replied 19 November 2020, 2:12 pm EST

    Hi Davide,

    The Dev Team informed us that it is by design. We are following the Excel Policy for adding new Sheets. Please let me know if you have any further queries.

    Regards
    Avinash
Need extra support?

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

Learn More

Forum Channels