Cannot find the bug in the CopyRow code from Google Sheets - google-apps-script

I'm looking for a code to copy 4 cells for the row that I've selected the cell from one sheet from another sheet of the same spreadsheet, on the last row that is written. The code is this:
function CopiarFila() {
//declaration of sheets
var ss = SpreadsheetApp.openById("ID");
var sheet = ss.getSheetByName("FORD 2019");
var sheetTo = ss.getSheetByName("FORD 2019-F");
//declaration of variables
var row = sheet.getActiveCell().getRow();
var range1 = sheet.getRange("B"+row+":B"+row).getValues();
var range2 = sheet.getRange("C"+row+":C"+row).getValues();
var range3 = sheet.getRange("F"+row+":F"+row).getValues();
var range4 = sheet.getRange("I"+row+":I"+row).getValues();
//declaration of the last row from the new sheet
var ultimaFila = sheetTo.getLastRow();
//adding a row after the last one
sheetTo.insertRowsAfter(ultimaFila, 1);
//copying values to the last row
sheetTo.getRange("B"+ultimaFila+":B"+ultimaFila).setValues(range1);
sheetTo.getRange("D"+ultimaFila+":D"+ultimaFila).setValues(range2);
sheetTo.getRange("C"+ultimaFila+":C"+ultimaFila).setValues(range3);
sheetTo.getRange("H"+ultimaFila+":H"+ultimaFila).setValues(range4);`
}
As I see when running the code, the row is created so the problem isn't from the commands: Sheet, SheetTo and ultimaFila. Am I using properly the command '.getValues('?
I'm stuck and I cannot find a way to fix it. Thank you for your help!

IF you don't mind the blank columns, there is an easier way to do this,
var ss1 = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("FORD 2019");
var ss2 = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("FORD 2019-F");
ss1.getRange("B:B").copyTo(ss2.getRange("B:B"));
ss1.getRange("C:C").copyTo(ss2.getRange("D:D"));
ss1.getRange("F:F").copyTo(ss2.getRange("C:C"));
ss1.getRange("I:I").copyTo(ss2.getRange("H:H"));

Related

Google Sheets Script - Copying data range from one sheet to another - whole range not copying

Bit of a rookie at this and learning atm. I am using the following script to copy data from one sheet to another in the same Google spreadsheet:
function recordHistory() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Cover");
var source = sheet.getRange("a1:c21");
var values = source.getValues();
var sheet2 = ss.getSheetByName("January 2020")
values [0] [0] = new Date();
sheet2.appendRow(values [0])
}
Which works fine to copy a single row but does not copy the entire range listed (a1:c21). Now I imagine the problem lies around Row 8 but I am not sure.
Here is the link to the spreadsheet I am using for trying this out:
https://docs.google.com/spreadsheets/d/15pEbNy-PmrGeAmpASRUK2iXIyJPfg43Mm2r6bbiTBuM/edit#gid=1296663899
function recordHistory() {
var ss = SpreadsheetApp.getActive();
var sheet = ss.getSheetByName("Cover");
var source = sheet.getRange("a1:c21");
var values = source.getValues();
var sheet2 = ss.getSheetByName("January 2020")
values [0][0] = new Date();
sheet2.getRange(sheet2.getLastRow()+1,1,values.length,values[0].length).setValues(values);
}

Using onEdit trigger, is there a way to make sure the last row in a Sheet is visible to users?

I have a Google Sheet that accumulates up to 100 entries per day. As users add new entries, the script puts new data into the last row (see snippet below). However, there are only 12 rows visible on the screen, so once those are filled, the user can't see new entries.
How can I make the last row visible to the user each time data is entered?
I am new to google-apps-script, so thanks for any ideas. I believe onEdit trigger no longer works with sheet.setActiveRange(), so I am looking for a work-around. If there's no simple solution, I'll take a clunky one. For example, can a script be used to hide rows above the last row?
var ss = SpreadsheetApp.getActiveSpreadsheet();
var SHEET_NAME = 'Sheet1' // Name of current sheet
var sheet = ss.getSheetByName(SHEET_NAME);
var dataCell = sheet.getRange('A1');
var lastRowNumber = sheet.getDataRange().getNumRows();
var newData = dataCell.getValue().split(",");
var lastRow = ss.getLastRow();
sheet.appendRow(newData);
Found a solution: setActiveRange(lastRow+2, lastColumn). Scrolls down far enough, without the need to get the range of the appended row. Added advantage is it scrolls before the row is appended, so user sees data as it goes in.
function onEdit(e) { // Puts csv user data into Sheet1
var ss = SpreadsheetApp.getActiveSpreadsheet();
var SHEET_NAME = 'Sheet1' // Name of current sheet
var sheet = ss.getSheets()[0];
var dataCell = sheet.getRange('A1');
var newData = dataCell.getValue().split(",");
// Gets the address of the very last cell of this sheet
var lastRow = sheet.getLastRow();
var lastColumn = sheet.getLastColumn();
// Scrolls to make sure user can see 2 rows below the current last cell
var lastCell = sheet.getRange(lastRow+2, lastColumn);
// Puts user entered data into a new row at the bottom of the spreadsheet
sheet.appendRow(newData);
sheet.setActiveRange(lastCell);
}
You could get the newly added range by adding 1 to the lastrow and activate it:
var lastRow = ss.getLastRow();
sheet.appendRow(newData);
sheet.getRange("A" + (lastRow+1)).activate();//activate the newly appended row's A cell

Copy a range from last row to new sheet

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

Re: Copy every row of Data from one sheet that has a given value to another sheet

I saw your answer in an old thread and i wanted to ask for clarification. This is the thread i am referring to: Copy every row of Data from one sheet that has a given value in column K to another sheet
My issue is, I'm using a version of your skeleton script, but i am having some trouble. Instead of the script copying the entire row, it seems to be copying the first column in the conditional rows and pasting the results of the array all in a single row.
Here is my script as is:
function formSubmitCriticalAltThree(){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Form Responses");
var lastRow = ss.getLastRow();
var target = ss.getSheetByName("Chad");
var data = sheet.getDataRange().getValues();
var line = new Array();
var targetArray= new Array();
for (n=0;n<data.length;++n){ // iterate rows
if (data[n][10] == "cbellani"){ // if condition is true copy the whole row to target
line.push(data[n]);// copy the whole row
}//if
}//for
if(line.length>0){// if there is something to copy
targetArray.push(line)}// add row to destination
target.getRange(3,1,targetArray.length,targetArray[0].length).setValues(targetArray);
}
Any help on this would be greatly appreciated.
I have not tested this, but I think it should look like this:
function formSubmitCriticalAltThree(){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Form Responses");
var lastRow = ss.getLastRow();
var target = ss.getSheetByName("Chad");
var data = sheet.getDataRange().getValues();
var targetArray = new Array();
for (var n = 0; n < data.length; ++n){ // iterate rows
if (data[n][10] == "cbellani") { // if condition is true copy the whole row to target
targetArray.push(data[n])}// add row to destination
}//if
}//for
target.getRange(3,1,targetArray.length,targetArray[0].length).setValues(targetArray);
}

Copy Columns from a sheet to add at lastcolumn in "archive" sheet with GAS

Sorry for my english. ^^
Erm, i´ve found a script to copy from a sheet to an other Sheet.
My problem is: its copy with rows.
I want to copy columns and add 2 columns to lastcolumn on script running.
It becames a problem, because columns has no a1notification. A=1, B=2, etc.
copyTo wants A1notification.
Heres my script:
function Archiv() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var target = SpreadsheetApp.openById("sheetid");
var source_sheet = ss.getSheetByName("sheetname");
var target_sheet = target.getSheetByName("sheetnametarget");
var source_range = source_sheet.getRange("D4:E89");
var last_col = target_sheet.getLastColumn();
target_sheet.insertColumnAfter(last_col);
var target_range = target_sheet.getRange(">>>>(lastcol+1)<<<<4:>>>>>(lastcol+2)<<<<89");
source_range.copyTo(target_range, {contentsOnly:true});
}
The script have to run every Monday to archive the content of 2 Columns.
I hope you can understand my wish.
I have tested many things, but nothing helps. :(
Many thanks in advance.
Sebastian
You can use another range selection mode that will makes things very simple, see this doc, using integers for row and column numbers.
Your code could become like this :
function Archiv() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var target = SpreadsheetApp.openById("sheetid");
var source_sheet = ss.getSheetByName("sheetname");
var target_sheet = target.getSheetByName("sheetnametarget");
var source_range = source_sheet.getRange("D4:E89");
var last_col = target_sheet.getLastColumn();
while(target_sheet.getMaxColumns()<=last_col+2){// add columns only if necessary
target_sheet.insertColumnAfter(last_col)}
var source_values = source_range.getValues();
var target_range = target_sheet.getRange(4,last_col+1,source_values.length,2);// row Nr4, starting on last column+1, 2 columns width and 89-4 rows heigth
target_range.setValues(source_values);
}
I used getValues() and setValues() instead of copyTo() that returned an error : target and source must be in the same spreadsheet