How to copy data between sheets in Google Sheets like Excel's Range.Value?

I’m working on a sales form in Google Sheets and need to move data to a separate database sheet. In Excel, I’d typically use the Range.Value function to do this. But I’m not sure how to achieve the same result in Google Sheets.

Here’s what I want to do:

function copyData() {
  var sourceSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('SalesForm');
  var targetSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Database');
  
  // How do I copy values from SalesForm to Database?
  // Something like: targetSheet.getRange('A1').setValue(sourceSheet.getRange('A1').getValue());
}

Is there a simple way to copy data between sheets in Google Sheets, similar to Excel’s Range.Value? Any help would be great!

In Google Sheets, you can efficiently copy data between sheets using the getValues() and setValues() methods. Here’s a more complete example based on your code:

function copyData() {
  var sourceSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('SalesForm');
  var targetSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Database');
  
  var sourceRange = sourceSheet.getRange('A1:Z100'); // Adjust range as needed
  var sourceValues = sourceRange.getValues();
  
  targetSheet.getRange(1, 1, sourceValues.length, sourceValues[0].length).setValues(sourceValues);
}

This script copies a range from ‘SalesForm’ to ‘Database’. Modify the range to suit your specific requirements. The setValues() method automatically adjusts to the size of the data being copied, making it quite versatile.

hey there! i’ve done this before. u can use getValues() and setValues() methods. somethin like:

sourceValues = sourceSheet.getRange(‘A1:B10’).getValues();
targetSheet.getRange(‘A1:B10’).setValues(sourceValues);

this copies a range. adjust it to fit ur needs. hope it helps!

I’ve faced similar challenges when transitioning from Excel to Google Sheets. One approach that’s worked well for me is using the getRange().copyTo() method. It’s straightforward and efficient:

function copyData() {
  var sourceSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('SalesForm');
  var targetSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Database');
  
  var sourceRange = sourceSheet.getRange('A1:D10'); // Adjust as needed
  sourceRange.copyTo(targetSheet.getRange('A1'), {contentsOnly: true});
}

This copies the specified range from ‘SalesForm’ to ‘Database’, starting at cell A1. The {contentsOnly: true} option ensures only values are copied, not formatting. It’s been a reliable solution for me, especially when dealing with larger datasets. Remember to adjust the range to fit your specific form layout.