I already have a script that kind of making what I want, but I have a small issue with it, it's that it copy all the sheet with everything instead for me, I want it to be just the values of the cells and past them to the 2nd sheet.
Here is my code
function copyValueFromSheetToSheet() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var copySheet = ss.getSheetByName("Sheet1");
var pasteSheet = ss.getSheetByName("Sheet2");
// get source range
var source = copySheet.getRange(2,1,12,4);
// get destination range
var destination = pasteSheet.getRange(pasteSheet.getLastRow()+1,1,12,1);
// copy values to destination range
source.copyTo(destination);
// clear source values
source.clearContent();
Browser.msgBox('Commande Confirmer');
}
thank you
Description
Modify your code as shown below
Script
// get source range
var source = copySheet.getRange(2,1,12,4).getValues();
var destination = pasteSheet.getRange(pasteSheet.getLastRow()+1,1,12,4).setValues(source);
// copy values to destination range
// source.copyTo(destination); <- not needed
Related
I Have this code to copy all sheet data from one spreadsheet to another
function importData() {
var ss1 = SpreadsheetApp.openById("1_cujCKj2Xd-JT-bCu2-xSv2gij6qpQXzFtNQ6BY").getSheetByName("Sheet1").getDataRange();
var ss2 = SpreadsheetApp.openById("1N1tMU3ydG1poly4RDekdDGzlmsyRYLPn3LsfWmWg").getSheetByName("Sheet5");
var toRange = ss2.getRange(1, 1, ss1.getNumRows(), ss1.getNumColumns())
toRange.setValues(ss1.getValues());
}
But what i need to copy specific data range instead of copying all the sheet.
Can anyone help me so i can copy range like ("A1:E10") from the source spreadsheet to the cell ("B1") in the target sheet
In your situation, how about the following modification?
Modified script:
function importData() {
var srcRange = "Sheet1!A1:E10"; // Please set the source range as A1Notation.
var dstRange = "Sheet5!B1"; // Please set the destination range as A1Notation.
var ss1 = SpreadsheetApp.openById("1_cujCKj2Xd-JT-bCu2-xSv2gij6qpQXzFtNQ6BY").getRange(srcRange);
var ss2 = SpreadsheetApp.openById("1N1tMU3ydG1poly4RDekdDGzlmsyRYLPn3LsfWmWg").getRange(dstRange);
var srcValues = ss1.getValues();
ss2.offset(0, 0, srcValues.length, srcValues[0].length).setValues(srcValues);
}
When this script is run, the values are retrieved from "Sheet1!A1:E10", and the retrieved values are put to "Sheet5!B1".
first I'm not good in coding just trying figure my way and make what i need.
so my function is to copy data from sheet to sheet and delete the data after finishing.
my issue is small but can't find a solution for it, it's when I'm copying it copy just 12 rows, but for me i want it to copy as much rows will be.
my final code i want it to copy the rows that have a specific word, but for now I'm searching for a solution for this issue.
var ss = SpreadsheetApp.getActiveSpreadsheet();
var copySheet = ss.getSheetByName("Sheet1");
var pasteSheet = ss.getSheetByName("Sheet2");
// get source range
var range = copySheet.getRange(2,1,12,4);
var source = range.getValues();
var destination = pasteSheet.getRange(pasteSheet.getLastRow()+1,1,12,4).setValues(source);
range.clearContent();
// get destination range
var destination = pasteSheet.getRange(pasteSheet.getLastRow()+1,1,12,1);
// clear source values
Browser.msgBox('Commande Confirmer');
}```
this is my code
thanks in advance
Description
In this example, the script gets all values from Sheet1, not just 12 rows. It then uses the Array.filter() to get all rows that have the word "Hello" in Column A. Not knowing how many rows match the filter I use source.length to tell getRange() how many rows to output.
Code.gs
function test() {
try {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var copySheet = ss.getSheetByName("Sheet1");
var pasteSheet = ss.getSheetByName("Sheet2");
// get source range
var range = copySheet.getDataRange(); // Get all values
var source = range.getValues();
source = source.filter( row => row[0] === "Hello" );
var destination = pasteSheet.getRange(pasteSheet.getLastRow()+1,1,source.length,source[0].length).setValues(source);
// clear source values
range.clearContent();
Browser.msgBox('Commande Confirmer');
}
catch(err) {
console.log(err);
}
}
Reference
Sheet.getDataRange()
Array.filter()
I have been using a script in my google sheet that I found here:
copy data from one sheet to another in google app script and append a row, one small issue
var ss = SpreadsheetApp.getActiveSpreadsheet();
var copySheet = ss.getSheetByName("Copy");
var pasteSheet = ss.getSheetByName("Paste");
// get source range
var source = copySheet.getRange(2,2,12,2);
// get destination range
var destination = pasteSheet.getRange(pasteSheet.getLastRow()+1,2,12,2);
// copy values to destination range
source.copyTo(destination);
// clear source values
source.clearContent();
}
When I am running it now it gives me an error:
The coordinates of the target range are outside the dimensions of
the sheet.
which wasn't coming up before. Does anyone have an idea why it is doing this?
Issue:
The error is in this line:
var destination = pasteSheet.getRange(pasteSheet.getLastRow()+1,2,12,2);
pasteSheet.getLastRow() returns a row number which is at the very bottom of your sheet. Then you consider 12 rows range but the pasteSheet does not have 12 rows after the last row with content.
Solutions:
Add more rows in the pasteSheet:
or you can use insertRowsAfter:
pasteSheet.insertRowsAfter(pasteSheet.getLastRow(), 12)
Code snippet:
function myFunction() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var copySheet = ss.getSheetByName("Copy");
var pasteSheet = ss.getSheetByName("Paste");
// get source range
var source = copySheet.getRange(2,2,12,2);
// insert rows to make sure you have enough space
pasteSheet.insertRowsAfter(pasteSheet.getLastRow(), 12)
// get destination range
var destination = pasteSheet.getRange(pasteSheet.getLastRow()+1,2,12,2);
// copy values to destination range
source.copyTo(destination);
// clear source values
source.clearContent();
}
I am using below sample code to copy only values from one sheet and paste to another. But here it paste my formula also from source file. I want only value and format(dont want formula) to be copied from source and paste to destination sheet.
how it can be done in google spreadsheet scripts?
function copyInfo() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var copySheet = ss.getSheetByName("Copy");
var pasteSheet = ss.getSheetByName("Paste");
// get source range
var source = copySheet.getRange(2,2,12,2);
// get destination range
var destination = pasteSheet.getRange(pasteSheet.getLastRow()+1,2,12,2);
// copy values to destination range
source.copyTo(destination);
// clear source values
source.clearContent();
}
I have almost same kind of situation. I have to copy data form sheet "Important work" 'range B4 : M getlast row' and paste it into "sheet 8" below last row
please correct this code ...
function copyInfo() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var copySheet = ss.getSheetByName("Important work");
var pasteSheet = ss.getSheetByName("Sheet8");
// get source range
var source = copySheet.getRange(4,2,copySheet.getLastRow(),13);
var source1 = source.getValues()
Logger.log(source1);
// get destination range
var paste1 = pasteSheet.setRange(pasteSheet.getLastRow()+1,1,1,13);
// copy values to destination range
source1.setValues(paste1);
// clear source values
source1.clearContent();
}
I am trying to copy data from a spreadsheet and append it to another AND exclude header values.
I've tried inputing range values on current range methods but they won't accept strings. I am getting this error message: Cannot find method getDataRange(string). (line 63, file "Code").
Is there a way around this? Should I approach this differently?
function copyData(ssA, ssB) {
//Locates spreadsheets: Source and Destination.
var ssA = SpreadsheetApp.getActiveSpreadsheet(); //Leads to Spreadsheet A (source) which is active.
var ssB = ('spreadsheeturlgoeshere'); //Leads to Spreadsheet B (destination) URL.
//Source document stuff.
//Opens source document.
var sss = SpreadsheetApp.getActiveSpreadsheet();
//Opens source sheet.
var ss = sss.getSheetByName('Input');
//Gets full range of data.
var sRange = ss.getDataRange('a2:k');
//Gets number of rows.
var sValueLength = ss.getDataRange().getValues().length;
//Gets A1 notation identifying range of data.
var A1Range = sRange.getA1Notation();
//Gets the data values in range.
var sData = sRange.getValues();
//Destination document stuff.
//Opens destination document.
var dss = SpreadsheetApp.openByUrl(ssB);
//Opens destination sheet.
var ds = dss.getSheetByName('Output');
//Gets last row from destination sheet.
var dsLastRow = ds.getLastRow();
var dsLastColumn = ds.getLastColumn();
//Actions.
//Clears the Google Sheet before copy
ds.clear({contentsOnly: false});
//Finds last row on destination sheet and adds source values.
ds.getRange(dsLastRow+1, 1, sValueLength, dsLastColumn).setValues(sData);
}
Try this:
function copyData(ssA, ssB) {
var sss=SpreadsheetApp.getActive();
var dss=SpreadsheetApp.openByUrl('spreadsheeturlgoeshere');//I would just open by id
var sh=sss.getSheetByName('Input');
var rg=sh.getRange(2,1,sh.getLastRow()-1,11);
var sData=rg.getValues();
var dsh=dss.getSheetByName('Output');
dsh.getRange(dsh.getLastRow()+1,1,sData.length,sData[0].length).setValues(sData);
}