Copy an Area into the next empty Row of another spreadsheet - google-apps-script

I am trying to copy a certain area into the next empty row of another spreadsheet.
Right now I am only able to copy it to a certain destination.
This is the code I am currently using:
function myFunction() {
var sheet=SpreadsheetApp.getActiveSheet();
var source="A3:S3";
var destination="Historie!B3";
sheet.getRange(source).copyTo(sheet.getRange(destination));
}

try something like this:
function xxx() {
var reference = 'sheet1!A2';
var rng = SpreadsheetApp.getActiveSpreadsheet().getRange(reference);
rng.setValue(rng.getValue()+1);
var ss = SpreadsheetApp.getActiveSpreadsheet ();
var ss = SpreadsheetApp.getActiveSpreadsheet ();
var source = ss.getRange ("Sheet1!A3:S3");
var destSheet = ss.getSheetByName("Historie");
var values = source.getValues().filter(function(e) {return e.some(function(f) {return f})});
destSheet.getRange(destSheet.getLastRow() + 1, 1, values.length, values[0].length).setValues(values);
}

Related

How do i paste into another file through app script

I am using this code:
function copyInfo() {  
var ss = SpreadsheetApp.getActiveSpreadsheet();  
var copySheet = ss.getSheetByName("Product-analyse");  
var pasteSheet = ss.getSheetByName("Blad18"); // get source range  
var source = copySheet.getRange(93,7,39,6);  // get destination range  
var destination = pasteSheet.getRange(pasteSheet.getLastRow()+1,1,39,6); // copy values to destination range  
source.copyTo(destination);  // clear source values
source.clearContent();
} 
Now its pasting in "Blad18". Instead I want it to be pasted in another file.
I tried adding {contentOnly: TRUE}. Though this didnt seem to work
I cannot figure this out unfortunately. Everything else is correctly set already.
Any help is greatly appreciated.
Updated script I am using:
function copyInfo() {
var ssSource = SpreadsheetApp.getActiveSpreadsheet();
var copySheet = ssSource.getSheetByName("Product-analyse");
var ssPaste = SpreadsheetApp.openById("1qhnA8L3ZkU-iEyOJtszAC4OW58sg2TJZSa_rmN_EXBI"); // the id of the other spreadsheet
var pasteSheet = ssPaste.getSheetByName("Submitted Data"); // get source range
var source = copySheet.getRange(93,7,39,6); // get destination range
var destination = pasteSheet.getRange(pasteSheet.getLastRow()+1,1,39,6); // copy values to destination range
source.copyTo(destination); // clear source values
source.clearContent();
}
Try this:
function copyInfo() {
var ss = SpreadsheetApp.getActive();
var csh = ss.getSheetByName("Sheet0");
var psh = ss.getSheetByName("Sheet1");
const cshsr = 2;
var crg = csh.getRange(cshsr, 1, csh.getLastRow() - cshsr + 1, csh.getLastColumn());
var prg = psh.getRange(psh.getLastRow() + 1, 1);
crg.copyTo(prg);
}
I believe what you want to do is copy from one spreadsheet file to another spreadsheet file. Here is an example
function copyInfo() {
var ssSource = SpreadsheetApp.getActiveSpreadsheet();
var copySheet = ssSource.getSheetByName("Product-analyse");
var ssPaste = SpreadsheetApp.openById("dfsfhsfhskfhaskjfhsjkfas"); // the id of the other spreadsheet
var pasteSheet = ssPaste.getSheetByName("Blad18"); // get source range
var source = copySheet.getRange(93,7,39,6); // get destination range
var destination = pasteSheet.getRange(pasteSheet.getLastRow()+1,1,39,6); // copy values to destination range
source.copyTo(destination); // clear source values
source.clearContent();
}
Reference
SpreadsheetApp.openById()

Missing Headers row and Leaving space

I was working with a combine sheets app script which is working fine at the spot by mereging A bunch of 36 sheetsinto one masterenter code here
the only flaw of the script is that it is not showing a common header row in the top and also leaving a space after data end from first sheets and so on >>>
here the code can anyone help a bit here
function combineSheets() {
var sApp = SpreadsheetApp.getActiveSpreadsheet();
var s1= sApp.getSheetByName("A1");
var s2= sApp.getSheetByName("A2");
var s3= sApp.getSheetByName("A3");
var s4= sApp.getSheetByName("A4");
var s5= sApp.getSheetByName("A5");
var s6= sApp.getSheetByName("A6");
var s7= sApp.getSheetByName("A7");
var s8= sApp.getSheetByName("A8");
var s9= sApp.getSheetByName("A9");
var s10= sApp.getSheetByName("A10");
var s11= sApp.getSheetByName("A11");
var s12= sApp.getSheetByName("A12");
var s13= sApp.getSheetByName("A13");
var s14= sApp.getSheetByName("A14");
var s15= sApp.getSheetByName("A15");
var s16= sApp.getSheetByName("A16");
var s17= sApp.getSheetByName("A17");
var s18= sApp.getSheetByName("A18");
var s19= sApp.getSheetByName("A19");
var s20= sApp.getSheetByName("A20");
var s21= sApp.getSheetByName("A21");
var s22= sApp.getSheetByName("A22");
var s23= sApp.getSheetByName("A23");
var s24= sApp.getSheetByName("A24");
var s25= sApp.getSheetByName("A25");
var s26= sApp.getSheetByName("A26");
var s27= sApp.getSheetByName("A27");
var s28= sApp.getSheetByName("A28");
var s29= sApp.getSheetByName("A29");
var s30= sApp.getSheetByName("A30");
var s31= sApp.getSheetByName("A31");
var s32= sApp.getSheetByName("A32");
var s33= sApp.getSheetByName("A33");
var s34= sApp.getSheetByName("A34");
var s35= sApp.getSheetByName("A35");
var s36= sApp.getSheetByName("A36");
var s37= sApp.getSheetByName("Master");
// If Master doesn't exist you'll need to create it here.
var s1values = s1.getRange(2,1,s1.getLastRow(),27).getValues();
var s2values = s2.getRange(2,1,s2.getLastRow(),27).getValues();
var s3values = s3.getRange(2,1,s3.getLastRow(),27).getValues();
var s4values = s4.getRange(2,1,s4.getLastRow(),27).getValues();
var s5values = s5.getRange(2,1,s5.getLastRow(),27).getValues();
var s6values = s6.getRange(2,1,s6.getLastRow(),27).getValues();
var s7values = s7.getRange(2,1,s7.getLastRow(),27).getValues();
var s8values = s8.getRange(2,1,s8.getLastRow(),27).getValues();
var s9values = s9.getRange(2,1,s9.getLastRow(),27).getValues();
var s10values = s10.getRange(2,1,s10.getLastRow(),27).getValues();
var s11values = s11.getRange(2,1,s11.getLastRow(),27).getValues();
var s12values = s12.getRange(2,1,s12.getLastRow(),27).getValues();
var s13values = s13.getRange(2,1,s13.getLastRow(),27).getValues();
var s14values = s14.getRange(2,1,s14.getLastRow(),27).getValues();
var s15values = s15.getRange(2,1,s15.getLastRow(),27).getValues();
var s16values = s16.getRange(2,1,s16.getLastRow(),27).getValues();
var s17values = s17.getRange(2,1,s17.getLastRow(),27).getValues();
var s18values = s18.getRange(2,1,s18.getLastRow(),27).getValues();
var s19values = s19.getRange(2,1,s19.getLastRow(),27).getValues();
var s20values = s20.getRange(2,1,s20.getLastRow(),27).getValues();
var s21values = s21.getRange(2,1,s21.getLastRow(),27).getValues();
var s22values = s22.getRange(2,1,s22.getLastRow(),27).getValues();
var s23values = s23.getRange(2,1,s23.getLastRow(),27).getValues();
var s24values = s24.getRange(2,1,s24.getLastRow(),27).getValues();
var s25values = s25.getRange(2,1,s25.getLastRow(),27).getValues();
var s26values = s26.getRange(2,1,s26.getLastRow(),27).getValues();
var s27values = s27.getRange(2,1,s27.getLastRow(),27).getValues();
var s28values = s28.getRange(2,1,s28.getLastRow(),27).getValues();
var s29values = s29.getRange(2,1,s29.getLastRow(),27).getValues();
var s30values = s30.getRange(2,1,s30.getLastRow(),27).getValues();
var s31values = s31.getRange(2,1,s31.getLastRow(),27).getValues();
var s32values = s32.getRange(2,1,s32.getLastRow(),27).getValues();
var s33values = s33.getRange(2,1,s33.getLastRow(),27).getValues();
var s34values = s34.getRange(2,1,s34.getLastRow(),27).getValues();
var s35values = s35.getRange(2,1,s35.getLastRow(),27).getValues();
var s36values = s36.getRange(2,1,s36.getLastRow(),27).getValues();
// Now, we can put out all together and stuff it in Master
var s37values = [];
s37values = s1values.concat(s2values,s3values,s4values,s5values,s6values,s7values,s8values,s9values,s10values,s11values,s12values,s13values,s14values,s15values,s16values,s17values,s18values,s19values,s20values,s21values,s22values,s23values,s24values,s25values,s26values,s27values,s28values,s29values,s30values,s31values,s32values,s33values,s34values,s35values,s36values);
s37.getRange(1,1,s37values.length,27).setValues(s37values);
}
Modification points:
About not showing a common header row in the top, in your script, all values are retrieved from the 2nd row. I thought that this might be the reason for your issue. From a common header row, in this modification, the header row is retrieved from the 1st sheet.
About also leaving a space after data end from first sheets and so on, I thought that the reason of your issue is due to getRange(2,1,s1.getLastRow(),27). In this case, the values are retrieved from 2nd row to the last row + 1 rows. In this case, it required to be getRange(2,1,s1.getLastRow() - 1,27).
In your script, I thought that the sheet names from "A1" to "A36" can be created using a script.
When these points are reflected in your script, it becomes as follows.
Modified script:
function combineSheets() {
var sApp = SpreadsheetApp.getActiveSpreadsheet();
var values = [...Array(36)].map((_, i) => `A${i + 1}`).flatMap((s, i) => {
var sheet = sApp.getSheetByName(s);
var lastRow = sheet.getLastRow();
if (i == 0) {
return sheet.getRange(1, 1, lastRow, 27).getValues();
}
return lastRow > 1 ? sheet.getRange(2, 1, lastRow - 1, 27).getValues() : [];
});
sApp.getSheetByName("Master").getRange(1, 1, values.length, 27).setValues(values);
}
The headr row is retrieved from the 1st sheet.
Note:
In this modification, it supposes that the sheet names of "A1" to "A36" and "Master" are existing in your Spreadsheet. Please be careful about this.
From a common header row in the top, this script supposes that your all sheets have the same header row. Please be careful about this.
About Exception: The number of rows in the range must be at least 1. AT HERE................var values = [...Array(36)].map((_, i) => A${i + 1}).flatMap((s, i) => { var sheet = sApp.getSheetByName(s); return sheet.getRange(i == 0 ? 1 : 2, 1, sheet.getLastRow() - (i == 0 ? 0 : 1), 27).getValues(); , from your question, I had thought that your all sheets have the values. But from your reply, it seems that my understanding was not correct. So, I updated my proposed script. Could you please confirm it?
References:
getRange(row, column, numRows, numColumns)
map()

How do I make this Google Script faster?

function Backup() {
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
spreadsheet.setActiveSheet(spreadsheet.getSheetByName('A'), true);
spreadsheet.getRange('A13:H').activate();
///Copy source to target and clear it
var target = SpreadsheetApp.openById("XXX");
var source_sheet = spreadsheet.getSheetByName("A");
var target_sheet = target.getSheetByName("B");
var source_range = source_sheet.getActiveRange();
var last_row = target_sheet.getLastRow();
var values = source_range.getValues();
target_sheet.getRange(last_row + 1, 1,values.length,values[0].length).setValues(values);
source_range.clearContent();
///How do I make this Google Script faster?
I believe your goal is as follows.
You want to reduce the process cost of your script.
In this case, how about the following patterns?
Pattern 1:
In this pattern, the Spreadsheet service (SpreadsheetApp) is used.
function sample1() {
var srcSpreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var srcSheet = srcSpreadsheet.getSheetByName("A");
var srcRange = srcSheet.getRange('A13:H' + srcSheet.getLastRow())
var srcValues = srcRange.getValues();
var dstSpreadsheet = SpreadsheetApp.openById("XXX"); // Please set the destination Spreadsheet ID here.
var dstSheet = dstSpreadsheet.getSheetByName("B");
dstSheet.getRange(dstSheet.getLastRow() + 1, 1, srcValues.length, srcValues[0].length).setValues(srcValues);
srcRange.clearContent();
}
Pattern 2:
In this pattern, Sheets API is used. Before you use this script, please enable Sheets API at Advanced Google services.
function sample2() {
var srcSpreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var srcSheet = srcSpreadsheet.getSheetByName("A");
var srcRange = "'A'!A13:H" + srcSheet.getLastRow();
var values = Sheets.Spreadsheets.Values.get(srcSpreadsheet.getId(), srcRange).values;
var dstSpreadsheetId = "XXX"; // Please set the destination Spreadsheet ID here.
Sheets.Spreadsheets.Values.append({ values }, dstSpreadsheetId, "B", { valueInputOption: "USER_ENTERED" });
srcSpreadsheet.getRange(srcRange).clearContent();
}
References:
Method: spreadsheets.values.get
Method: spreadsheets.values.append
To speed up, don't use activate() in your script. Try
// source
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var source_sheet = spreadsheet.getSheetByName("A");
var source_range = source_sheet.getRange('A13:H');
var values = source_range.getValues();
// target
var target = SpreadsheetApp.openById("XXX");
var target_sheet = target.getSheetByName("B");
var last_row = target_sheet.getLastRow();
// copy
target_sheet.getRange(last_row + 1, 1, values.length, values[0].length).setValues(values);
source_range.clearContent();

Column history - save

small beginner here.
I need to do copy values from one column and paste them in another one.
Firstly, i try something like "saveHistoryColumn". It works, but I do not wanna write variables from A-ZZ......Therefore I was looking for some looping, "Test" worsk, but my question is how to place the original data from B--> A, D-->C etc. in one sheet ? The test copy value from sheet 1 to sheet at same position...
function saveHistoryColumn() {
var ss = SpreadsheetApp.getActiveSpreadsheet ();
var ss1 = SpreadsheetApp.getActiveSpreadsheet ();
var source = ss.getRange ("Sheet1!B15:B20");
var source2 = ss1.getRange ("Sheet1!D15:D20");
source.copyTo (ss.getRange ("Sheet1!A15:A20"), {contentsOnly: true});
source2.copyTo (ss1.getRange ("Sheet1!C15:C20"), {contentsOnly: true});
}
function test () {try {
var spread = SpreadsheetApp.getActiveSpreadsheet();
var sheet = spread.getSheetByName("Sheet1");
var rlist = sheet.getRangeList(["B1:B10","D1:D10"]);
sheet = spread.getSheetByName("Sheet2"); // target will be Sheet1 A1:A10,C1:C10... etc.
for( var i=0; i<rlist.getRanges().length; i++ ) {
var r1 = rlist.getRanges()[i];
var r2 = sheet.getRange(r1.getA1Notation());
r1.copyTo(r2);
}} catch(err) {
Logger.log(err);}}
You can use something like
function backupColumns() {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1");
for(col = 1; col < sheet.getLastColumn(); col+=2) {
var values = sheet.getRange(1, col + 1, sheet.getLastRow(), 1).getValues();
sheet.getRange(1, col, sheet.getLastRow(), 1).setValues(values);
}
}
to copy each even column into each odd if that's what you want

Script to copy values from a range to the end of another spreadsheet

I'm trying to copy the values from one sheet :
https://docs.google.com/spreadsheets/d/1UDWk1xGnnL6qH_l5vo2MMwGH8wyXu0D9DCQMmZEKUJ0/edit#gid=0
To another :
https://docs.google.com/spreadsheets/d/1dWYj2W3gTDnNuqgX9O9RS0WWL02aDUQAVl1HJ5jTtvE/edit#gid=595677628
I tried the code there, but when I run it, nothing happens. I'm new to programmation, so maybe it's something vary basic.
function myFunction() {
function transferList() {
var sourceSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("OVERVIEW");
var sourceData = sourceSheet.getDataRange().getValues();
var targetSS = SpreadsheetApp.openById("1dWYj2W3gTDnNuqgX9O9RS0WWL02aDUQAVl1HJ5jTtvE").getSheetByName("RAWDATA");
var targetRangeTop = targetSS.getLastRow();
targetSS.getRange(targetRangeTop+1,1,sourceData.length,sourceData[0].length).setValues(sourceData);
}
}
I would like the values to be copied at the last available row...
function transferList() {
var sourceSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("OVERVIEW");
var sourceData = sourceSheet.getDataRange().getValues();
var targetSS = SpreadsheetApp.openById("1dWYj2W3gTDnNuqgX9O9RS0WWL02aDUQAVl1HJ5jTtvE").getSheetByName("RAWDATA");
var targetRangeTop = targetSS.getLastRow();
targetSS.getRange(targetRangeTop+1,1,sourceData.length,sourceData[0].length).setValues(sourceData);
}