Copying & pasting cells from one spreadsheet to another - errors

600 views
Skip to first unread message

G Mengelt

unread,
Mar 1, 2022, 4:56:54 PM3/1/22
to Google Apps Script Community
Very limited experience in scripting but this is what I have.  Getting "d.copyTo not a function error"  Any assistance is appreciated.

function copySmhValues()
{

  //Get source range
var source = SpreadsheetApp.openById('source ID'); //Different spreadsheet 

var sourceSheet = source.getSheetByName('current'); //Sheet tab with source data

var startRow = 27;  // First row of data to process
var numRows = 1;   // Number of rows to process
var startCol = 8;  //First column of data to process
var numCols = 14;    // Number of columns to process 
  
var dataRange = sourceSheet.getRange(startRowstartColnumRowsnumCols);

  // Fetch values for  row in the Range.
var data = dataRange.getValues();  

var d = SpreadsheetApp.getActiveSpreadsheet();

var getdestinationsheet = d.getSheetByName('smh');
   // get destination range

var destination = getdestinationsheet.getRange(getdestinationsheet.getLastRow()+1,8,1,14);

  // copy values to destination range
d.copyTo(destination);

}

Martin Hawksey

unread,
Mar 1, 2022, 5:07:30 PM3/1/22
to Google Apps Script Community
Not tested but from looking at your code I think changing 

d.copyTo(destination);

to

dataRange.copyTo(destination);

might solve the error you are encountering

G Mengelt

unread,
Mar 1, 2022, 5:14:25 PM3/1/22
to Google Apps Script Community
Thanks for taking your time to help.  When I use dataRange.copyTo(destination)  I get the following exception. 
"Exception: Target range and source range must be on the same spreadsheet."

Martin Hawksey

unread,
Mar 1, 2022, 5:32:58 PM3/1/22
to Google Apps Script Community
ah ok - how about replacing 

d.copyTo(destination);

with

destination.setValues(data);

G Mengelt

unread,
Mar 1, 2022, 5:41:30 PM3/1/22
to Google Apps Script Community
Thank you so much.  That solved the problem.  Have a great day!

Dejan Georg

unread,
Nov 5, 2022, 12:34:04 PM11/5/22
to Google Apps Script Community
Hey guys, I have a similar issue, but I don't understand how to solve it by looking at your example. Could you take a look?

function moveRows() {

  var ss = SpreadsheetApp.openById('1PugO3VL8ZwGfA1stYfUkQK2WoU-FfRUBHUzRDTmEbYE');
  var target = SpreadsheetApp.openById('17my-WAAVkZK8d132dH50jwpQZYQY4WEC_xm04-fCKR8');

  var source_sheet = ss.getSheetByName('Sheet2');
  var target_sheet = target.getSheetByName('Sheet1');

  let row = source_sheet.getActiveRange().getRow();
  let activeRow = source_sheet.getRange( row ,1, 1 ,10);
  let last_row = target_sheet.getLastRow();
   
  activeRow.copyTo(target_sheet.getRange('A'+(last_row+1)+':Q'+(last_row+1)));           -> on this row, getting the: Exception: Target range and source range must be on the same spreadsheet.
  source_sheet.deleteRow(row);

}

Dejan Georg

unread,
Nov 5, 2022, 12:35:37 PM11/5/22
to Google Apps Script Community
I have tried to adjust the script, so that the target & destination are on the same spreadsheet, but different sheets. And it works like a charm in that situation. However I need them to be 2 different spreadsheets.
Reply all
Reply to author
Forward
0 new messages