Paste into a Specific Column - Google Sheets - google-apps-script

Good afternoon, I made this script.
But, it's pasting in column A and I need to paste it in column E.
How can I do this?
function Teste() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var source = ss.getRange('Entradas!B9:H29');
var spreadsheet = SpreadsheetApp.getActive();
var destSheet = ss.getSheetByName('BD');
var lastFilledRowInColumnE = getLastPopulatedRow(destSheet.getRange('E:K').getValues());
var destRange = destSheet.getRange(lastFilledRowInColumnE+1,1);
source.copyTo(destRange, {contentsOnly: true});
};

I think the problem could be with the line:
var destRange = destSheet.getRange(lastFilledRowInColumnE+1,1);
There are a few different getRange functions. If you only supply two arguments, you are calling the getRange(row, column) version. You are pasting into the row "lastFilledRowInColumnE+1" and column 1, which is A.
You may simply need to write "5" instead of "1" to represent column "E".

Related

Copy -Paste into next available row in selected column

I am trying to write a script that is tied to a button so that when clicked it copies my selected range into the next available row based on contents in column A. I have formulas in column D and currently when ran it pastes into the next available row but I would like it to paste into the next available row based on if column A has data.
Here is what I have so far
function myFunction() {
var ss = SpreadsheetApp.getActiveSpreadsheet ();
var source = ss.getRange ("JOB BUILD!A3:E21");
var destSheet = ss.getSheetByName("JOB IN PROGRESS");
var destRange = destSheet.getRange(destSheet.getLastRow()+1,1);
source.copyTo (destRange, {contentsOnly: false})
}
If you want to paste the values based on the last available row from a certain column, you will have to find the last row of that column.
In order to get the last row of a certain row, you can use this method from this answer. Assuming you want to paste the values based on column A from the destSheet, you can try the below code:
function myFunction() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sourceSheet = ss.getSheetByName("JOB BUILD");
var sourceData = sourceSheet.getRange("A3:E21");
var destSheet = ss.getSheetByName("JOB IN PROGRESS");
var aRange = destSheet.getRange("A1:A").getValues();
var colA = aRange.filter(String).length;
var destRange = destSheet.getRange(colA, "COL_NO", "NO_OF_ROWS", "NO_OF_COLS");
sourceData.copyTo(destRange, {
contentsOnly: false
})
}
Note
Please bear in mind that depending on the exact structure of your data from the two sheets, you will have to adjust the ranges.
Reference
Determining the last row in a single column;
Apps Script Sheet Class - getRange(row, column, numRows, numColumns).

How optimize code to copy one sheet range to another sheet range

My code below works, but I thinking that there's a much more elegant way of doing it. I want to copy a portion of a selected row (D:R) on the Customer sheet (source) to the Test sheet (destination) to the first blank row in that sheet (C:Q).
The code works, but just looks non-performant/non-elegant.
function rowSelected() {
var customer_sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Customer_Info");
var invoice_sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Test");
var customer_selection_row = customer_sheet.getActiveRange().getRow();
//Browser.msgBox(customer_selection_row);
var customer_selection_start = 'D' + customer_selection_row;
var customer_selection_end = 'R' + customer_selection_row;
var customer_selection_range = customer_selection_start + ':' + customer_selection_end;
var source_range = customer_sheet.getRange(customer_selection_range);
customer_sheet.setActiveRange(source_range);
//Browser.msgBox(source_range.getA1Notation());
var invoice_selection_row = getFirstEmptyRowWholeRow();
var invoice_selection_start = 'C' + invoice_selection_row;
var invoice_selection_end = 'Q' + invoice_selection_row;
var invoice_selection_range = invoice_selection_start + ':' + invoice_selection_end;
var destination_range = invoice_sheet.getRange(invoice_selection_range);
invoice_sheet.setActiveRange(destination_range);
//Browser.msgBox(destination_range.getA1Notation());
customer_sheet.setActiveRange(source_range).copyTo((destination_range), {contentsOnly:true});
}
I'm thinking there is some way of doing this with less code.
You want to copy the column "D" to "R" at the row of the active range of the sheet Customer_Info to the column "C" to "Q" at the row retrieved by getFirstEmptyRowWholeRow() of the sheet Test.
You want to activate the source range.
If my understanding is correct, how about this modification?
Modification points:
SpreadsheetApp.getActiveSpreadsheet() is declared one time as `ss``.
getRange(row, column, numRows, numColumns) is used instead of getRange(a1Notation).
About destination_range, when the start cell is set, the source range can be copied from it.
When the active sheet is Customer_Info, the script is run.
Modified script:
function rowSelected() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var customer_sheet = ss.getActiveSheet();
if (customer_sheet.getSheetName() == "Customer_Info") {
var invoice_sheet = ss.getSheetByName("Test");
var activeRange = ss.getActiveRange();
var source_range = customer_sheet.getRange(activeRange.getRow(), 4, activeRange.getNumRows(), 15).activate();
var destination_range = invoice_sheet.getRange(getFirstEmptyRowWholeRow(), 3); // or invoice_sheet.getRange("C" + getFirstEmptyRowWholeRow())
source_range.copyTo(destination_range, {contentsOnly: true});
}
}
getFirstEmptyRowWholeRow() of your script is also used for above modified script.
In this modified script, if you want to select the range of "A1:A3" on the sheet Customer_Info and run the script, the values of cells "D1:R3" are copied from the cell of the column "C" and the row of getFirstEmptyRowWholeRow().
References:
getRange(row, column, numRows, numColumns)
activate()
If I misunderstood your question and this was not what you want, I apologize.

Script for a button in google sheets

I created a button that does the following:
A. Copy a group of columns from sheet 1 (daily sports) and paste them into sheet 2 (Record).
B. The columns pasted in sheet 2 go to the next empty column (or create a new column) so they don't over-right the info that's already there..
C. Every time I'm done putting in new information I press the button and it automatically gets copied to sheet 2.
This is the code I'm using:
function moveValuesOnly () {
var ss = SpreadsheetApp.getActiveSpreadsheet ();
var source = ss.getRange ("Daily sports!B2:D20");
var destSheet = ss.getSheetByName("Record");
var destRange = destSheet.getRange(destSheet.getLastRow(),1);
var destRange = destSheet.getRange(destSheet.getLastColumn(),2);
source.copyTo (destRange, {contentsOnly: true});
source.clear ();}
The problems I'm having are as follows
1) Every time I hit the button it does not create a new column, instead it just covers the old columns.
2) the original source gets cleared (I know, It's the last function, it clearly says "clear" I just don't know what to write instead... I want the source to stay as is)
If anyone could help me with this script that would be great..
Thank you!
The following code should do what are you looking for:
function moveValuesOnly() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var source = ss.getRange ("Daily sports!B2:D20");
var destSheet = ss.getSheetByName("Record");
var row = 1; //the starting row of the range
var column = destSheet.getLastColumn()+1; //the starting column of the range
var numRows = 19; //the number of rows to return
var numColumns = 3; //the number of columns to return
var destRange = destSheet.getRange(row, column, numRows, numColumns);
source.copyTo(destRange, {contentsOnly: true})
}

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

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