Aggregates of custom type are being exported as string

Hi, i'm using grid excel export functionality, of version "22.1.34"

  aggregates: AggregateRowModel[] = [
    {
      columns: [
        {
          type: "Custom",
          field: "display_name",
          customAggregate: () => {
            return "Total";
          },
        },
        {
          type: "Sum",
          field: "plans_purchased",
          format: "N",
        },
        {
          type: "Sum",
          field: "class_booked",
          format: "N",
        },
        {
          type: "Sum",
          field: "total_attended",
          format: "N",
        },
        {
          type: "Sum",
          field: "total_late",
          format: "N",
        },
        {
          type: "Sum",
          field: "total_absent",
          format: "N",
        },
        {
          type: "Custom",
          field: "attendance_rate",
          format: "P2",
          customAggregate: (data: ReturnType | StudentReport[]) => {
            // When ejsGrid.export(), data provided will be StudentReport[]
            if ((data as ReturnType).aggregates) {
              let aggData = data as ReturnType;
              this.totalAttendanceRate =
                (aggData.aggregates["total_attended - sum"] +
                  aggData.aggregates["total_late - sum"]) /
                aggData.aggregates["class_booked - sum"];

              if (isNaN(this.totalAttendanceRate)) {
                return;
              }
            }
            return this.totalAttendanceRate;
          },
        },

        {
          type: "Sum",
          field: "lifetime_value",
          format: "N2",
        },
      ],
    },
  ];
These are my aggregate on the grid but the aggregate of type custom is being exported as string you can see in the image below Image_6538_1728898191407 the same issue does occur with N2 format aggregates of custom type as well , although my custom aggregate does return the number, how can i export it as number ?

3 Replies

AR Aishwarya Rameshbabu Syncfusion Team October 15, 2024 05:27 PM UTC

Hi Usman Ali,


Greetings from Syncfusion support.


By default, Grid aggregate values are maintained as strings, displaying along with the aggregate type (min, max, sum, aggregate). Consequently, these values are returned and exported as strings, which is the expected behavior. However, you we have provided a sample-level customization based on your requirement. Please refer to the following code example and sample, where we customized the aggregate values of the Sales column in the excelAggregateQueryCellInfo event of the Grid. In this event we have parsed the string to number using parseFloat method for percentage format by replacing the % symbol however for numeric format you can directly parseFloat the value.


App.component.ts

 

    excelAggregateQueryCellInfo(args: any): void {

        if (args.cell.column.field === 'Sales') {

          if (args.style.value.includes('%')) { 

            let newvalue = parseFloat(args.style.value.replace("%", "")); // converting the string to number.

            args.style.value = newvalue / 100; // Converting to original value from percentage value.

          }

        }

      }

 

App.component.html

 

    <ejs-grid  #grid id='DefaultExport' [dataSource]="data" [allowSorting]='true' [enableHover]='false' [allowMultiSorting]='true' [allowFiltering]='true' [filterSettings]='filterSettings' [aggregates]='aggregates'  [toolbar]='toolbar'

        height=300 [gridLines]="gridLines"   [allowExcelExport]='true' [allowPdfExport]='true'  (toolbarClick)='toolbarClick($event)'  (excelAggregateQueryCellInfo)='excelAggregateQueryCellInfo($event)'>

        <e-columns>

               ……………………………………….

        </e-columns>                                           

    </ejs-grid>

 



Sample: https://stackblitz.com/edit/angular-vuu7yf-fkd36v?file=src%2Fapp.component.html,src%2Fapp.component.ts,lib.es5.d.ts,src%2Fdata.ts


Screenshot:




API Reference: excelAggregateQueryCellInfo


If you need any other assistance or have additional questions, please feel free to contact us.


Regards

Aishwarya R



UA Usman Ali October 16, 2024 08:28 AM UTC

It's working. Thank you so much for the help.  

👍

 



AR Aishwarya Rameshbabu Syncfusion Team October 17, 2024 10:33 AM UTC

Hi Usman Ali,


You’re most welcome. We are happy to hear that the provided solution was helpful. Please get back to us if you need any other assistance.


Regards

Aishwarya R


Loader.
Up arrow icon