Copy and paste into different sheets in Google Sheets

0

I'm trying to copy a value from one table and send it to another using the Google Sheets script, but I get the following error 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 ();
}
    
asked by anonymous 23.09.2016 / 17:17

1 answer

2

Use the IMPORTRANGE(spreadsheet_key, range_string) function of Google Sheets itself.

Link to help you: link

function myFunction() {

  var sheets = SpreadsheetApp.openById("1HjOhDmBrzLZ1UyB9P-HuF0gJbfngW03zdOWWFNjS708");

  sheets.getSheetByName("Sheet1").getRange("A1").setFormula("=IMPORTRANGE(\"https://docs.google.com/spreadsheets/d/1uJkUxmkDHKaTghy-H4_YQ3ILKRurEkSEbR5UAy8SrIQ/edit#gid=0\",\"A1:C3\")");  
}
    
29.03.2017 / 22:19