Felipe
Felipe

Reputation: 101

How to copy cells from one spreadsheet and paste it to another

I've been trying to copy a range from a spreadsheet and paste into another spreadsheet on Google Docs. Whenever I run the script I get Target range and source range must be on the same spreadsheet

function myFunction() {
var ss1 = SpreadsheetApp.getActiveSpreadsheet ();
var ss2 = SpreadsheetApp.openById("1wIVQA-QhgSvTpmrIl4R6Gwhh1pMKcVKEcdFPa8_KTXw");
var source = ss2.getRange ("Moz!D2:J2");
var destiny = ss1.getRange("Sheet1!A2:B2");
source.copyTo (destiny, {contentsOnly: true});
source.clear ();
}

Apparently is possible but I couldn't work it out yet. Source: https://developers.google.com/apps-script/reference/spreadsheet/spreadsheet-app

Upvotes: 0

Views: 204

Answers (1)

opowell
opowell

Reputation: 587

Try:

function myFunction() {
  var ss1 = SpreadsheetApp.getActiveSpreadsheet ();
  var ss2 = SpreadsheetApp.openById("1wIVQA-QhgSvTpmrIl4R6Gwhh1pMKcVKEcdFPa8_KTXw");
  var source = ss2.getSheetByName("Moz").getRange("D2:E2");
  var sourceValues = source.getValues();
  var destiny = ss1.getSheetByName("Sheet1").getRange("A2:B2");
  destiny.setValues(sourceValues);
  source.clear();
}

Upvotes: 1

Related Questions