Reputation: 321
I'm trying to copy / transfer a sheet from one spreadsheet to another spreadsheet. I have tried various methods and .copyTo seems to be the best and most efficient way.
.copyTo works well but I'm struggling to send to a specific sheet...
Here is my code:
function TransferDataOut() {
var source = SpreadsheetApp.getActiveSpreadsheet();
var sheetA = source.getSheets()[0]; //sheet source number
var destination = SpreadsheetApp.openById('the destination sheet');
var each = "Data_Incoming";
var ss = SpreadsheetApp.getActiveSpreadsheet();
sheetA.copyTo(destination); // I tried renaming .setName(each);
}
So if I only use sheetA.copyTo(destination); it simply creates a copy sheet, like Copy of the_souce_sheet_name. If I try renaming to make it a specific name I will get error after running for the second time that the sheet already exists in destination spreadsheet.
What I really need to achieve is that the function from source spreadsheet copies data from the source sheet to always the same sheet in destination spreadsheet. Perhaps .copyTo is not the right way to do it? Any suggestions and code will help please!
The reason for receiving data on a exact sheet in the destination spreadsheet is because I have a trigger On change that executes another script to work with the new incoming data.
Upvotes: 1
Views: 264
Reputation: 201703
var each = "Data_Incoming"
.If my understanding is correct, how about this answer? I would like to propose 2 samples. So please select one of them for your situation.
The flow of this sample script is as follows.
each
sheet of Spreadsheet B is deleted.each
.function TransferDataOut() {
var source = SpreadsheetApp.getActiveSpreadsheet();
var sheetA = source.getSheets()[0]; //sheet source number
var destination = SpreadsheetApp.openById('the destination sheet');
var each = "Data_Incoming";
var destSheet = destination.getSheetByName(each);
if (destSheet) {
destination.deleteSheet(destSheet);
}
sheetA.copyTo(destination).setName(each);
}
The flow of this sample script is as follows.
If the sheet with the sheet name of each
is not existing in Spreadsheet B, the copied sheet is renamed to each
.
If the sheet with the sheet name of each
is existing in Spreadsheet B, the source sheet is copied to the Spreadsheet B as Copy of ###
.
each
sheet is cleared.each
sheet.function TransferDataOut() {
var source = SpreadsheetApp.getActiveSpreadsheet();
var sheetA = source.getSheets()[0]; //sheet source number
var destination = SpreadsheetApp.openById('the destination sheet');
var each = "Data_Incoming";
var copiedSheet = sheetA.copyTo(destination);
var destSheet = destination.getSheetByName(each);
if (destSheet) {
destSheet.clear();
var srcRange = copiedSheet.getDataRange();
srcRange.copyTo(destSheet.getRange(srcRange.getA1Notation()));
destination.deleteSheet(copiedSheet);
} else {
copiedSheet.setName(each);
}
}
each
sheet of the Spreadsheet B (destination Spreadsheet) is deleted. So please be careful this.
If I misunderstood your question and this was not the result you want, I apologize.
The following sample script is for achieving above situation.
At first, the script of destination Spreadsheet is prepared.
Script:function doGet() {
sample(); // This is the function that you want to run when the source values are copied.
return ContentService.createTextOutput();
}
After copy and paste above script to the script editor of destination Spreadsheet, please do the following flow.
https://script.google.com/macros/s/#####/exec
.As the next step, the script of source Spreadsheet is prepared as follows. In this sample, the sample 2 was used. I think that you can also use the sample 1.
Script:function TransferDataOut() {
var source = SpreadsheetApp.getActiveSpreadsheet();
var sheetA = source.getSheets()[0]; //sheet source number
var destination = SpreadsheetApp.openById('the destination sheet');
var each = "Data_Incoming";
var copiedSheet = sheetA.copyTo(destination);
var destSheet = destination.getSheetByName(each);
if (destSheet) {
destSheet.clear();
var srcRange = copiedSheet.getDataRange();
srcRange.copyTo(destSheet.getRange(srcRange.getA1Notation()));
destination.deleteSheet(copiedSheet);
} else {
copiedSheet.setName(each);
}
// Added
var url = "https://script.google.com/macros/s/###/exec"; // Please set the retrieved URL of Web Apps.
UrlFetchApp.fetch(url);
}
TransferDataOut()
of the source Spreadsheet was run, doGet()
of the destination Spreadsheet is run by UrlFetchApp.fetch(url)
.Upvotes: 2