Pivot Table values in Spreadsheet

Hello everyone,

I have built a Syncfusion Pivot Table within a page of my Angular project and the same page also contains a Syncfusion Spreadsheet. 

I would like to create a function similar to the PivotViewComponent's excelExport, but that instead of downloading the Excel file, it brings me the data, as it is displayed in the excel result of the “excelExport” method, inside the Syncfusion Spreadsheet with similar formatting.

For example, there will be an “Export to Spreadsheet” button that calls this function and brings the data within the Pivot Table back into the destination Spreadsheet, as is done for Excel export.

How can I do this?


Thank you for your attention and help!


9 Replies 1 reply marked as answer

AP AngelinFaithSheeba PaulvannanRajadurai Syncfusion Team August 15, 2024 04:43 PM UTC

Hi Nick,


We understand that you want to render the Syncfusion Spreadsheet component using Pivot Table data upon a button click. To achieve this, you can export the Pivot Table as an Excel document through the toolbar UI, capture the blob data using the excelComplete event, and then use that data to render the Spreadsheet component.


In the code example below, enable the engine exporting using the allowEngineExport property within the dataBound event to process the blob data. Then, upon button click, the excelExport() method is triggered with the fourth parameter set to true to retrieve the blob data. The blob data is then captured in the exportComplete() event, and the Spreadsheet component is rendered using this data.


Code example:

  dataBound(): void {

    // Enable engine Exporting here.

    this.pivotObj.allowEngineExport = true;

  }

  exportComplete(argsany) {

    if (args.promise !== null) {

        args.promise.then((e=> {

          // Store the blob data of pivot table here.

          this.base64String = e.blobData;

          // Render the spreedsheet with the pivot blob data here.

          let fileFile = new File([this.base64String], 'Sample.xlsx');

          this.spreadsheetObj.open({ file: file });

        });

    }

  }

  exportToSpreadSheet() {

    // Need to pass "isBlob" (4th parameter of this method) as true to get the blob data.

    this.pivotObj.excelExport({}, falsenulltrue);

  }


Output screenshot:


Meanwhile, we have prepared a sample for your reference. In the below sample, click the "Export to SPREADSHEET" button to render the pivot values within Spreadsheet.


Sample: https://stackblitz.com/edit/angular-mg76sh-e7xxhg?file=src%2Fapp.component.ts,package.json,src%2Fapp.component.html


Please let us know if you have any concerns.


Regards,

Angelin Faith Sheeba.



NF Nick Fox August 15, 2024 11:09 PM UTC

Hi Angelin,

thank you so much for your reply! It is perfect!


However, I have one more question for you. In this way the excel export is done and the content of the exported file is loaded inside the spreadsheet. Is there any way to be able to take only the values from the table so that I can then place it from a cell of my choice?


I'll try to explain further. In my project I generate a Pivot Table and export its data inside a spreadsheet. Next, I generate a second Pivot Table and I would like the exported table to be put inside the same spreadsheet where the values from the first Pivot Table are also present. Basically, I would like, for example, the data from the first Pivot Table to be placed starting from cell B3, while the data from the second Pivot Table, for example, would be placed starting from cell H5. In what way could I achieve this?


I hope I have made myself clear, otherwise I will try to explain again!


Thank you very much in advance!

Nick



SK Sridhar Karunakaran Syncfusion Team August 16, 2024 04:29 PM UTC

Hi Nick,


Currently, we are preparing the sample to meet your requirement, and we will update the details by the next working day, August 19, 2024.


We appreciate your patience until then.


Regards,
Sridhar Karunakaran.



NF Nick Fox August 16, 2024 05:27 PM UTC

Hi Sridhar,

Thank you!


I take this opportunity to ask one last thing. Is it possible then to make the entire table uneditable except for a random row chosen during configuration? So keep the entire spreadsheet editable except for the cells that make up the exported table, but one row in the table will have to be editable.


Taking the example from the previous answer, then have the ability to edit only the cells associated with the "France" row and not the others. But still leaving the ability to edit the empty cells that make up the spreadsheet (e.g. A8, A9, B8, B9.


 I'll wait for your sample! Thank you again!


Regards,

Nick



ME Mohanraj Elumalai Syncfusion Team August 19, 2024 10:38 AM UTC

Hi Nick,


Thanks for the patience. Please find the response below for your queries.


Query

Comments

However, I have one more question for you. In this way the excel export is done and the content of the exported file is loaded inside the spreadsheet. Is there any way to be able to take only the values from the table so that I can then place it from a cell of my choice?

 

 

 

I'll try to explain further. In my project I generate a Pivot Table and export its data inside a spreadsheet. Next, I generate a second Pivot Table and I would like the exported table to be put inside the same spreadsheet where the values from the first Pivot Table are also present. Basically, I would like, for example, the data from the first Pivot Table to be placed starting from cell B3, while the data from the second Pivot Table, for example, would be placed starting from cell H5. In what way could I achieve this?

We understand that you need to display multiple pivot table values on the same spreadsheet page. To achieve this, you can use the pivotValues property, which holds the pivot cell value information. In the following code example, we use an external button click to retrieve the pivot cell information from the first pivot table (accessed via the pivot instance, this.pivotObj). After gathering the pivot values, we extract the relevant cell data, structure it similarly to the spreadsheet’s cell format, and insert it into the spreadsheet component using the updateCell method, based on the row and column indices of the pivot cells. Once the first pivot table’s values are rendered, we move on to render the second pivot table’s values in the columns that follow the last column of the first pivot table.

 

Code example:

    document.getElementById('data').onclick = () => {

      var rowIndex = 0;

      var colIndex = 0;

      // Here we insert the first pivot table values

      this.pivotObj.pivotValues.forEach((row) => {

        row.forEach((cell) => {

          var data = { value: cell.formattedText };

          this.spreadsheetObj.updateCell(data, getCellAddress(cell.rowIndex, cell.colIndex));

          rowIndex = cell.rowIndex;

          colIndex = cell.colIndex;

        });

      });

      // Here, we place the values of the second pivot table in the column immediately following the last column of the first pivot table. However, you can adjust the column index to suit your specific needs.

      this.pivotObj1.pivotValues.forEach((row) => {

        row.forEach((cell) => {

              var data = { value: cell.formattedText };

              this.spreadsheetObj.updateCell(data, getCellAddress(cell.rowIndex, colIndex + 2 + cell.colIndex));

          });

      });

 

Output Screenshots:

Pivot Table:

 

Spreadsheet

 

 

Meanwhile, we have prepared a sample for your reference. You can find the sample at the following link.

 

Sample: https://stackblitz.com/edit/angular-mg76sh-ha5yv6?file=src%2Fapp.component.ts,src%2Fapp.component.html,src%2Fapp%2Fapp.config.ts

 

I take this opportunity to ask one last thing. Is it possible then to make the entire table uneditable except for a random row chosen during configuration? So keep the entire spreadsheet editable except for the cells that make up the exported table, but one row in the table will have to be editable.

 

 

 

Taking the example from the previous answer, then have the ability to edit only the cells associated with the "France" row and not the others. But still leaving the ability to edit the empty cells that make up the spreadsheet (e.g. A8, A9, B8, B9.

We understand that your requirement is to make only one row editable while keeping the remaining cells in the pivot table non-editable. If this is the case, you can achieve it by using the drillThrough event to prevent cell editing based on a specific condition. In the code example below, we have restricted editing for all pivot table rows except the France row. Please refer to the code example below for your reference.

 

Code example:

drillThrough(args) {

    if (!args.rowHeaders.includes('France')) {

      args.cancel = true;

    }

  }

 

You can also restrict the editing of spreadsheet cells by using the isReadOnly property to prevent editing based on specific conditions when loading the pivot table values into the spreadsheet. In the code example below, we have restricted editing for all spreadsheet rows rendered by the pivot table values, except for the France row, while keeping the remaining spreadsheet rows (empty cells) editable. Please refer to the code example below for reference.

 

Code example:

document.getElementById('data').onclick = () => {

      var rowIndex = 0;

      var colIndex = 0;

      this.pivotObj.pivotValues.forEach((row) => {

        row.forEach((cell) => {

          var data = {

            value: cell.formattedText,

            isReadOnly: (cell.valueSort && cell.valueSort.levelName && (cell.valueSort.levelName as string).includes('France')) || (cell.rowHeaders && (cell.rowHeaders as string).includes('France')) ? false : true,

          };

        });

      });

      this.pivotObj1.pivotValues.forEach((row) => {

        row.forEach((cell) => {

          var data = {

            value: cell.formattedText,

            isReadOnly: (cell.valueSort && cell.valueSort.levelName && (cell.valueSort.levelName as string).includes('France')) || (cell.rowHeaders && (cell.rowHeaders as string).includes('France')) ? false : true,

          };

        });

      });

 

 

Please refer to the below UG document to know more about to make spreadsheet cells read-only.

https://ej2.syncfusion.com/javascript/documentation/spreadsheet/protect-sheet#make-cells-read-only-without-protecting-worksheet

 

Meanwhile, we have prepared a sample for your reference. You can find the sample at the following link.

 

Sample: https://stackblitz.com/edit/angular-mg76sh-ha5yv6?file=src%2Fapp.component.ts,src%2Fapp.component.html,src%2Fapp%2Fapp.config.ts


Additionally, If you have any further requirements, you can use the spreadsheet functionality to customize the provided sample. For more information about the spreadsheet component and its feature, please refer to the documentation below.


Document: https://ej2.syncfusion.com/javascript/documentation/spreadsheet/overview


Please let us know if you have any concerns.


Regards,

Mohanraj Elumalai.



NF Nick Fox August 19, 2024 06:29 PM UTC

Hi Mohanraj,

thank you for the sample! It's a good starter point for what I need and everything works fine!


However, I encountered a problem. I was able to set the editability of the cells according to my configurations, however when I export the excel file and then import it again into the spreadsheet, that configuration is missing. So I can, if I want, edit the cells that are read-only.


Is there any way to be able to keep this configuration even after exporting?


Thank you!

Regards,

Nick.



SK Sridhar Karunakaran Syncfusion Team August 20, 2024 03:05 PM UTC

Hi Nick,

As we mentioned in our previous response, you can restrict the editing of spreadsheet cells based on specific conditions by using the isReadOnly property when loading pivot table values into the spreadsheet. We have already provided a solution with an sample in the second query of our last response. However, we are sharing the sample again for your reference.

Samplehttps://stackblitz.com/edit/angular-mg76sh-ha5yv6?file=src%2Fapp.component.ts,src%2Fapp.component.html,src%2Fapp%2Fapp.config.ts

In the meantime, if you want to restrict cell editing to specific rows while loading the Spreadsheet component with the exported Excel blob data from the pivot table, you can use the beforeCellRender event in the Spreadsheet component. In the code example below, within  the beforeCellRender event, we verify if the current cell is part of the "France" row. If this condition is met, we prevent editing of the spreadsheet cell by setting "args.cell.isReadOnly" to true.

Code example:

  beforeCellRender(args) {

    if(args.row) {

      // Here we obtain the row header name (i.e., France)

      var rowHeader = args.row.querySelectorAll('td')[0].innerText;

      if(rowHeader == "France") {

        // If it does we restrict the cell editing

        args.cell.isReadOnly = true;

      }

    }

  }


Output GIF Image:


Meanwhile, we have prepared a sample for your reference. You can find the sample at the following link.
Sample: https://stackblitz.com/edit/angular-mg76sh-ghm8ko?file=src%2Fapp.component.ts,package.json,src%2Fapp.component.html

Please let us know if you have any concerns.

Regards,
Sridhar Karunakaran


Marked as answer

NF Nick Fox August 21, 2024 04:03 PM UTC

Hi Sridhar,

thank you so much for the help! Your answer is perfect again!


Thank you again!


Best regards,

Nick



SK Sridhar Karunakaran Syncfusion Team August 22, 2024 04:16 PM UTC

Hi Nick,


Thank you for your update. We are delighted to hear that you are satisfied with the solution we provided. If you have any further questions or need assistance, please don't hesitate to contact us. We are here to help!


Best regards,

Sridhar Karunakaran.


Loader.
Up arrow icon