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()
Related
I want to copy manually selected/highlighted non-adjacent rows and paste them directly below each other on another sheet ("fight list raw"). With the script below it works fine, as long as the rows are adjacent. If the rows are non-adjacent, only one row is pasted (the row which was selected lastly / where the cursor is in).
Has someone an idea how to manage it?
function copynonadjacent rows() {
SpreadsheetApp.flush();
var ss = SpreadsheetApp.getActiveSpreadsheet();
var target = SpreadsheetApp.getActiveSpreadsheet();
var source_sheet = ss.getActiveSheet();
var source_range = source_sheet.getActiveRange();
var target_sheet = target.getSheetByName("fight list raw");
var last_row = target_sheet.getLastRow();
target_sheet.insertRowsAfter(last_row,1);
source_range.copyTo(target_sheet.getRange("A"+(last_row+1+":AA"+(last_row+1))),{contentsOnly:true});
}
Thanks a lot!
From your following reply,
I want to copy row number 2 and row number 4. When I select the two rows by clicking on the row numbers and run the script, only row number 4 is copied.
But if I select rows 2 and 3 (so no rows between them), the script copies both rows. In the destination sheet the rows are pasted below each other without a row between them. I want this the same way, when I copy row 2 and 4.
In your script, you use var source_range = source_sheet.getActiveRange();. I thought that this might be the reason for your issue. In this case, how about the following modification?
Modified script:
function myFunction() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
// Source sheet.
var source_sheet = ss.getActiveSheet();
var source_ranges = source_sheet.getActiveRangeList().getRanges();
var values = source_ranges.flatMap(r => r.getValues());
var len = values.length;
// Target sheet.
var target_sheet = ss.getSheetByName("fight list raw");
var last_row = target_sheet.getLastRow();
target_sheet.insertRowsAfter(last_row, len);
target_sheet.getRange(last_row + 1, 1, len, values[0].length).setValues(values);
}
When this script is run, the selected rows are retrieved, and copy the values to the target sheet.
I thought that you use {contentsOnly:true}. So, I thought that getValues and setValues might be able to be used.
Reference:
getActiveRangeList()
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
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'm copying data from one sheet to another 3x per week. The triggers are working fine but the data is supposed to copy to a new row sequentially. However, right now it's copying with varying numbers of rows in between them and I cant figure out why.
I have tried deleting all of the data in the sheet, deleting the sheet and recreating it, and double checked how I've input what data needs to be copied.
var logSheet = ss.getSheetByName('Sheet1');
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sourceSheet = ss.getSheetByName('Data Sheet');
var RDSheet = ss.getSheetByName('RawDataSheet');
var lastRow = logSheet.getLastRow();
var RawData = sourceSheet.getRange(45,1,1,34);
RawData.copyTo(RDSheet.getRange(lastRow + 1, 1), {contentOnly: false});
I expect the range (45,1,1,34), one long row, to copy itself to the RawDataSheet and when it triggers it places the data 1 row below the previously copied data.
Try this:
function myfunction() {
var ss=SpreadsheetApp.getActive();
var logSheet=ss.getSheetByName('Sheet1');
var sourceSheet=ss.getSheetByName('Data Sheet');
var RDSheet=ss.getSheetByName('RawDataSheet');
var lastRow=logSheet.getLastRow();
var RawData=sourceSheet.getRange(45,1,1,34);
RawData.copyTo(logSheet.getRange(lastRow + 1, 1), {contentOnly: false});
}
I want to take the last response from a Google Form and copy it to the same range on another sheet.
The second sheet has some data in columns A & B (a login and password) that I want to assign as somebody completes the registration form.
I want the data from column B to H from the form response sheet to be copied to column C to I on the sheet that contains the login/password columns when a response is received.
I know I am missing the code to take the range from the last row, but I know NO coding at all and my current script is compiled from things I've found around the web and the reference material.
Any advice would be greatly appreciated.
function onEdit(e) {
var spreadsheet = SpreadsheetApp.openById("sheetid");
var sheet = spreadsheet.getSheets()[0];
var lastrow = sheet.getLastRow();
var range = sheet.getRange(2,2,1,7);
values = range.getValues();
var ss = SpreadsheetApp.getActiveSpreadsheet();
var SSsheet = ss.getSheets()[1];
var ssRange = SSsheet.getRange(2,3,1,7);
ssRange.setValues(values);
}
Try using copyTo(destination). It copies the data from a range of cells to another range of cells. Both the values and formatting are copied.
// The code below will copy the first 5 columns over to the 6th column.
var sheet = SpreadsheetApp.getActiveSheet();
var rangeToCopy = sheet.getRange(1, 1, sheet.getMaxRows(), 5);
rangeToCopy.copyTo(sheet.getRange(1, 6));
}
You can also try using copyValuesToRangecopyValuesToRange.
For additional code samples, you may also refer to this forum.
function onEdit(e) {
var spreadsheet = SpreadsheetApp.openById("ID");
var sheet = spreadsheet.getSheets()[0];
var lastRow = sheet.getLastRow();
var range = sheet.getRange("A"+(lastRow)+":G"+(lastRow));
var values = range.getValues();
var ss = SpreadsheetApp.getActiveSpreadsheet();
var SSsheet = ss.getSheets()[1];
var getRow = SSsheet.getLastRow() + 1;
var ssRange = SSsheet.getRange(getRow, 1, values.length, values[0].length);
ssRange.setValues(values);
Try with this,
this code copies the last value you want from a certain range and pastes it in the last available row of the other sheet.
The code it's the same only in the place where you are going to copy it, specify it to be the size of what I want to copy. I hope it helps you :D