How to transfer cell ranges between sheets using Google Apps Script?

Hey everyone! I’m trying to figure out how to move data between sheets using Google Apps Script. I’ve got a 4x2 block of cells (C14:F15) on one sheet, and I want to copy those values to B13:E14 on another sheet. I know how to move single cell values, but I’m stumped when it comes to ranges. Any tips or code examples would be super helpful! I’ve been scratching my head over this for a while now. Thanks in advance for any advice you can share!

hey alice, i’ve done this before! try something like:

var sourceRange = sourceSheet.getRange(‘C14:F15’);
var values = sourceRange.getValues();
targetSheet.getRange(‘B13:E14’).setValues(values);

this should copy the data over. hope it helps!

I’ve encountered this scenario in my work with Google Sheets. Here’s a robust method I’ve found effective:

function transferRange() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sourceSheet = ss.getSheetByName(‘SourceSheetName’);
var targetSheet = ss.getSheetByName(‘TargetSheetName’);

var sourceRange = sourceSheet.getRange(‘C14:F15’);
var targetRange = targetSheet.getRange(‘B13:E14’);

sourceRange.copyTo(targetRange, {contentsOnly: true});
}

This approach uses copyTo() which is more versatile than setValues(). It allows you to specify options like preserving formatting if needed. Just replace ‘SourceSheetName’ and ‘TargetSheetName’ with your actual sheet names. Remember to run this function or call it from a trigger for automation.

I’ve faced a similar issue in my projects and found a simple solution that works well. I usually begin by assigning both the source and target sheets using their names with getSheetByName. After that, I retrieve the range from the source sheet using getRange(‘C14:F15’) and obtain its values. Those values are then transferred to the target sheet by selecting the range with getRange(‘B13:E14’) and applying setValues. This approach reliably copies the data, including formulas and formatting, and I always recommend verifying the sheet names and ranges to avoid any errors.