I'm trying to create a script that allows a user to copy a highlighted range of cells to another, unopened, google sheet document. I can make it work if the sheet I'm copying data to is in the current document, but I can NOT figure out how to make the data copy to a different spreadsheet.
I've tried openById or openbyUrl, but I keep getting the following error:
"Target range and source range must be on the same spreadsheet."
function copyToDifferentDocument() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
// *** Have to figure out how to make the target a different document!!!! ***
var target = SpreadsheetApp.openById("targetsheetIDgoeshere");
/*
Next we need to pick the particular sheets within those spreadsheets.
Let's say your row is on the sheet named "New Stuff", and you have a sheet in the target spreadsheet named "Archive".
*/
var source_sheet = ss.getSheetByName("New Stuff");
var target_sheet = target.getSheetByName("Archive");
// The below makes the highlighted cells the range that will be copied.
var source_range = source_sheet.getActiveRange();
var last_row = target_sheet.getLastRow();
target_sheet.insertRowAfter(last_row);
var target_range = target_sheet.getRange("A"+(last_row+1)+":G"+(last_row+1));
// Take the highlighted rant and put it on the last row of the target sheet.
source_range.copyTo(target_range);
}
I'm trying to get the highlighted range to copy to a sheet in a different google sheet document.