SpreadJs

Posted by: moudud.hassan on 5 April 2023, 3:24 am EST

    • Post Options:
    • Link

    Posted 5 April 2023, 3:24 am EST

    We are using grape city in our Angular app.

        "@grapecity/spread-excelio": "^15.2.1",
        "@grapecity/spread-sheets": "^15.2.1",
        "@grapecity/spread-sheets-angular": "^15.2.1",

    We are showing the excel file inside gc-spread-sheets component

     <gc-spread-sheets
        *ngIf="!noData"
        [hostStyle]="spreadHostStyle"
        (workbookInitialized)="workbookInit($event)">
      </gc-spread-sheets>

    inside the workbookInit method we set the culture

    this.workbook = args.spread;
        var myCulture = new GC.Spread.Common.CultureInfo();
        // set default date format to use
        myCulture.DateTimeFormat.shortDatePattern = "dd/mm/yyyy";
        GC.Spread.Common.CultureManager.addCultureInfo("my-culture", myCulture);
        // update culture
        GC.Spread.Common.CultureManager.culture("my-culture");

    The issue is, when the spreadsheet renders in the browser it shows mm/dd/yyyy format. However if I double click on the cell to edit, it shows dd/mm/yyyy. If I click outside of the cell it goes back to mm/dd/yyyy. How can i always show dd/mm/yyyy

  • Posted 5 April 2023, 5:44 pm EST

    Hello Moudud,

    When you apply the custom culture to set the shortDatePattern, the editing format follows the current culture, using shortDatePattern.

    However, if any style formatter is applied to the cell, the cell display controlled by style formatter, and the cell editing text controlled by editing format. but that format is not fixed, it will change according to the last inputting text, and autoFomatter strategy. This behavior is same as that of Microsoft Excel.

    If you want the editor format to be same as that of the cell display format, you could use the following workaround:

            var oldCmd = GC.Spread.Sheets.Commands.editCell.execute;
            GC.Spread.Sheets.Commands.editCell.execute = function (context, options: any, isUndo) {
                var sheet = context.getSheetFromName(options.sheetName);
                // Get the Formatter or set your own formatter
                var formatter = sheet.getFormatter(options.row, options.col, GC.Spread.Sheets.SheetArea.viewport);
    
                if (options.editingFormatter && formatter) {
                    options.editingFormatter = new GC.Spread.Formatter.GeneralFormatter(formatter);
                }
                return oldCmd.apply(this, arguments);
            }
    
            var oldSetEditorValueFn = GC.Spread.Sheets.CellTypes.Text.prototype.setEditorValue;
            GC.Spread.Sheets.CellTypes.Text.prototype.setEditorValue = function (
                editor,
                value,
                context
            ) {
                var sheet = context.sheet,
                    row = context.row,
                    column = context.col;
                var cellValue = sheet.getValue(row, column);
                // Get the formatter or set your own formatter
                var formatter = sheet.getFormatter(row, column);
    
                if (cellValue && formatter) {
                    value = new GC.Spread.Formatter.GeneralFormatter(formatter).format(cellValue);
                    oldSetEditorValueFn.call(this, editor, value, context);
                } else {
                    oldSetEditorValueFn.apply(this, arguments);
                }
            };
    

    If you examine the above code snippet, I am getting the current cell formatter (var formatter) , you could replace it with another formatter that you want to display.

    Sample: https://jscodemine.grapecity.com/share/FoakIhU6j0qvsEOpnsYJfA/?defaultOpen={“OpenedFileName”:[“/index.html”,“/package.json”,“/src/app.component.ts”],“ActiveFile”:“/src/app.component.ts”}

    You may also refer to the following Forum case that discusses the similar issue: https://www.grapecity.com/forums/spreadjs/date-cell-formatter-return-to-default-eng-after-value-confirmation

    I hope that clarifies your question. Please let me know if you still face any issues.

    Regards,

    Ankit

  • Posted 6 April 2023, 12:23 am EST

    Hi Ankit,

    Thanks for your reply. Unfortunately your solution did not work for me. I am trying to load the attached excel file in SpreadJs. If you open the file, you can see that the cell G and H has dates in them and the format is dd/mm/yyyy. But when SpreadJs renders it, it changes the format to mm/dd/yyyy.

    Please note, I do not need to edit the cell so I don’t care what format edit cell shows.

    I need to SpreadJs to show how the excel file shows it which is dd/mm/yyyy.

    One more point, I have noticed that SpreadJs shows the date if the excel file has it as yyyy/mm/dd or mm/dd/yyyy. The problems seems to be only with dd/mm/yyyy format.SpreadJS_Date_Format.zip

  • Posted 9 April 2023, 2:42 pm EST - Updated 9 April 2023, 2:47 pm EST

    Hello Moudud,

    Thanks for sharing the excel file with us. Since Microsoft Excel is a native application, it picks the Short Date Format from the System itself and shows the date in that format only. For example, if the format for Short Date in your system is “dd/mm/yyyy”, Microsoft Excel shows the Date in the format “dd/mm/yyyy”.

    For SpreadJS, since it is a web application, it doesn’t have access to the System Short Date Format to be used.

    SpreadJS allows you to customize the date format based on your system locale settings while importing an Excel file. You need to add the default number format for your custom culture.

    For example, if you want the Excel file to show the date in the format “dd/mm/yyyy”, you need to set the LocalNumberFormat:

    
            // Apply Custom Culture
            let myCulture = new GC.Spread.Common.CultureInfo();
            myCulture.DateTimeFormat.shortDatePattern = "dd/mm/yyyy";
            // Set the LocalNumberFormat
            myCulture.LocalNumberFormat = {
                14: "dd/mm/yyyy"
            }
            GC.Spread.Common.CultureManager.addCultureInfo("my-culture", myCulture);
    

    Sample: https://jscodemine.grapecity.com/share/24pb4OGYHkOsJ9tcuH9fsw/?defaultOpen={"OpenedFileName"%3A["%2Fsrc%2Fapp.component.ts"]%2C"ActiveFile"%3A"%2Fsrc%2Fapp.component.ts"}

    References:

    Locale-based Date Format Imported: https://www.grapecity.com/spreadjs/docs/excelimpexp/excelimport/LocalebasedDateFormatImported#site_main_content-doc-content_title

    Excel-built in number format: https://learn.microsoft.com/en-us/dotnet/api/documentformat.openxml.spreadsheet.numberingformat?view=openxml-2.8.1

    Regards,

    Ankit

  • Posted 9 April 2023, 7:39 pm EST

    Yap that has fixed the problem. Thanks Ankit

Need extra support?

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

Learn More

Forum Channels