DEV Community

Ryan Haley
Ryan Haley

Posted on

Copying Data in Google Sheets

Note: This is meant as a quick-and-dirty guide to getting up and running with a Google Sheets macro. I don't claim that this is enough to keep things well organized.

  1. Head to script.google.com
  2. Click "New Project"
  3. Click "Untitled project" at the top and give your project a descriptive name
  4. Modify the following to suit your needs and paste it into the editor:

     function copyData() {
      // This will grab the currently active spreadsheet, which will be the one
      // you're looking at when you run the script.
      var ss = SpreadsheetApp.getActive();
    
      // This is the source sheet/tab. Change Sheet1 to the name of the source
      // sheet.
      var sourceSheet = ss.getSheetByName('Sheet1');
    
      // This is the destination sheet/tab. Change Sheet2 to the name of the destination sheet.
      var destSheet = ss.getSheetByName('Sheet2');
    
      // This is the range that you'll be copying from the source. Change A:A
      // to be whatever the valid A1 notation range should be.
      var sourceRange = sourceSheet.getRange("A:A");
    
      // This is the range that you'll be copying from the source. Change A:A
      // to be whatever the valid A1 notation range should be.
      var destRange = destSheet.getRange("A:A");
    
      // Do the actual copying
      sourceRange.copyTo(destRange, SpreadsheetApp.CopyPasteType.PASTE_NORMAL);
    }
    
  5. Hit Ctrl+s to save

  6. Open your spreadsheet

  7. Click Tools -> Macros -> Import

  8. Click "Add Function"

  9. Close the dialog window

You can now use the function you created by clicking Tools -> Macros -> copyData (or whatever you renamed the function to).

There are a lot of other tricks you can use, like creating a trigger to run the function when the sheet changes, setting a keyboard shortcut, etc. but those are outside the scope of this post.

Top comments (1)

Collapse
 
meeladmashaw profile image
Meelad Mashaw

Perfect, just what I needed! Thank you.
Cheers,
Meelad
Apipheny