skip to Main Content

Is there any way to apply this function to a sheet named "Production Log"?

function CopyDatatoLog() {
  var spreadsheet = SpreadsheetApp.getActive().getSheetByName('Production Log');
  spreadsheet.getRange('B8:B11').activate();
  spreadsheet.getActiveSheet().insertRowsBefore(spreadsheet.getActiveRange().getRow(), 4);
  spreadsheet.getActiveRange().offset(0, 0, 4,            spreadsheet.getActiveRange().getNumColumns()).activate();
  spreadsheet.getRange('A8:BL11').activate();
  spreadsheet.getRange('4:7').copyTo(spreadsheet.getActiveRange(), SpreadsheetApp.CopyPasteType.PASTE_VALUES, false);
};

I keep getting this error "TypeError: spreadsheet.getActive sheet is not a function". I’m not sure what I’m missing here. This function runs on a trigger and adds 4 rows above a specific row and then copy’s and pastes 4 rows of data (just the values) into the added rows. This code does work in the sheet as long as i’m active in it but as soon as I added in the getSheetByName, It stopped working. If any one has any advice or can point me in the right direction, I would greatly appreciate it!

2

Answers


  1. The problem is with this line:

    spreadsheet.getActiveSheet().insertRowsBefore(spreadsheet.getActiveRange().getRow(), 4);
    

    Change it to

    spreadsheet.insertRowsBefore(spreadsheet.getActiveRange().getRow(), 4);
    

    and the issue should be resolved.

    Login or Signup to reply.
  2. Try it this way

    function CopyDatatoLog() {
      const ss = SpreadsheetApp.getActive();
      var sh = ss.getSheetByName('Production Log');
      sh.getRange('B8:B11').activate();
      ss.getActiveSheet().insertRowsBefore(sh.getActiveRange().getRow(), 4);
      sh.getActiveRange().offset(0, 0, 4, sh.getActiveRange().getNumColumns()).activate();
      sh.getRange('A8:BL11').activate();
      sh.getRange('4:7').copyTo(sh.getActiveRange(), SpreadsheetApp.CopyPasteType.PASTE_VALUES, false);
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search