Set default styles for all sheets including new sheets

Posted by: richard on 13 January 2025, 2:52 pm EST

    • Post Options:
    • Link

    Posted 13 January 2025, 2:52 pm EST

    Is there a way to set default styles for an entire workbook so even new sheets will always have it set be default?

    I see the ability to set default style but that only works on the current sheet.

    https://developer.mescius.com/spreadjs/docs/features/worksheet/work-with-worksheets#set-default-style

    How can I set the default style for all sheets in the workbook AND ensure that new sheets added also inherit this default style.

    In particular I’m trying to set the font color/size by default but I can only get it working on a per-sheet basis.

  • Posted 14 January 2025, 7:24 am EST

    Hi,

    In SpreadJS, styles are applied at the worksheet level rather than the workbook level. This behavior aligns with Excel’s functionality, where applying styles to the current sheet does not impact other sheets in the workbook.

    To apply default styles across all worksheets, you will need to iterate through each sheet in the workbook. Below is an example implementation for your reference:

    Code Snippet
    // Ensuring the default style is applied to the active sheet
    spread.bind(GC.Spread.Sheets.Events.ActiveSheetChanged, function (e, args) {
        if (isStyleApplied) {
            var sheet = args.newSheet;
           sheet.setDefaultStyle(defaultStyle , GC.Spread.Sheets.SheetArea.viewport);
        }
    });
    
    // Adding styles to all worksheets in the workbook
    document.getElementById('addStyle').addEventListener("click", function () {
        var sheetCount = spread.getSheetCount();
        isStyleApplied = true;
        for (var i = 0; i < sheetCount; i++) {
            var sheet = spread.getSheet(i);
            sheet.setDefaultStyle(defaultStyle , GC.Spread.Sheets.SheetArea.viewport);
        }
    });

    For newly created worksheets, you can use the ActiveSheetChanged event to apply the default style to the new sheets.

    Sample: Sample.zip

    You could refer to the attached sample project that demonstrates this functionality in detail.

    References :-

    ActiveSheetChanged: https://developer.mescius.com/spreadjs/api/classes/GC.Spread.Sheets.Events#activesheetchanged

    setDefaultStyle API = https://developer.mescius.com/spreadjs/api/classes/GC.Spread.Sheets.Worksheet#setdefaultstyle

    If you have further questions or require additional assistance, feel free to reach out.

    Best regards,

Need extra support?

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

Learn More

Forum Channels