Skip to content

Diagnostic reference

Target range and source range must be on the same spreadsheet

Fix Exception: Target range and source range must be on the same spreadsheet in Google Apps Script using getValues/setValues or Sheet.copyTo().

Updated December 6, 2026

Apps Script error

Exception: Target range and source range must be on the same spreadsheet.

What it means

Range.copyTo() only copies cells between ranges inside the same spreadsheet file. When the source range comes from one Spreadsheet (for example getActiveSpreadsheet()) and the destination range comes from another Spreadsheet opened with openById() or create(), Apps Script throws this error.

Related error messages

  • Exception: Target range and source range must be on the same spreadsheet. (Range.copyTo)

Before and after

The code before and after the fix.

Problem 2 changed lines
function copySummaryToArchive(archiveId) {
  const srcSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Summary');
  const dstSheet = SpreadsheetApp.openById(archiveId).getSheetByName('Archive');
  if (!srcSheet || !dstSheet) throw new Error('Missing source or archive sheet.');
  // Fails because srcSheet and dstSheet belong to different spreadsheets.
  srcSheet.getDataRange().copyTo(dstSheet.getRange('A1'));
}
Fixed 4 changed lines
function copySummaryToArchive(archiveId) {
  const srcSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Summary');
  const dstSheet = SpreadsheetApp.openById(archiveId).getSheetByName('Archive');
  if (!srcSheet || !dstSheet) throw new Error('Missing source or archive sheet.');

  const values = srcSheet.getDataRange().getValues();
  if (values.length === 0) return;
  dstSheet.getRange(1, 1, values.length, values[0].length).setValues(values);
}

How to fix it

  • To copy values between two spreadsheets, read a 2D array with sourceRange.getValues() and write it with targetSheet.getRange(1, 1, values.length, values[0].length).setValues(values).
  • To copy an entire tab including formatting and formulas into another workbook, call sourceSheet.copyTo(destinationSpreadsheet) instead of Range.copyTo().
  • If you also need cell backgrounds, font weights, or number formats on a sub-range, copy getBackgrounds(), getFontWeights(), and getNumberFormats() alongside getValues().

Things to check

  • Opening another spreadsheet with SpreadsheetApp.openById() requires the full spreadsheets OAuth scope and will fail if the project uses /** @OnlyCurrentDoc */.
  • myFunction catches cross-spreadsheet Range.copyTo() calls statically under rule MF94012.

In the Apps Script editor

Catch errors like this before clicking Run.

myFunction (published as Error Checker for Google Apps Script on the Chrome Web Store) checks your code while you edit at script.google.com. Diagnostics, autocomplete, and rule-based quick fixes are free without an account, run in your browser, and request no Google Drive, Gmail, or Apps Script project OAuth scopes. Writing Apps Script with an AI coding agent? Connect the Apps Script MCP server to run the same static checks before pasting code.