How to Copy and Paste Values Using Apps Script in Google Sheets

It’s simple to copy and paste values into Google Sheets. We can use the keyboard shortcut and other commands for doing so. But there is a distinctive feature in Google Sheets like the Apps Script. We can use this extension to insert some codes and perform amazing things. For instance, I can automatically copy and paste values through it. If I want Google Sheets to complete the task at a specific time, I could also add some Triggers in our code. So, in this article, we’ll see 4 useful ways to copy and paste values using Apps Script in Google Sheets with clear images and steps. And lastly, we’ll get an output like this from our article.

How to Copy and Paste Values Using Apps Script in Google Sheets


A Sample of Practice Spreadsheet

You can download Google Sheets from here and practice very quickly.


4 Useful Apps Scripts to Copy and Paste Values in Google Sheets

Let’s get introduced to our dataset first. Here we have some products in Column B. Now we want to copy and paste these products into another column automatically without using any command or function. We can do that by using the Apps Script extensions. So I’ll show you 4 useful ways to copy and paste values using Apps Script in Google Sheets by using this dataset.

Dataset to Copy and Paste Values Using Apps Script in Google Sheets


1. Copying and Pasting Manually

We can copy and paste values manually by using Apps Script in Google Sheets. Here we just have to run the script code again and again whenever we want to copy and paste any values. Let’s see the steps.

Steps:

  • Firstly, rename the sheet as “Dataset 2”.
  • Secondly, go to Extensions > Apps Script.

Selecting Apps Script from Extensions Menu

  • Thirdly, insert the following code there-
function PasteValue(){
var cp = SpreadsheetApp.getActiveSpreadsheet();
var copypaste = cp.getSheetByName("Dataset 2");
copypaste.getRange("B4:B").copyTo(copypaste.getRange("D4:D"),{contentsOnly:true});
}
  • Then, rename the script as “Copy and Paste” and the file as “Manual”.
  • Next, click on the Save button and press Run.

Inserting Code in Apps Script

Code Breakdown

  • Here we have used only one function called PasteValue.
  • This function directly copies any values in the given range from Column B and from the given sheet which is Dataset 2.
  • Then it pastes the values in the given range of Column D in the same sheet.
  • Finally, you’ll see that all the values from Column B are pasted into Column D.
  • Output of Copying and Pasting Manually Using Apps Script

    Read More: How to Paste Space Separated Values in Google Sheets (2 Ways)


    2. Generating Copy and Paste Button

    Apart from the previous method, we can also generate a copy-and-paste button using Apps Script in Google Sheets. The advantage of this method is that we don’t have to run the code repeatedly. If we click on this button, the Apps Script will automatically copy and paste our desired values. Let’s see how to do it.

    Steps:

    • First, give the sheet a new name, “Dataset 3”.
    • Go to Extensions > Apps Script next.

    Selecting Apps Script from Extensions Menu

    • Then, put the code in the code box-
    function onOpen(){
    var cp = SpreadsheetApp.getActiveSpreadsheet();
    var entry = [];
    entry.push({name: "Paste", functionName: "PasteValue"});
    cp.addMenu("Copy", entry);
    }
    function PasteValue(){
    var cp = SpreadsheetApp.getActiveSpreadsheet();
    var copypaste = cp.getSheetByName("Dataset 3");
    copypaste.getRange("B4:B").copyTo(copypaste.getRange("D4:D"),{contentsOnly:true});
    }
    • Moreover, rename the script and file to “Copy and Paste” and “Generating Button” respectively.
    • Next, select the Save option.
    • Select onOpen once again then hit Run.

    Inserting Code in Apps Script

    Code Breakdown

    • Here we have used 2 functions called onOpen and PasteValue.
    • The onOpen function creates a new menu in the current sheet called Copy and a submenu under it called Paste.
    • The PasteValue function works the same as method 1.
  • After that, you’ll see a new menu appearing titled “Copy”. Under the Copy menu, there is another menu titled Paste. These 2 menus will work like an automatic button to copy and paste values.
  • So, click on the Copy and Paste buttons serially.
  • Generating Copy and Paste Button

    • In the end, you’ll notice that Column D now has all of the data from Column B.

    Output of Generating Copy and Paste Button Using Apps Script

    Read More: [Solved!] Paste Values Is Not Working in Google Sheets (2 Fixes)


    3. Automatically Copy and Paste at Specific Time

    Now, we’ll further extend our previous example. We want to copy and paste values at a specific time say every week automatically. In order to do that we have to add some Triggers to our Apps Script code. This Trigger will enable our code to run automatically and produce updated results every week at a specific time.

    Steps:

    function PasteValue(){
    var cp = SpreadsheetApp.getActiveSpreadsheet();
    var copypaste = cp.getSheetByName("Dataset 2");
    copypaste.getRange("B4:B").copyTo(copypaste.getRange("D4:D"),{contentsOnly:true});
    }
    • After that, name the script “Copy and Paste” and the script’s file “Manual”.
    • Next, select the Save button and then press Run.

    Inserting Code in Apps Script

    • Then, go to Triggers under the Apps Script window.

    Selecting Trigger Under Apps Script

    • Next, click on the Add Trigger button at the bottom side of the Trigger window.

    Adding Trigger

    • Add Trigger for Copy and Paste window will open.
    • Then, choose PasteValue under Choose which function to run menu.
    • After that, scroll down for more options.

    Selecting Options Under Add Trigger for Copy and Paste Window

    • Thereafter, select Time-driven under the Select event source menu.
    • Then, choose the Week Timer because we want to copy and paste values every week.
    • After that, we pick Sunday as our day and gave the time from 10 am to 11 am. The time depends on your Google Sheets timing.
    • Lastly, click on the Save button.

    Selecting Options Under Add Trigger for Copy and Paste Window

    • At last, you’ll notice that every Sunday between 10 and 11 am, all the data from Column B are automatically pasted into Column D.

    Results of Automatically Copy and Paste at Specific Time Using Apps Script

    Read More: How to Paste Comma Separated Values in Google Sheets


    4. Copying and Pasting to Another Sheet

    Sometimes we need to copy some values from a sheet and paste it into another sheet in the same Google Sheets. We can also do that by using the Apps Script code. Below we’ll see the process.

    Steps:

    • Earlier on open a new sheet in the same Google Sheets and rename the sheet as “Dataset 5”.
    • Then, go to Extensions > Apps Script.

    Selecting Apps Script from Extensions Menu

    • Next, type the following code there-
    function sendDataToDestinationSpreadSheet() {
    var currentSheet = SpreadsheetApp.getActiveSpreadsheet();
    var mainSheet = currentSheet.getSheetByName('Dataset');
    var mainRange = mainSheet.getRange('B4:B');
    var mainValues = mainRange.getValues();
    var targetSheet = currentSheet.getSheetByName('Dataset 5');
    for(var u = 0; u < mainValues.length; u++) {
    var firstcolumn = mainValues[0,u][0];
    targetSheet.getRange(u+4, 2).setValue(firstcolumn);
    }
    }
    • Again, change the script’s name to “Copy and Paste” and the file’s name to “Another Sheet”.
    • Consequently, select Run after selecting the Save button.

    Inserting Code in Apps Script

    Code Breakdown

    • Here the function name is sendDataToDestinationSpreadSheet.
    • This function directly copies any values in the given range from Column B and from the current sheet which is Dataset.
    • Then it pastes the values in the given range of Column D in the new sheet called Dataset 5.
  • At last, you’ll notice that all the numbers have finally been put into Column D of the new sheet, Dataset 5.
  • Output of Copying and Pasting to Another Sheet Using Apps Script


    Conclusion

    That’s all for now. Thank you for reading this article. In this article, I have discussed 4 useful ways to copy and paste values using Apps Script in Google Sheets. Please comment in the comment section if you have any queries about this article. You will also find different articles related to google sheets on our officewheel.com. Visit the site and explore more.

    Sajid Ahmed

    Sajid Ahmed

    Hi! This is Sajid Ahmed. At present, I am working as an Executive, Content Development at OfficeWheel.com. Here I will be posting articles related to different problems of Google Sheets. I have completed my graduation from Bangladesh University of Engineering and Technology in Naval Architecture and Marine Engineering. Despite a different background, I have a great interest in the research and development of data science. I try to make innovative solutions to different problems regarding Google Sheets. Please follow my blog at Officewheel.com to get different solutions regarding Google Sheets.

    We will be happy to hear your thoughts

    Leave a reply

    OfficeWheel
    Logo