How to get cell value in spreadSheet?

I have 2 sheets, sheet_a and sheet_b.

In sheet_b, cell A2 has a value of 20. How can I, in sheet_a, retrieve the value of cell A2 from sheet_b? After obtaining this value, I want to check if sheet_b!A2 > 10. If true, I would like to display a specific value in cell A2 of sheet_a.

My expectation is to have a function like getCellValue to be able to retrieve the value from sheet_b in sheet_a.


1 Reply

SP Sangeetha Priya Murugan Syncfusion Team November 27, 2023 03:50 PM UTC

Hi Sum,


Suggestion 1:


Your requirement can be achieved by using the formula reference in spreadsheet, you can refer the cells from different sheets and display the value based on your need. For your convenience we have prepared the sample that refers the cell from second sheet and display the own value based on true/false condition in first sheet.


Code Block:


 <CellDirective

                      formula='=IF(Sheet_b!F2 > 10, "true case", "false case")'

                      style={bold}

                    ></CellDirective>

 


Sample Link: https://stackblitz.com/edit/react-aih3qp-zqdq9k?file=index.js


Documentation Link: https://ej2.syncfusion.com/react/documentation/spreadsheet/formulas


Demos Link: https://ej2.syncfusion.com/react/demos/#/material3/spreadsheet/formula


Suggestion 2:


Or else you can also achieve your requirement using getCell and updateCell method as shown below.


Code Block:


 function onCreated() {

    let sheet = spreadsheet.sheets[1]; // fetch the second sheet model

    let range = getRangeIndexes('Sheet_b!F2'); // pass the address

    let cell = getCell(range[0], range[1], sheet); // returns cell Model

    console.log(cell);

    // Update the value in 1st sheet based on the secomd sheet cell model value using updateCell method.

    spreadsheet.updateCell(

      cell.value > 10

        ? { value: 'Custom Value1' }

        : { value: 'custom value 2' },

      'Sheet_a!B1'

    );

    spreadsheet.activeSheetIndex = 0;

  }

 


Sample Link: https://stackblitz.com/edit/react-aih3qp-kudkun?file=index.js


API Link:

https://ej2.syncfusion.com/react/documentation/api/spreadsheet/#updatecell


Loader.
Up arrow icon