Background
I wrote script for copying range of cells from one document to another. It works correctly. But depending on volume of data it could run for 10+ minutes. Which is odd, because manual copy-pasting of this data without script will took 2 minutes. I ran some tests that revealed most time-consuming piece of code that needs to be speed up
Question
What is the quickest method of copying range of cells from one Google Spreadsheet document to another? Or how should I change my code to speed it up?
// Copying 1st batch of data
var srcLastRow = srcSheet1.getLastRow();
var srcLastCol = srcSheet1.getLastColumn();
var srcRange = srcSheet1.getRange(2, 1, srcLastRow-1, srcLastCol);
var transfValues = srcRange.getValues();
destSheet.getRange(destLastRow + 1,1,srcLastRow-1, srcLastCol).setValues(transfValues);
Appendix
And here is full script, that runs correctly but slow, just in case
function sborka() {
// Destination sheet variables
var destSpreadSheet = SpreadsheetApp.getActiveSpreadsheet();
var destSheet = destSpreadSheet.getSheetByName("destination");
// Source sheet variables
var srcSpreadSheet1 = SpreadsheetApp.openById('xxxxxx');
var srcSheet1 = srcSpreadSheet1.getSheetByName("source 1");
var srcSpreadSheet2 = SpreadsheetApp.openById('ууууууу');
var srcSheet2 = srcSpreadSheet2.getSheetByName("source 2");
// Clear all except 1st row with headers
var destLastRow = destSheet.getLastRow();
destSheet.deleteRows(2,destLastRow)
// What is last row in destination sheet?
var destLastRow = destSheet.getLastRow();
// Copying 1st batch of data
var srcLastRow = srcSheet1.getLastRow();
var srcLastCol = srcSheet1.getLastColumn();
var srcRange = srcSheet1.getRange(2, 1, srcLastRow-1, srcLastCol);
var transfValues = srcRange.getValues();
destSheet.getRange(destLastRow + 1,1,srcLastRow-1, srcLastCol).setValues(transfValues);
// What is last row in destination sheet now?
var destLastRow = destSheet.getLastRow();
// Copying 2nd batch of data
var srcLastRow = srcSheet2.getLastRow();
var srcLastCol = srcSheet2.getLastColumn();
var srcRange = srcSheet2.getRange(2, 1, srcLastRow-1, srcLastCol);
var transfValues = srcRange.getValues();
destSheet.getRange(destLastRow + 1,1,srcLastRow-1, srcLastCol).setValues(transfValues);
// Done
}
Try this:
var srcSheet1 = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var destSS = SpreadsheetApp.openById("destination ss id");
//Copy Source Sheet
srcSheet1.copyTo(destSS);
var newSrcSheet1 = destSS.getSheetByName("Copy of "+srcSheet1.getName());
Logger.log(newSrcSheet1);
//Copy range
var destSheet = destSS.getSheetByName("Sheet1");
var srcLastRow = newSrcSheet1.getLastRow();
var srcLastCol = newSrcSheet1.getLastColumn();
var srcRange = newSrcSheet1.getRange(2, 1, srcLastRow-1, srcLastCol);
srcRange.copyTo(destSheet.getRange(destSheet.getLastRow() + 1,1));
//Delete Sheet
destSS.deleteSheet(newSrcSheet1);
I copied first the source sheet to the destination spreadsheet. The name of the copied sheet will be "Copy of [original sheet name]". Refer to Sheet.copyTo()
Then, I copied the range from the new source sheet to the destination sheet using Range.copyTo(destination)
Lastly, I deleted the new source sheet.
Related
Objective - To copy all non-empty cells from a Spreadsheet-1 to Spreadsheet-2.
I saw some codes online and modified for my use, sharing below.
I need help in getting the script right so that a variable range(i.e. to selects non-empty up to last rows and last columns) can be selected and data copied to Spreadsheet-2.
I am trying to make it workable for 'A1:GZ1000' range.
If numbers of rows / cols with non-zero (non-empty) increases, script needs to update range for passing values to Spreadsheet-2.
''''
function dataImport(ssaID, ssbID) {
// source doc
var ssaID = '1DZX1Jb4qwCHETtAENIsRioJvl4TC0hUWZDn_k1q3oes' ;
var sheet = SpreadsheetApp.openById(ssaID);
var ssbID = '1HptKwYzdWCsz9oey9hOouhWNTQ3GAyu9oswZ1xgP3D0' ;
// source sheet
var ss = sheet.getSheetByName('Data - Price');
// Get full range of data
var SRange = ss.getDataRange();
// get A1 notation identifying the range
var A1Range = SRange.getA1Notation();
// get the data values in range
var SData = SRange.getValues();
// Logger.log(SData)
// target spreadsheet
var tss = SpreadsheetApp.openById(ssbID);
// target sheet
var ts = tss.getSheetByName('Data - Price');
// Clear the Google Sheet before copy
ts.clear({ contentsOnly: true });
Logger.log(SRange.length);
// set the target range to the values of the source data
var tsRange = ts.getRange(1,1,SRange.length,SRange[0].length);
tsRange.setValues(Sdata);
}
''''
Pl. do ask for details if you require.
R U
Try it this way:
function dataImport(ssaID, ssbID) {
var ssaID = '1DZX1Jb4qwCHETtAENIsRioJvl4TC0hUWZDn_k1q3oes';
var ss = SpreadsheetApp.openById(ssaID);
var ssbID = '1HptKwYzdWCsz9oey9hOouhWNTQ3GAyu9oswZ1xgP3D0';
var sh = ss.getSheetByName('Data - Price');
var rg = sh.getDataRange();
var vs = rg.getValues();
var tss = SpreadsheetApp.openById(ssbID);
var tsh = tss.getSheetByName('Data - Price');
tsh.clearContents();
tsh.getRange(1, 1, vs.length, vs[0].length).setValues(vs);
}
this is my final script
function archive2() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var source = ss.getSheetByName('Lista'); // name of source sheet
var sourceRange = source.getRange('A1:B'); // range to copy
var destination1 = ss.getSheetByName('Sheet2'); // name of log sheet
var lastRow = destination1.getLastRow();
sourceRange.copyTo(destination1.getRange(5000 + 1, 1),{contentsOnly:true});
var source = ss.getSheetByName('Sheet2'); // name of source sheet
var sourceRange = source.getRange('H1:I5000'); // range to copy
var destination2 = ss.getSheetByName('Sheet3'); // name of log sheet
var lastRow = destination2.getLastRow();
destination2.getRange('A1:B').activate();
destination2.getActiveRangeList().clear({contentsOnly: true, skipFilteredRows: true});
var source = ss.getSheetByName('Sheet2'); // name of source sheet
var sourceRange = source.getRange('H1:I5000'); // range to copy
var destination3 = ss.getSheetByName('Sheet3'); // name of log sheet
sourceRange.copyTo(destination3.getRange(1 + 1, 1),{contentsOnly:true});
var spreadsheet = SpreadsheetApp.getActive();
var source = ss.getSheetByName('Sheet3'); // name of source sheet
var sourceRange = source.getRange('A1:B5000'); // range to copy
var destination4 = ss.getSheetByName('Sheet2'); // name of log sheet
var lastRow = destination4.getLastRow();
spreadsheet.setActiveSheet(spreadsheet.getSheetByName('Sheet2'), true);
spreadsheet.getRange('A:B').activate();
spreadsheet.getActiveRangeList().clear({contentsOnly: true, skipFilteredRows: true});
sourceRange.copyTo(destination4.getRange('A:B'),{contentsOnly:true});
var hide1 = ss.getSheetByName('Sheet2');
hide1.hideSheet();
var hide2 = ss.getSheetByName('Sheet3');
hide2.hideSheet();
}
Basically it makes a static copy of some data that I need to sum later. This part works fine.
What isn't working are the last lines, basically I want to hide Sheet2 and Sheet3, which are helper sheets to make the calcs.
The best way would be to make all the calcs without unhide those sheets but I wasn't able to find a solution (so if someone know what I've to change to perform it, would be great), so I'm searching a way to at least hide them after the script run.
With these lines, sometimes they are both hide, sometime just 1 of them (randomly between sheet2 and sheet3, I guess it have something to do with "active", and so I've tried to active both in sequence but still doesn't work).
Could you help me to hide both everytime (or make it works without unhide anything at all) without failures? Thanks
https://docs.google.com/spreadsheets/d/1GREsQNrDAaLqOROwAixajK6tpTRGOhqGHo0kfyyyuGw/edit#gid=0
I have three source files - Alpha, Beta, Kappa. They have same headers and basically, I just want them all consolidated in one file on a weekly basis. I started with Kappa and somehow it works - it creates a new spreadsheet and copies info from the Kappa file and paste it to the newly created spreadsheet as values.
Now I tried, adding the Alpha file to the same spreadsheet, just below the info that I copied from the Kappa file using the getLastRow function. I got an error saying that source range and target range must be on the same spreadsheet.
function RawExtractCopy() {
var version = 'ver. 4.01'
var ssnew = SpreadsheetApp.create('V5 Raw Extract '+ version ); //create a spreadsheet
var ssnewsheet = ssnew.getSheetByName('Sheet1'); //get the sheet named Sheet1
ssnewsheet.insertRows(1,30000); //inserting more rows
ssnewsheet.setName('V5 Raw Extract'); //rename the sheet name of the newly created spreadsheet
var ssKappa = SpreadsheetApp.getActiveSpreadsheet(); //opens source file
var targetss = ssnew; //define created ss as the target ss
var srcSheetKappa = ssKappa.getSheetByName('KAPPA'); //get source sheet name
var targetSheet = targetss.getSheetByName('V5 Raw Extract'); //defining target sheet
var srcRangeKappa = srcSheetKappa.getRange("A1:AM30000"); //get source data
var destRangeKappa = targetss.getRange("A1"); //define target
var values = srcRangeKappa.getValues(); //line 18 to 21 is just to match source sheet and target sheet
var bGcolors = srcRangeKappa.getBackgrounds();
var colors = srcRangeKappa.getFontColors();
var fontSizes = srcRangeKappa.getFontSizes();
destRangeKappa.setValues(values);
destRangeKappa.setBackgrounds(bGcolors);
destRangeKappa.setFontColors(colors);
destRangeKappa.setFontSizes(fontSizes);
srcRangeKappa.copyTo(destRangeKappa, {contentsOnly: true});
var ssAlpha = SpreadsheetApp.openById("1gIs4vCdcGG79poDujz9t8Fq7_BWlaEiMrYUH9DTxVHs").activate();
var srcSheetAlpha = ssAlpha.getSheetByName('V5 ALPHA');
var srcRangeAlpha = srcSheetAlpha.getRange("A2:AM30000");
var destRangeAlpha = targetSheet.getRange(targetSheet.getLastRow()+1,1);
var valuesAlpha = srcRangeAlpha.getValues();
var bGcolorsAlpha = srcRangeAlpha.getBackgrounds();
var colorsAlpha = srcRangeAlpha.getFontColors();
var fontSizesAlpha = srcRangeAlpha.getFontSizes();
destRangeAlpha.setValues(values);
destRangeAlpha.setBackgrounds(bGcolors);
destRangeAlpha.setFontColors(colors);
destRangeAlpha.setFontSizes(fontSizes);
srcRangeKappa.copyTo(destRangeAlpha, {contentsOnly: true});
}
The thing, it still copies the data from Kappa but did not push through with Beta. Can someone please be kind to tell what I am missing here?
The OP's goal is to make a duplicate of three sheets in three separate spreadsheets; Alpha, Beta and Kappa. The OP's code works for Kappa, but not Alpha or Beta.
Why does this code work and the OP code doesn't
Well, the OP code does work, but the OP fudges by specifying a fixed range (30000 rows long and about 40 column wide). This works for the first sheet, but not so easily for the second and third sheets. The following code differs in two major respects from the OP code:
the data ranges in Alpha, Beta, Kappa and "Raw Extract" are evaluated using getLastRow and getLastColumn. So the source range and the target range will match each other. In addition, data from the second and third sheets is easily appended one row below the preceding last row.
the copyTo code is changed to copy the whole sheet rather than a range. The reason for this is the the preceding four commands setValues(values), setBackgrounds(bGcolors), setFontColors(colors), and setFontSizes(fontSizes) have already copied the data range to "Raw Extract".
function so_55448299() {
var version = 'ver. 4.01'
var ssnew = SpreadsheetApp.create('V5 Raw Extract ' + version); //create a spreadsheet
var ssnewsheet = ssnew.getSheetByName('Sheet1'); //get the sheet named Sheet1
ssnewsheet.setName('V5 Raw Extract'); //rename the sheet name of the newly created spreadsheet
// copy Kappa and data
var ssKappa = SpreadsheetApp.getActiveSpreadsheet(); //opens source file
var targetss = ssnew; //define created ss as the target ss
var srcSheetKappa = ssKappa.getSheetByName('KAPPA'); //get source sheet name
var targetSheet = targetss.getSheetByName('V5 Raw Extract'); //defining target sheet
// get the last row and column of the source sheet
var kappaLastRow = srcSheetKappa.getLastRow();
var kappaLastCol = srcSheetKappa.getLastColumn();
//Logger.log("DEBUG: Kappa Last row = "+kappaLastRow+", Last column = "+kappaLastCol);//DEBUG
// declare the source and target ranges
var srcRangeKappa = srcSheetKappa.getRange(1, 1, kappaLastRow, kappaLastCol); //get source data
var destRangeKappa = targetSheet.getRange(1, 1, kappaLastRow, kappaLastCol); //define target
// get values and other data
var values = srcRangeKappa.getValues(); //line 18 to 21 is just to match source sheet and target sheet
var bGcolors = srcRangeKappa.getBackgrounds();
var colors = srcRangeKappa.getFontColors();
var fontSizes = srcRangeKappa.getFontSizes();
// set values and other data
destRangeKappa.setValues(values);
destRangeKappa.setBackgrounds(bGcolors);
destRangeKappa.setFontColors(colors);
destRangeKappa.setFontSizes(fontSizes);
// duplicate the entire sheet
srcSheetKappa.copyTo(targetss);
// end copy Kappa and data
// start copy Alpha and data
var ssAlpha = SpreadsheetApp.openById("<insert code here>");
var srcSheetAlpha = ssAlpha.getSheetByName('V5 ALPHA');
// get the last row and column of the source sheet
var alphaLastRow = srcSheetAlpha.getLastRow();
var alphaLastCol = srcSheetAlpha.getLastColumn();
//Logger.log("DEBUG: Alpha Last row = "+alphaLastRow+", Alpha Last Column = "+alphaLastCol);//DEBUG
// get the last row and column of the target sheet
var rawLastRow = targetSheet.getLastRow();
var rawLastCol = targetSheet.getLastColumn();
Logger.log("DEBUG: Target Last row = " + rawLastRow + ", Target Last Column = " + rawLastCol); //DEBUG
// declare the source and target ranges
var srcRangeAlpha = srcSheetAlpha.getRange(2, 1, alphaLastRow - 1, alphaLastCol);
var destRangeAlpha = targetSheet.getRange(rawLastRow + 1, 1, alphaLastRow - 1, alphaLastCol);
//Logger.log("DEBUG:destRangeAlpha = "+destRangeAlpha.getA1Notation());//DEBUG
// get values and other data
var values = srcRangeAlpha.getValues();
var bGcolors = srcRangeAlpha.getBackgrounds();
var colors = srcRangeAlpha.getFontColors();
var fontSizes = srcRangeAlpha.getFontSizes();
// set values and other data
destRangeAlpha.setValues(values);
destRangeAlpha.setBackgrounds(bGcolors);
destRangeAlpha.setFontColors(colors);
destRangeAlpha.setFontSizes(fontSizes);
// duplicate the entire sheet
srcSheetAlpha.copyTo(targetss);
// end copy Alpha and data
// start copy Beta and data
var ssBeta = SpreadsheetApp.openById("<insert code here>");
var srcSheetBeta = ssBeta.getSheetByName('V5 BETA');
// get the last row and column of the source sheet
var betaLastRow = srcSheetBeta.getLastRow();
var betaLastCol = srcSheetBeta.getLastColumn();
//Logger.log("DEBUG: Beta Last row = "+betaLastRow+", Beta Last Column = "+betaLastCol);//DEBUG
// get the last row and column of the target sheet
var rawLastRow = targetSheet.getLastRow();
var rawLastCol = targetSheet.getLastColumn();
//Logger.log("DEBUG: Target Last row = "+rawLastRow+", Target Last Column = "+rawLastCol);//DEBUG
// declare the source and target ranges
var srcRangeBeta = srcSheetBeta.getRange(2, 1, betaLastRow - 1, betaLastCol);
var destRangeBeta = targetSheet.getRange(rawLastRow + 1, 1, betaLastRow - 1, betaLastCol);
//Logger.log("DEBUG:destRangeBeta = "+destRangeBeta.getA1Notation());//DEBUG
// get values and other data
var values = srcRangeBeta.getValues();
var bGcolors = srcRangeBeta.getBackgrounds();
var colors = srcRangeBeta.getFontColors();
var fontSizes = srcRangeBeta.getFontSizes();
// set values and other data
destRangeBeta.setValues(values);
destRangeBeta.setBackgrounds(bGcolors);
destRangeBeta.setFontColors(colors);
destRangeBeta.setFontSizes(fontSizes);
// duplicate the entire sheet
srcSheetBeta.copyTo(targetss);
// end copy Beta and data
}
UPDATE - One Small Function
#tehhowch rightly observes that a more appropriate way to manage this process would be one small function called with a few parameters from a driver function. This is something that I had in mind at the time but (for better or worse) I felt that the long-hand approach would enable the OP to better understand how the code varied from their own. However, this variation seeks to fulfil tehhowch's observation. I have left in the Logger statements for the benefit of the OP (which explains the extraordinary length of the code
function so_55448299_04() {
//Note#1: this function assumes that it is located in the "Kappa" sheet
//Note#2: the spreadsheet ID for Alpha and Beta cannot be assigned to a variable. they must be entered longhand BEFORE this function is processed.
//Note#3: the target sheet names are also entered longhand. BUT the number of sheets is described in the variable "usersheets"
// user defined variables
var targetVn = 'ver. 4.01';
var targetName = 'V5 Raw Extract';
var usersheets = 3;
// create the targetspreadsheet and starting sheet
var targetss = SpreadsheetApp.create(targetName + " " + targetVn);
var targetSheet = targetss.getSheetByName('Sheet1');
targetSheet.setName(targetName);
//loop through the usersheets
for (var i = 0; i < usersheets; i++) {
// if i==0, then process this sheet - KAPPA
if (i == 0) {
//Logger.log("DEBUG: This is KAPPA");//DEBUG
var srcss = SpreadsheetApp.getActiveSpreadsheet();
//Logger.log("DEBUG: this spreadsheet is "+srcss.getName());//DEBUG
// startrow is 1 in order to include headers
var startrow = 1;
var srcSheet = srcss.getSheetByName("v5 KAPPA");
}
// if i=1, then process Alpha
else if (i == 1) {
//Logger.log("DEBUG: This is ALPHA");//DEBUG
var srcss = SpreadsheetApp.openById("<Insert code>");
//Logger.log("DEBUG: this spreadsheet is "+srcss.getName());//DEBUG
// startrow is 2 in iorder to avoid duplicating headers
var startrow = 2;
var srcSheet = srcss.getSheetByName("v5 ALPHA");
}
// if i=2, then process Beta
else if (i == 2) {
//Logger.log("DEBUG: This is BETA");//DEBUG
var srcss = SpreadsheetApp.openById("<Insert code>");
//Logger.log("DEBUG: this spreadsheet is "+srcss.getName());//DEBUG
// startrow is 2 in iorder to avoid duplicating headers
var startrow = 2;
var srcSheet = srcss.getSheetByName(usersheets[i]);
}
// run the subroutine to copy and paste data
// Logger.log("DEBUG: srcSheet: "+srcSheet.getName()+", targetSheet: "+targetSheet.getName()+", startrow: "+startrow+", targetss: "+targetss.getName());//DEBUG
var getresult = getData04(srcSheet, targetSheet, startrow, targetss);
}
}
function getData04(srcSheet, targetSheet, startrow, targetss) {
//get the source sheet - last row and column
var srcLastRow = srcSheet.getLastRow();
var srcLastCol = srcSheet.getLastColumn();
//Logger.log("DEBUG: Source Last row = "+srcLastRow+", Last column = "+srcLastCol);//DEBUG
// get the target sheet - last row and column
var targetLastRow = targetSheet.getLastRow();
var targetLastCol = targetSheet.getLastColumn();
//Logger.log("DEBUG: Target Last row = "+targetLastRow+", Target Last Column = "+targetLastCol);//DEBUG
// declare the source and target ranges
if (startrow == 1) {
var srcRange = srcSheet.getRange(startrow, 1, srcLastRow, srcLastCol);
var targetRange = targetSheet.getRange(startrow, 1, srcLastRow, srcLastCol);
} else {
var srcRange = srcSheet.getRange(startrow, 1, srcLastRow - 1, srcLastCol);
var targetRange = targetSheet.getRange(targetLastRow + 1, 1, srcLastRow - 1, srcLastCol);
}
//Logger.log("DEBUG: srcRange = "+srcRange.getA1Notation()+", target range = "+targetRange.getA1Notation());//DEBUG
// get source values and other data
var values = srcRange.getValues();
var bGcolors = srcRange.getBackgrounds();
var colors = srcRange.getFontColors();
var fontSizes = srcRange.getFontSizes();
// set values and other data
targetRange.setValues(values);
targetRange.setBackgrounds(bGcolors);
targetRange.setFontColors(colors);
targetRange.setFontSizes(fontSizes);
// duplicate the entire sheet
srcSheet.copyTo(targetss);
var result = "Successful";
return result;
}
In an effort to organize a set of bank account and credit card statements, I download them. Then I open them in Excel or Google Sheets, reorganize them in matching columns, put all in one tab (TOTAL), and then sort.
The question is now how I do the copy from the individual sheets. I think I'm pretty close after pasting and repeating (the manual way) some function I found by searching. The problem is that this function removes all formatting from the source rows
Example sheet
The last tab example_TOTAL is put there only to show how I want tab TOTAL to look after the process is done.
Here's my function so far which does the most, but erases formatting (colors).
function Copy() {
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var ss = spreadsheet.getSheetByName('TabA'); //replace with source Sheet tab name
var range = ss.getRange('A1:L'); //assign the range you want to copy
var data = range.getValues();
var ts = spreadsheet.getSheetByName('TOTAL'); //replace with destination Sheet tab name
ts.getRange(ts.getLastRow()+1, 1, data.length, data[0].length).setValues(data);
var ss = spreadsheet.getSheetByName('TabB'); //replace with source Sheet tab name
var range = ss.getRange('A1:L'); //assign the range you want to copy
var data = range.getValues();
ts.getRange(ts.getLastRow()+1, 1, data.length, data[0].length).setValues(data);
var ss = spreadsheet.getSheetByName('TabC'); //replace with source Sheet tab name
var range = ss.getRange('A1:L'); //assign the range you want to copy
var data = range.getValues();
ts.getRange(ts.getLastRow()+1, 1, data.length, data[0].length).setValues(data);
var ss = spreadsheet.getSheetByName('TabD'); //replace with source Sheet tab name
var range = ss.getRange('A1:L'); //assign the range you want to copy
var data = range.getValues();
ts.getRange(ts.getLastRow()+1, 1, data.length, data[0].length).setValues(data);
}
function copyFromTo() {
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var ss = spreadsheet.getSheetByName('TabA');
var copy = ss.getRange('A1:L');
var target = spreadsheet.getSheetByName('TOTAL');
var dest = target.getRange('A1:L');
copy.copyTo(dest);
var ss = spreadsheet.getSheetByName('TabB');
var copy = ss.getRange('A1:L');
copy.copyTo(dest);
var ss = spreadsheet.getSheetByName('TabC');
var copy = ss.getRange('A1:L');
copy.copyTo(dest);
var ss = spreadsheet.getSheetByName('TabD');
var copy = ss.getRange('A1:L');
copy.copyTo(dest);
}
How can I append from these sheets and still keep the formatting?
I want to copy data from one spreadsheet into another one. But
1. The Data are on several sheets
2. Only certain Columns (just to speed it up, don't need all columns)
For the beginning I used the following script:
function CopyDataToNewFile() {
var sss = SpreadsheetApp.openById('0AjN7uZG....'); // sss = source spreadsheet
var ss = sss.getSheetByName('Monthly'); // ss = source sheet
//Get full range of data
var SRange = ss.getDataRange();
//get A1 notation identifying the range
var A1Range = SRange.getA1Notation();
//get the data values in range
var SData = SRange.getValues();
var tss = SpreadsheetApp.openById('8AjN7u....'); // tss = target spreadsheet
var ts = tss.getSheetByName('RAWData'); // ts = target sheet
//set the target range to the values of the source data
ts.getRange(A1Range).setValues(SData);
}
I took this one from user2741, which was posted here:
..this one works fine for one sheet.
But now I am unsure how to get the data from more sheets and actually sort it. Does anybody has a good idea on how to do this?
Thank you all,
Sascha
This one here works, if everything happens in the same spreadsheet
SpreadsheetApp.getActiveSpreadsheet().getSheetByName("SummarySheet").clear();
mergeSheet2("SummarySheet", "123");
mergeSheet2("SummarySheet", "345");
mergeSheet2("SummarySheet", "748");
mergeSheet2("SummarySheet", "293");
function mergeSheet2(targetSheetName, sourceSheetName) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sourceSheet = ss.getSheetByName(sourceSheetName);
var lastRow = sourceSheet.getLastRow();
var lastCol = sourceSheet.getLastColumn();
var source = sourceSheet.getRange(1,1,lastRow,lastCol); // Error after some iterations: The coordinates or dimensions of the range are invalid. (line 11, file "copy-m.js") -> might be empty spreadsheet
var destSheet = ss.getSheetByName(targetSheetName);
var destRange = destSheet.getRange(destSheet.getLastRow()+1,1);
source.copyTo(destRange, {contentsOnly: true});
}
but for some reason I don't get it run when using different spreadsheets
Error: "Target range and source range must be on the same spreadsheet"
I tried it with:
SpreadsheetApp.getActiveSpreadsheet().getSheetByName("SummarySheet").clear();
SpreadsheetApp.openById('ID');
mergeSheet2("SummarySheet", "123");
function mergeSheet2(targetSheetName, sourceSheetName) {
var ss = SpreadsheetApp.openById('ID');
var ssd = SpreadsheetApp.openById('ID');
var sourceSheet = ss.getSheetByName(sourceSheetName);
var lastRow = sourceSheet.getLastRow();
var lastCol = sourceSheet.getLastColumn();
var source = sourceSheet.getRange(1,1,lastRow,lastCol); // Error after some iterations: The coordinates or dimensions of the range are invalid. (line 11, file "copy-m.js") -> might be empty spreadsheet
var destSheet = ssd.getSheetByName(targetSheetName);
var destRange = destSheet.getRange(destSheet.getLastRow()+1,1);
source.copyTo(destRange, {contentsOnly: true});
Any ideas?
The solution is simpler than what you tried, since you want to copy values only and knowing that copyTo only works in the same spreadsheet, just get the values from source and paste it in destination like this:
function copyFromSourceToTarget(targetSheetName, sourceSheetName) {
var ss = SpreadsheetApp.openById('ID');
var ssd = SpreadsheetApp.openById('ID');
var sourceSheet = ss.getSheetByName(sourceSheetName);
var sourceData = sourceSheet.getDataRange().getValues();
var destSheet = ssd.getSheetByName(targetSheetName);
destSheet.getRange(destSheet.getLastRow()+1,1,sourceData.length,sourceData[0].length).setValues(sourceData);
}
To make it more flexible I'd recommend to use 4 parameters and simplify again the code :
function copyFromSourceToTarget(targetSheetName,targetSsId, sourceSheetName,sourceSsId) {
var ss = SpreadsheetApp.openById(sourceSsId)getSheetByName(sourceSheetName);
var ssd = SpreadsheetApp.openById(targetSsId).getSheetByName(targetSheetName);
var sourceData = ss.getDataRange().getValues();
ssd.getRange(ssd.getLastRow()+1,1,sourceData.length,sourceData[0].length).setValues(sourceData);
}