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:
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);
}