Home > Net >  How to run script on specific sheet and insert a formula
How to run script on specific sheet and insert a formula

Time:09-22

I'm new and started learning to code this year.

I am trying to insert a formula into a specific sheet. The script is working and I am able to insert a new row and the formula, however the script runs even when I edit a different sheet. I only want it to run when cell A2 is edited on "Test" sheet.

Here is my code:

    function onEdit(e) {
      insertRow(e);
      copyFormulas(e);


    function insertRow(e) {
    // get sheet
    var sheet = SpreadsheetApp.getActive().getSheetByName("Test");

    // Get the edited range
    var editedRange = e.range;

      if (editedRange.getA1Notation() === "A2") {
        // check 
          sheet.insertRowBefore(2);
         }
      }
   function copyFormulas(e) {
   SpreadsheetApp.getActive().getSheetByName('Test').getRange('C3').setFormula("=SUM(A3*B3)");

     }

     }

LINK to the sheet enter image description here

  • Related