Copy and paste into different sheets in Google Sheets

Asked

Viewed 3,140 times

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 ();
}

1 answer

2

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

Link to help you: http://www.mousewhisperer.co.uk/drivebunny/using-importrange-in-google-sheets/

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\")");  
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.