We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

Insert row with previous row formulas

Dear Syncfuison Team,

I have to copy a spreadsheet formula to a newly inserted cell. 
I have tried to achieve it by using the context menu and binding to the on click action. I check if the click was an insert and I am trying get the formula from the previous row but I see no methods anywhere to get the formula from a cell.
Is there a way to get the formula of the previous row and use it to set the next row? or insert the next row and have it have the all previous rows formulas?

Thanks, 

Rafael

1 Reply

VR Vasanth Ravi Syncfusion Team January 16, 2023 01:59 PM UTC

Hi Rafael,


We have checked your requirement and prepared a sample to achieve it. We have triggered the actionComplete event and made  condition to check the action done. Then looped with the selected row and updated the formula present in the previous row to the newly inserted row.


Please find the code block and the sample for your kind reference.


CODE BLOCK:


public actionComplete(args) {

    if (args.action === "insert"){

      let sheet = this.spreadsheetObj.getActiveSheet();

      let selectedRange = sheet.selectedRange;

      let index = getRangeIndexes(selectedRange);

      let prevRowIdx; let newRowIdx;

      if (args.eventArgs.modelType === 'Row' && args.eventArgs.insertType == 'below') {

        newRowIdx = index[0] + (index[2] - index[0]) + 1;

      }

      prevRowIdx = newRowIdx - 1;

      let rowData = this.spreadsheetObj.sheets[sheet.index].rows[prevRowIdx].cells;

      for(let i = 0; i < rowData.length; i++){

        if(isNullOrUndefined(rowData[i]) || !isNullOrUndefined(rowData[i].formula)){

          let address = getCellAddress(newRowIdx, i);

          this.spreadsheetObj.updateCell({formula: rowData[i].formula}, address);

        }

      }

    }

  }

 


Stackblitz Sample: https://stackblitz.com/edit/angular-7cwi7b-pq1hvw?file=app.component.ts


Get back to us if you need further assistance regarding.


Loader.
Live Chat Icon For mobile
Up arrow icon