Search code examples
google-apps-scriptgoogle-sheetsgoogle-sheets-macros

I need my Macros "Save" button to add a new row every time new data is saved, not replace the current one that was saved before


I just need for my Macro to save the information and add a new row to another sheet within the same workbook without replacing the old saved data but instead add a new row.

I am very new at this and I need expert help, thank you to everyone who is willing to help.

    enter code here

function CopyandPaste() {
  var spreadsheet = SpreadsheetApp.getActive();
  spreadsheet.getRange('J3:N3').activate();
  spreadsheet.setActiveSheet(spreadsheet.getSheetByName('Work Log Data'), true);
  spreadsheet.getRange('A3:E3').activate();
  spreadsheet.getRange('\'Customer Interface\'!J3:N3').copyTo(spreadsheet.getActiveRange(), SpreadsheetApp.CopyPasteType.PASTE_VALUES, false);
};

I need for all of the data copied from range J3:N3 to be copied into Sheet "Customer interface" without replacing the information copied before, by adding a new row every time the "save" button is clicked. Thanks!!


Solution

  • I think this is what you want. But it's hard to tell. Macros may be easy to make but they make poorly documented scripts.

    function CopyandPaste() {
      var ss = SpreadsheetApp.getActive();
      ss.getRange('J3:N3').activate();
      ss.setActiveSheet(ss.getSheetByName('Work Log Data'), true);
      ss.getRange('A3:E3').activate();
      ss.getRange('\'Customer Interface\'!J3:N3').copyTo(ss.getActiveRange(), SpreadsheetApp.CopyPasteType.PASTE_VALUES, false);
      var sh=ss.getSheetByName('Customer Interface');
      var rg=sh.getRange('J3:N3');
      var values=rg.getValues();
      sh.appendRow(values[0]);
    }
    

    Version Edited by OP:

    function CopyandPaste() { 
      var ss = SpreadsheetApp.getActive(); 
      ss.getRange('J3:N3').activate(); 
      ss.setActiveSheet(ss.getSheetByName('Work Log Data'), true); 
      ss.getRange('A3:E3').activate(); 
      ss.getRange('\'Customer Interface\'!J3:N3').copyTo(ss.getActiveRange(), 
      SpreadsheetApp.CopyPasteType.PASTE_VALUES, false); 
      var sh=ss.getSheetByName('Work Log Data'); 
      var rg=sh.getRange('A3:E3'); 
      var values=rg.getValues(); 
      sh.appendRow(values[0]); 
    } 
    

    In answer to your question in the comments:

    function CopyandPaste() { 
      var ss = SpreadsheetApp.getActive(); 
      ss.getRange('J3:N3').activate(); 
      ss.setActiveSheet(ss.getSheetByName('Work Log Data'), true);
      ss.getRange('A3:E3').activate(); 
    
      //this copies from 'Customer Interface'!J3:N3 to 'Work Log Data'!A3:E3
      ss.getRange('\'Customer Interface\'!J3:N3').copyTo(ss.getActiveRange(), SpreadsheetApp.CopyPasteType.PASTE_VALUES, false);
    
    
      var sh=ss.getSheetByName('Work Log Data'); 
      var rg=sh.getRange('A3:E3'); 
      var values=rg.getValues(); 
      //this append 'Work Log Data'!A3:E3 to the bottom of the 'Work Log Data'
      sh.appendRow(values[0]);
    }