I am trying to copy paste a row (A3:S3) to the next free row of my sheet.
This works fine with the following code.
What I am now trying to do is to delete the cells C3-K3.
function copypastescript() {
var reference = 'Definitionstabelle!A2';
var rng = SpreadsheetApp.getActiveSpreadsheet().getRange(reference);
rng.setValue(rng.getValue()+1);
var ss = SpreadsheetApp.getActiveSpreadsheet ();
var ss = SpreadsheetApp.getActiveSpreadsheet ();
var source = ss.getRange ("Definitionstabelle!A3:S3");
var destSheet = ss.getSheetByName("Definitionstabelle");
var values = source.getValues().filter(function(e) {return e.some(function(f) {return f})});
destSheet.getRange(destSheet.getLastRow() + 1, 1, values.length, values[0].length).setValues(values);
}
Answer for Q1:
You want to delete the cells "C3:K3" of the sheet of Definitionstabelle after the values of "A3:S3" are copied.
If my understanding is correct, how about this modification?
Modified script:
Please add the following script after destSheet.getRange(destSheet.getLastRow() + 1, 1, values.length, values[0].length).setValues(values);.
ss.getRange ("Definitionstabelle!C3:K3").clearContent();
or
ss.getRange ("Definitionstabelle!C3:K3").clear();
References:
clearContent()
clear()
If I misunderstood your question and this was not the result you want, I apologize.
Answer for Q2:
some code to copy K3 and paste it into J3 before deleting C3:I3 and K3
You want to copy the value of "K3" to "J3" of the sheet "Definitionstabelle".
You want to copy before the vaues of cells "C3:K3" are deleted.
In your situation, should the copy from "K3" to "J3" be done before the copy of "A3:S3"?
If my understanding is correct how about this modification?
Please put the following script before ss.getRange ("Definitionstabelle!C3:K3").clear();.
Modified script:
function copypastescript() {
var reference = 'Definitionstabelle!A2';
var rng = SpreadsheetApp.getActiveSpreadsheet().getRange(reference);
rng.setValue(rng.getValue()+1);
var ss = SpreadsheetApp.getActiveSpreadsheet ();
var source = ss.getRange ("Definitionstabelle!A3:S3");
var destSheet = ss.getSheetByName("Definitionstabelle");
// Added
destSheet.getRange("K3").copyTo(destSheet.getRange("J3"), {contentsOnly:true}); // or destSheet.getRange("K3").copyTo(destSheet.getRange("J3"))
var values = source.getValues().filter(function(e) {return e.some(function(f) {return f})});
destSheet.getRange(destSheet.getLastRow() + 1, 1, values.length, values[0].length).setValues(values);
// Added
ss.getRange ("Definitionstabelle!C3:K3").clearContent(); // or ss.getRange ("Definitionstabelle!C3:K3").clear()
}
Reference:
copyTo(destination, options)
Related
Good morning,
I am using the following script right now to get the following situation: For a lead list which is dynamic so constantly moves, we want to extract certain information if they fall in a specific category. "C-Grade"
Essentially all data from all "C Grade" rated people, need to be copied from "Data info" onto "Lead info"
Normally I would use =vlookup
but since its dynamic and constantly changing, I don't see the option to use a formula.
App Script I am using right now:
function copyInfo() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var copySheet = ss.getSheetByName("Data info");
var pasteSheet = ss.getSheetByName("Lead info");
// get source range
var source = copySheet.getRange(1,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();
}
Here is the example file: Example file
I believe your goal is as follows.
You want to copy the rows which have the value of C Grade in the column "D" of "Data info" sheet to "Lead info" sheet.
In this case, how about the following modification?
Modified script:
function copyInfo() {
var check = "C Grade"; // This is from your question.
var ss = SpreadsheetApp.getActiveSpreadsheet();
var copySheet = ss.getSheetByName("Data info");
var pasteSheet = ss.getSheetByName("Lead info");
var values = copySheet.getRange("A2:D" + copySheet.getLastRow()).getValues().filter(r => r[3] == check);
pasteSheet.getRange(pasteSheet.getLastRow() + 1, 1, values.length, values[0].length).setValues(values);
}
When I saw your script, the value of "L1" of "Data info" is copied to "B1:C12" of "Lead info". When I saw your provided Spreadsheet, "L1" of "Data info" has no value.
In this modification, the values of "Data info" are retrieved and filtered by "C Grade" of the column "D", and the filtered values are put to "Lead info" sheet.
Reference:
filter()
Added 1:
When I saw your provided sample Spreadsheet again, I noticed that your script has been changed. And, I noticed that 2 points are different from your initial sample Spreadsheet.
In your initial Spreadsheet, "ABC Grade" was column "D". But, in your current Spreadsheet, that is colum "E".
In my proposed script, I proposed var values = copySheet.getRange("A2:D" + copySheet.getLastRow()).getValues().filter(r => r[3] == check);. But in your current script, that is changed to var values = copySheet.getRange("A1:D10" + copySheet.getLastRow()).getValues().filter(r => r[0] == check);/
I think that the reason for your current issue is that you changed your initial Spreadsheet and my proposed script. When your current Spreadsheet is used, the sample script is as follows.
Sample script:
function copyInfo() {
var check = "C Grade"; // This is from your question.
var ss = SpreadsheetApp.getActiveSpreadsheet();
var copySheet = ss.getSheetByName("Data info");
var pasteSheet = ss.getSheetByName("Lead info");
var values = copySheet.getRange("A1:E" + copySheet.getLastRow()).getValues().filter(r => r[4] == check);
if (values.length == 0) return;
pasteSheet.getRange(pasteSheet.getLastRow() + 1, 1, values.length, values[0].length).setValues(values);
}
When values has no value, an error occurs. So, I added if (values.length == 0) return;.
Added 2:
From your following new 2nd question,
Would there also be a way to only get certain colums. In this case A,B,D & E.
In this case, how about the following sample script?
Sample script:
function copyInfo() {
var check = "C Grade"; // This is from your question.
var ss = SpreadsheetApp.getActiveSpreadsheet();
var copySheet = ss.getSheetByName("Data info");
var pasteSheet = ss.getSheetByName("Lead info");
var values = copySheet.getRange("A1:E" + copySheet.getLastRow()).getValues().filter(r => r[4] == check).map(r => [1, 2, 4, 5].map(e => r[e - 1]));
if (values.length == 0) return;
pasteSheet.getRange(pasteSheet.getLastRow() + 1, 1, values.length, values[0].length).setValues(values);
}
i have made this in excel but cannot use it online. so i made it in google sheets but it does not work. What i want to do:
data ( values only, no formats) from sheet Tmrw range C3:Y20 copy to sheet Today range C3:y20, and after that clear data from sheet Tmrw range C3:Y20
i used this code:
function moveValuesOnly () {
var ss = SpreadsheetApp.getActiveSpreadsheet ();
var source = ss.getRange ("Tmrw"!C3:Y20");
var destSheet = ss.getSheetByName("Today");
var destRange = destSheet.getRange("Today!C3:Y20");
source.copyTo (destRange,"Today!C3:Y20);
source.clear ();
}
it does not work. i am not surprised.
the file is here:
https://docs.google.com/spreadsheets/d/11uyaRe7khP9PywXKkEh5e5xWLvm68Nw58R6dHkaTxvk/edit?usp=sharing
Modification points:
When getRange of Class Spreadsheet, you can include the sheet name to the A1Notation.
In this case, ss.getRange("'Today'!C3:Y20") can be used instead of ss.getSheetByName("Today");.
"Tmrw"!C3:Y20" and "Today!C3:Y20 are not enclosed by ". In this case, please modify to "Tmrw!C3:Y20" and "'Tmrw'!C3:Y20", and "Today!C3:Y20" and "'Today'!C3:Y20".
In your situation, I thought that the arguments of copyTo of Class Range is copyTo(destination).
When above points are reflected to your script, it becomes as follows.
Modified script:
function moveValuesOnly() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var source = ss.getRange("'Tmrw'!C3:Y20");
var destRange = ss.getRange("'Today'!C3:Y20");
source.copyTo(destRange);
source.clear();
}
Note:
When you want to use getSheetByName, you can also modify your script as follows.
function moveValuesOnly() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var srcSheet = ss.getSheetByName("Tmrw");
var source = srcSheet.getRange("C3:Y20");
var dstSheet = ss.getSheetByName("Today");
var destRange = dstSheet.getRange("C3:Y20");
source.copyTo(destRange);
source.clear();
}
References:
A1 notation
getRange(a1Notation) of Class Spreadsheet
copyTo(destination) of Class Range
getSheetByName(name)
My issue is, I have data being moved from one Sheet to another via script when marked "Complete". In the "Completed" sheet is where I want to sort the data. I already have an Autosort script that works only when debugging, not even on edit (tried editing everywhere), Let alone when data is moved to the sheet.
I want to be able to Auto sort whenever data is moved to the new sheet (I don't think its an onEdit function?)
I am not a programmer and I'm fairly new to Google Apps Script. I just dabble into code once in a while because I find it interesting, so I don't even know where to start to get the results I'm looking for.
This is the Auto Sort,
function AutoSortOnEdit() {
var sheetNames = ["Complete"];
var ss = SpreadsheetApp.getActiveSpreadsheet();
sheetNames.forEach(function(name) {
var sheet = ss.getSheetByName(name);
var range = sheet.getRange(4, 1, sheet.getLastRow() - 1,
sheet.getLastColumn());
range.sort({column: 2, ascending: true});
});
}
EDIT: I realized I was using a script for multiple sheets. I changed it for one sheet, same issues though.
function AutoSortOnEdit() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Completed Returns");
var range = sheet.getRange(4, 1, sheet.getLastRow() - 1,
sheet.getLastColumn());
range.sort({column: 2, ascending: true});
}
Any help would be greatly appreciated!
I managed to get it working by throwing the 2 scripts together under 1 function like so. I could not figure out how to get the event function to run while calling multiple functions, so I just combined them. Above the 2 "}" brackets is the move Script and below them is the Auto sort.
function onEdit(e) {
var ss = e.source;
var s = ss.getActiveSheet();
var r = e.range;
var actionCol = 24;
var nameCol = 24;
var rowIndex = r.getRowIndex();
var colIndex = r.getColumnIndex();
var colNumber = s.getLastColumn();
if (e.value == "TRUE" && colIndex == actionCol) {
var targetSheet = ss.getSheetByName("Completed Returns");
if (ss.getSheetByName("Completed Returns")) {
var targetSheet = ss.getSheetByName("Completed Returns");
var targetRange = targetSheet.getRange(targetSheet.getLastRow()+1, 1, 1, colNumber);
var sourceRange = s.getRange(rowIndex, 1, 1, colNumber);
sourceRange.copyTo(targetRange);
s.deleteRow(rowIndex);
}
}
var sheet = ss.getSheetByName("Completed Returns");
var range = sheet.getRange(4, 1, sheet.getLastRow() - 1, sheet.getLastColumn());
range.sort({column: 2, ascending: true});
}
Thanks Everyone for the response!
You can add your function to one another
function AutoSortOnEdit() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('Completed Returns');
var range = sheet.getRange(
4,
1,
sheet.getLastRow() - 1,
sheet.getLastColumn()
);
range.sort({ column: 2, ascending: true });
}
function copyData() {}
function userActionRun() {
copyData();
AutoSortOnEdit();
}
At now you can bind it ti EDIT event
function onEdit(){
userActionRun();
}
You can use the Sort function to sort a data-set.
If you have a function that moves data form one sheet to another when the row is marked as complete, then you can add a line at the end of that to sort it.
You could also create a FilterView that sorts the whole sheet based on your parameters. Unlike Filters, FilterViews are saved between sessions.
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.
I am trying to copy a certain area into the next empty row of another spreadsheet.
Right now I am only able to copy it to a certain destination.
This is the code I am currently using:
function myFunction() {
var sheet=SpreadsheetApp.getActiveSheet();
var source="A3:S3";
var destination="Historie!B3";
sheet.getRange(source).copyTo(sheet.getRange(destination));
}
try something like this:
function xxx() {
var reference = 'sheet1!A2';
var rng = SpreadsheetApp.getActiveSpreadsheet().getRange(reference);
rng.setValue(rng.getValue()+1);
var ss = SpreadsheetApp.getActiveSpreadsheet ();
var ss = SpreadsheetApp.getActiveSpreadsheet ();
var source = ss.getRange ("Sheet1!A3:S3");
var destSheet = ss.getSheetByName("Historie");
var values = source.getValues().filter(function(e) {return e.some(function(f) {return f})});
destSheet.getRange(destSheet.getLastRow() + 1, 1, values.length, values[0].length).setValues(values);
}