Copy data from one sheet to another within the same Google Sheet using Apps Script

To copy data from one sheet to another within the same Google Sheet using Apps Script, you can use the getRange() and setValues() methods of the Sheet class. Here’s an example code snippet:

function copyData() {
  var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
  var sourceSheet = spreadsheet.getSheetByName('SourceSheet'); // Replace with the name of the source sheet
  var targetSheet = spreadsheet.getSheetByName('TargetSheet'); // Replace with the name of the target sheet
  
  var sourceRange = sourceSheet.getDataRange();
  var targetRange = targetSheet.getRange(1, 1, sourceRange.getNumRows(), sourceRange.getNumColumns());
  
  var values = sourceRange.getValues();
  targetRange.setValues(values);
}

In this code, the copyData function copies data from the source sheet to the target sheet within the same Google Sheet. You need to replace 'SourceSheet' and 'TargetSheet' with the actual names of your source and target sheets.

The getSheetByName() method is used to obtain references to the source and target sheets based on their names.

The getDataRange() method is called on the source sheet’s object to retrieve the range of cells that contain data. This range includes all the cells with data in the source sheet.

The getRange() method is called on the target sheet’s object to obtain a range with the same dimensions as the source range. The getRange() method accepts four parameters: startRow, startColumn, numRows, and numColumns. In this example, we specify 1, 1 as the starting position of the target range, and use sourceRange.getNumRows() and sourceRange.getNumColumns() to determine the number of rows and columns to copy.

The getValues() method is called on the source range object to retrieve the values of the cells in the source range. The values are stored in the values variable.

The setValues() method is called on the target range object, and the values variable is passed as an argument. This method sets the values of the cells in the target range to the values from the source range, effectively copying the data.

You can call the copyData function from another function or set up triggers to execute it based on your needs. When executed, it will copy the data from the source sheet to the target sheet within the same Google Sheet.

Feel free to modify the code to suit your specific requirements, such as copying data from specific ranges, copying formatting along with the data, or performing additional operations before or after the data copying process.

Pamai Tech
Turning ideas into Reality

Products

Office Add-in

Enterprise Solutions

Cloud Consulting

UI UX Design

Data Transformation

Services

FAQ's

Privacy Policy

Terms & Condition

Team

Contact Us

Company

About Us

Services

Features

Our Pricing

Latest News

© 2023 Pamai Tech