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).
Related
I'm looking to save data from a particular cell to another sheet. On said sheet I need it to save to the next available row within a defined column.
I have the script below that will save the data to the next available row, but am unsure of how to define the column.
In the example below, I'd need it to be saved into the 'A' column.
function transferList() {
var sourceSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Cancellations");
var sourceData = sourceSheet.getRange("D5").getValues();
var targetSS = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet12");
var targetRangeTop = targetSS.getLastRow();
targetSS.getRange(targetRangeTop+1,1,sourceData.length,sourceData[0].length).setValues(sourceData);
}
Issue:
If I understand you correctly, you want to write this to the first empty cell of a specific column (which may not correspond to the first empty row in the sheet).
Solution:
I'd suggest using Range.getNextDataCell to find the last cell before the first empty one, and Range.offset to get that empty one.
Code sample:
function transferList() {
var columnIndex = 1; // Column A
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sourceSheet = ss.getSheetByName("Cancellations");
var sourceData = sourceSheet.getRange("D5").getValue();
var targetSS = ss.getSheetByName("Sheet12");
var a1Range = targetSS.getRange(1,columnIndex);
var targetRange;
if (a1Range.offset(1,0).getValue() !== "") targetRange = a1Range.getNextDataCell(SpreadsheetApp.Direction.DOWN).offset(1,0);
else targetRange = targetSS.getRange(2,columnIndex);
targetRange.setValue(sourceData);
}
Note:
Since you are getting a value from a single cell, there's no need to use getValues and setValues; you can use getValue and setValue instead.
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".
I'm attempting to switch from Excel to Google Sheets and I already have a macro for this, but you can't run macros on Google spreadsheet. What I want is to move the Value in Log-in!C5 to another sheet "w1" starting in column A3. The code I have works but instead of moving it in "w1!A3" it was placed on cell A146 since it was the next empty row. Is there a way for the value to be moved in first empty row in Column A? regardless that the other columns are not empty?
Here is my code
function moveValuesOnly() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var source = ss.getRange('Log-in!C5');
var destSheet = ss.getSheetByName('w1');
var destRange = destSheet.getRange(destSheet.getLastRow()+1,1);
source.copyTo (destRange, {contentsOnly: true});
source.clear ();
}
getLastRow() only gets the lastRow of the sheet and not the range.
If you only want w1!A3,hardcode it directly: Try Changing from
destSheet.getRange(destSheet.getLastRow()+1,1);
to
destSheet.getRange(3,1);
Alternatively,To find the first empty cell in a range,
var colA = destSheet.getRange('A:A').getValues();
Code1:
//Filter 2D array to 1D
var fcolA = colA.filter(function (e) {return e[0];});
var firstEmptyRowIncolA = fcolA.indexOf('')+1;
or
Code2:
for(i=0;i<colA.length;i++){
if(colA[i][0] == ''){
var firstEmptyRowIncolA = i+1;
break;
}
}
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})
}
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