Google Script to Delete a Range of Empty Rows - google-apps-script

I need to use a button to delete empty rows in the range A1:A25 on Sheet1 in google sheets. Column A will be the reference column for empty cells. I cannot seem to find a simple answer on how to define the Range for the deletion of empty rows. I do not want it to delete empty rows below row 25.
// Deletes any row whose first column is blank
// WARNING: Assumes any valid row has data in column 1
function deleteBlankRows() {
var doc = SpreadsheetApp.getActiveSpreadsheet();
var sheet = doc.getSheetByName("Sheet1");
var lastRow = SpreadsheetApp.getActiveSheet().getLastRow();
for (var i = lastRow; i > 0; i--) {
var range = sheet.getRange(i,1);
var data = range.getValue();
if (data == '') {
sheet.deleteRow(i);
}
}
}

I believe your goal is as follows.
You want to delete rows when the column "A" is empty.
As a condition, you don't want to delete rows after row 25.
When your script is modified, how about the following modification?
From:
var lastRow = SpreadsheetApp.getActiveSheet().getLastRow();
To:
var lastRow = 25;
In this modification, when the column "A" of row 25 is empty, that row is deleted. When you want to leave row 25, please modify it to var lastRow = 26;.
Note:
In your script, when deleteRow() is run in the loop, when the process time is long, how about the folloging modified script? This modified script deletes the rows with the empty of column "A" before the row 25. If you want to leave row 25, please modify var v1 = values.splice(0, 25); to var v1 = values.splice(0, 26);.
function deleteBlankRows() {
var doc = SpreadsheetApp.getActiveSpreadsheet();
var sheet = doc.getSheetByName("Sheet1");
var range = sheet.getDataRange();
var values = range.getValues();
var v1 = values.splice(0, 25);
var res = [...v1.filter(([a]) => a.toString() != ""), ...values];
range.clearContent();
sheet.getRange(1, 1, res.length, res[0].length).setValues(res);
}
In this case, when you want to delete rows after row 25, please modify var res = [...v1.filter(([a]) => a.toString() != ""), ...values]; to var res = [...v1, ...values.filter(([a]) => a.toString() != "")];.
Added:
From your following reply,
The change to last row worked great. I, however, now need to stop the deletion at row 11. The code I have deletes all the way to row 1 (I know that is what is in my original post). Things changed in the last few minutes.
When you want to delete rows from 11 to 25 and when you want to modify your script, how about the following modification?
From:
var lastRow = SpreadsheetApp.getActiveSheet().getLastRow();
for (var i = lastRow; i > 0; i--) {
To:
var lastRow = 25;
for (var i = lastRow; i >= 11; i--) {
In this modification, the rows are deleted from 11 to 25.
If you will use my 2nd modification, how about the following modification?
function deleteBlankRows() {
var doc = SpreadsheetApp.getActiveSpreadsheet();
var sheet = doc.getSheetByName("Sheet1");
var range = sheet.getDataRange();
var values = range.getValues();
var v2 = values.splice(0, 25);
var v1 = v2.splice(0, 10);
var res = [...v1, ...v2.filter(([a]) => a.toString() != ""), ...values];
range.clearContent();
sheet.getRange(1, 1, res.length, res[0].length).setValues(res);
}

Related

Copying data from a sheet to another with conditions

I have been trying to copy some data from a sheet to another but I have been running into some trouble.
I have to copy data and stop copying until the scripts finds an empty space, and I have to paste this data into another sheet where there's blank space (available space).
This is the code I have so far:
function copyInfo() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var copySheet = ss.getSheetByName("sheet1");
var pasteSheet = ss.getSheetByName("sheet2");
var source = copySheet.getRange(11,1,1,10);
var destination = pasteSheet.getRange(1,1,1,10);
for (i = 1; i < 20; i++) {
if (destination.isBlank() == true) {
destination = pasteSheet.getRange(i, 1, 1, 10);
source = copySheet.getRange(i + 10, 1, 1, 10);
source.copyTo(destination);
} else {
destination = pasteSheet.getRange(i, 1, 1, 10);
}
}
}
It recognizes that the destination has an empty space, although it doesn't really paste it. The for (i = 1; i <20; i++) is for testing purposes.
If your data doesn't have any blank rows in between the first row and last non-empty row, then you can use this:
Sample Data:
sheet1
sheet2
Script:
function copyInfo() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var copySheet = ss.getSheetByName("sheet1");
var pasteSheet = ss.getSheetByName("sheet2");
// get last rows of each sheet
var sLastRow = copySheet.getLastRow();
var dLastRow = pasteSheet.getLastRow();
// get data from sheet1 from row 11 to last row
var source = copySheet.getRange(11,1,sLastRow - 10,10).getValues();
// paste data to sheet2's last row
pasteSheet.getRange(dLastRow + 1,1,source.length,source[0].length).setValues(source);
}
Output:
Alternative:
If you have blank rows in between your sheet1, you can filter the values in sheet1.
Sample Data:
blank row 13 in sheet1
function copyInfo() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var copySheet = ss.getSheetByName("sheet1");
var pasteSheet = ss.getSheetByName("sheet2");
// get last rows of each sheet
var sLastRow = copySheet.getLastRow();
var dLastRow = pasteSheet.getLastRow();
// get data from sheet1 from row 11 to last row
// exclude rows with blank values in all columns
var source = copySheet.getRange(11,1,sLastRow - 10,10).getValues()
.filter(row => row.filter(col => !col).length < row.length);
// paste data to sheet2's first blank row
pasteSheet.getRange(dLastRow + 1,1,source.length,source[0].length).setValues(source);
}
Output:

Google Script: copy values from one sheet to another determining the range

I have two sheets with unique IDs in one column. I need to make sure whenever a new ID is added in one sheet (Sheet1), it is copied to the last empty row in the other sheet (Sheet2). The IMPORTRANGE won't work as being dynamic any static information added in the other sheet would be irrelevant to the respective ID.
In another thread [1], I got help developing this script that will do exactly that. However, this script will insert the data in the other sheet in Column A. How can I modify the script so I can decide the specific range where I want the script to insert the data (in Sheet 2).
Update: I created this spreadsheet as an example. I'm trying to ensure that the script does the calculation (ie. adding vlaues that are not duplicated in the first available empty row), starting in cel C10 in "Sheet2". It would be also great if I can somehow change that range if I need to: https://docs.google.com/spreadsheets/d/1abESAXFrOHoqRQRNqQGfmAFxlbD10wlprAf1tca4y7o/edit#gid=132361488
Thanks!
function updateSheet() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sourceSheet = "Sheet1";
var destinationSheet = "Sheet2";
var source_sheet = ss.getSheetByName(sourceSheet);
var target_sheet = ss.getSheetByName(destinationSheet);
var lastCol = target_sheet.getLastColumn();
var lastRow = target_sheet.getLastRow();
if (lastRow > 1) { // <--- Added
var r = target_sheet.getRange(2,1, lastRow - 1, lastCol);
r.sort([{column: 1, ascending: true}]);
}
_updateSpreadsheet(source_sheet, target_sheet);
}
function _updateSpreadsheet(source_sheet, target_sheet) {
var last_row = target_sheet.getLastRow();
var source_data = source_sheet.getRange("A4:A" + source_sheet.getLastRow()).getValues(); // <--- Modified
var target_data = target_sheet.getDataRange().getValues();
var resultArray = [];
for (var n = 0 ; n < source_data.length ; n++) { // <--- Modified
var keep = true;
for(var p in target_data) {
if (source_data[n][0] == target_data[p][0]) {
keep = false; break;
}
}
var columnsToKeep = [0];
var tempData = [];
if(keep){
for(var c in columnsToKeep){ tempData.push(source_data[n][columnsToKeep[c]])}
resultArray.push(tempData);
}
}
last_row++;
resultArray = resultArray.filter(String); // <--- Added
if(resultArray.length>0){
target_sheet.getRange(last_row,1,resultArray.length,resultArray[0].length).setValues(resultArray);
}
}
[1] Google Script: Append new values in column to another sheet
target_sheet.getRange(last_row,1,resultArray.length,resultArray[0].length)
.setValues(resultArray);
This line
Takes the sheet stored in the variable target_sheet
Accesses the defined range
Sets values
The syntax to access a range used in your code is getRange(row, column, numRows, numColumns)
whereby the start column is numerical - in your case it's 1 which corresponds to column A.
If you want to modify the start column from A to B:
Just change your range definition from
getRange(last_row,1,resultArray.length,resultArray[0].length)
to
getRange(last_row,2,resultArray.length,resultArray[0].length)

How do you use a cell value to find the matching sheet name?

I am trying to find a way to copy information from one sheet to another. I know how to find the information from the sheet to copy from, but I'm having trouble finding the sheet to copy to. I want to copy to a sheet based off of a value in my original sheet.
I have a list of names in column C, ex. John, Mark, and Will that starts in row 40. I would like to then copy John's row of information to the sheet titled "John", and Mark's information to a sheet titled "Mark", etc. so that each person's information is summarized on their own sheet. I am having trouble using the value found in column C (the person's name), and then using that value to find a sheet with the coordinating name.
function CopyInfo() {
var CopyFrom = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Summary");
var ColumntoSearch = 3;
var LastRow = CopyFrom.getLastRow();
//Gets column to search for names to compare
var Range = CopyFrom.getRange(40, ColumntoSearch, LastRow, 1);
var Values = Range.getValues();
//Sets the amount of data to copy over
var NumberofColumns = 11;
var NumberofRows = 1;
//Compares all the names in the Summary sheet
var d=0;
for(var i=0;i<Values.length;i++) {
var Name = CopyFrom.getRange(i-d+40, 3);
var CopyTo = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(Name);
if(Name == ????????){
var RangetoCopy=CopyFrom.getRange(i-d+40,1,NumberofRows,NumberofColumns);
var DestRange=CopyTo.getRange(CopyTo.getLastRow()+1,1,NumberofRows,NumberofColumns);
RangetoCopy.copyTo(DestRange);
d++;
}
}
}
You want to copy the values from Summary sheet to each sheet with the sheet name retrieved from the column "C" in Summary sheet.
There are values for copying in the column "A:K".
For example, when the row 45 of Summary sheet has the values of a45, b45, c45, d45, e45, f45, g45, h45, i45, j45, k45, the values are copied to just below the last row of the sheet name of c45.
If my understanding is correct, how about this modification? I think your script is almost correct. So I modified your script a little.
Modified script:
function CopyInfo() {
var ss = SpreadsheetApp.getActiveSpreadsheet(); // Added
var CopyFrom = ss.getSheetByName("Summary"); // Modified
var ColumntoSearch = 3;
var LastRow = CopyFrom.getLastRow();
//Gets column to search for names to compare
var Range = CopyFrom.getRange(40, ColumntoSearch, LastRow, 1);
var Values = Range.getValues();
//Sets the amount of data to copy over
var NumberofColumns = 11;
var NumberofRows = 1;
//Compares all the names in the Summary sheet
// var d=0; // Removed
for (var i = 0; i < Values.length; i++) {
var Name = Values[i][0]; // Modified
var CopyTo = ss.getSheetByName(Name); // Modified
if (Name) { // Modified
if (!CopyTo) CopyTo = ss.insertSheet(Name); // Added
var RangetoCopy = CopyFrom.getRange(i + 40, 1, NumberofRows, NumberofColumns); // Modified
var DestRange = CopyTo.getRange(CopyTo.getLastRow() + 1, 1, NumberofRows, NumberofColumns);
RangetoCopy.copyTo(DestRange);
// d++; // Removed
}
}
}
Note:
In this modified script, if the sheet name is not found, new sheet of the sheet name is inserted. If you don't want to insert new sheet, please replace if(!CopyTo) CopyTo = ss.insertSheet(Name) to the following script.
if (CopyTo) {
var RangetoCopy = CopyFrom.getRange(i + 40, 1, NumberofRows, NumberofColumns);
var DestRange = CopyTo.getRange(CopyTo.getLastRow() + 1, 1, NumberofRows, NumberofColumns);
RangetoCopy.copyTo(DestRange);
}
If I misunderstood your question and this was not the result you want, I apologize.

Merging google sheets: match with specific col values and merge data in 2 different sheets

I would like to ask if there is any possible way to set up a simple version of app script that is similar to the below add-ons.
https://chrome.google.com/webstore/detail/merge-sheets/gdmgbiccnalapanbededmeiadjfbfhkl?utm_source=ablebits
Basically, I've got sheet 1 and sheet 3, both sheets will have a common key column with specific values in each cell, and I would like to map both sheets with the data in that column then update the whole row data in the other sheets (For example, if i updated the sheet 3 then it map that col value in sheet 1, then paste the data in the corresponding row)
I have came up with a code that runs but no changes have been made, could anyone please advise how can I modify it to a simple version similar to the above add-ons? Thanks in advance.
I would like to populate the date from sheet 3 to sheet 1 after the code run, while the data from col C is matched in both sheets, please see example below, thanks!
For example, the data in sheet 1 highlighted row is having its key col with col C for common lookup value with sheet 3 while the row sequence is diff with sheet 3 (Please see the next photo, thanks!)
As you can see in sheet 3, the data of the whole row is inserted to the correct row according to the col C key col value which matched with sheet 1.
function myFunction2(){
// Get your spreadsheet and the sheets "TB" and "2"
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet1 = ss.getSheetByName("Sheet 1");
var sheet3 = ss.getSheetByName("Sheet 3");
// Get the range on sheet "TB"
var tabsheet1 = sheet1.getRange(2, 2, sheet1.getLastRow(),
sheet1.getLastColumn());
// Get the values to compare
var datasheet1 = tabsheet1.getValues();
var datasheet3 = sheet3.getRange(2, 2, sheet3.getLastRow(), sheet3.getLastColumn());
for(var i = 0; i < datasheet1.length; i++){
for(var j = 0; j < datasheet3.length; j++){
// Compare data: if they're the same, put the value
if(datasheet1[i][0]=(datasheet3[j][0]) == 0){
//if(datasheet1[i][0].localeCompare(datasheet3[j][0]) == 0){
datasheet1[i][1] = datasheet3[j][1];
}
}
}
// Take the modified tab and put it on the spreadsheet
tabsheet1.setValues(datasheet1);
}
You want to copy the data of sheet 3 to sheet 1. For this situation, when the values of column C of sheet 3 and sheet 1 is the same, you want to copy the row of sheet 3 to sheet 1, because the order of rows for sheet 3 and sheet 1 are different. If my understanding is correct, how about this modification?
Modification points :
In order to retrieve the values from column C to last column, it uses getRange(2, 3, sheet1.getLastRow(), sheet1.getLastColumn()).
In your script, datasheet3 is the range.
About if(datasheet1[i][0]=(datasheet3[j][0]) == 0){, in order to comare the date, I used getDisplayValues().
By this, the values retrieved as a string are compared.
Modified script :
function myFunction2(){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet1 = ss.getSheetByName("Sheet 1");
var sheet3 = ss.getSheetByName("Sheet 3");
var tabsheet1 = sheet1.getRange(2, 3, sheet1.getLastRow(), sheet1.getLastColumn());
var datasheet1 = tabsheet1.getValues();
var datasheet1d = tabsheet1.getDisplayValues(); // Added
var tabsheet3 = sheet3.getRange(2, 3, sheet3.getLastRow(), sheet3.getLastColumn()); // Added
var datasheet3 = tabsheet3.getValues(); // Modified
var datasheet3d = tabsheet3.getDisplayValues(); // Added
for (var i = 0; i < datasheet1.length; i++) {
for (var j = 0; j < datasheet3.length; j++) {
if (datasheet1d[i][0] == datasheet3d[j][0]) { // Modified
datasheet1[i] = datasheet3[j]; // Modified
}
}
}
tabsheet1.setValues(datasheet1);
}
Reference :
getDisplayValues()
If I misunderstand your question, please tell me. I would like to modify it. At that time, in order to modify, can you share the sample sheet? Of course, please remove your private information from it.
Edit 1 :
function myFunction2(){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet1 = ss.getSheetByName("Sheet 1");
var sheet3 = ss.getSheetByName("Sheet 3");
var tabsheet1 = sheet1.getRange(2, 3, sheet1.getLastRow() - 1, sheet1.getLastColumn() - 2); // Modified
var datasheet1 = tabsheet1.getValues();
var datasheet1d = tabsheet1.getDisplayValues(); // Added
var tabsheet3 = sheet3.getRange(2, 3, sheet3.getLastRow() - 1, sheet3.getLastColumn() - 2); // Added
var datasheet3 = tabsheet3.getValues(); // Modified
var datasheet3d = tabsheet3.getDisplayValues(); // Added
for (var i = 0; i < datasheet1.length; i++) {
for (var j = 0; j < datasheet3.length; j++) {
if (datasheet1d[i][0] == datasheet3d[j][0]) { // Modified
datasheet3[j].push(""); // Modified
datasheet1[i] = datasheet3[j]; // Modified
}
}
}
tabsheet1.setValues(datasheet1);
}
Edit 2 :
function myFunction2(){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet1 = ss.getSheetByName("comment_log");
var sheet3 = ss.getSheetByName("CM");
var tabsheet1 = sheet1.getRange(2, 3, sheet1.getLastRow() - 1, sheet1.getLastColumn() - 2); // Modified
var datasheet1 = tabsheet1.getValues();
var datasheet1d = tabsheet1.getDisplayValues(); // Added
var tabsheet3 = sheet3.getRange(2, 3, sheet3.getLastRow() - 1, sheet3.getLastColumn() - 2); // Added
var datasheet3 = tabsheet3.getValues(); // Modified
var datasheet3d = tabsheet3.getDisplayValues(); // Added
for (var i = 0; i < datasheet1.length; i++) {
for (var j = 0; j < datasheet3.length; j++) {
if (datasheet1d[i][0] == datasheet3d[j][0]) { // Modified
if (datasheet1[i].length != datasheet3[j].length) {
for (var k = 0; k < datasheet1[i].length - datasheet3[j].length; k++) {
datasheet3[j].push(datasheet1[i][datasheet1[i].length - 1]);
}
}
datasheet1[i] = datasheet3[j]
}
}
}
tabsheet1.setValues(datasheet1);
}

Deleting rows in google sheets using Google Apps Script

I encountered the weirdest error while trying to delete rows that match a specific value using Google Apps Script.
Here is my Code:
function myFunction() {
var doc = SpreadsheetApp.getActiveSpreadsheet();
var sheet = doc.getSheetByName("file.csv");
var values = sheet.getRange("N2:N").getValues();
var row_del = new Array();
for(var i=0;i<values.length;i++)
{
if(values[i] == 'del'){
row_del.push(i+2); // This line was added for debugging purposes.
// sheet.deleteRow(i+2) was the line that was in this condition
// (i+2) is used because row 1 has headers and the range starts from 0.
}
}
// Logger.log(row_del);
// GmailApp.sendEmail("my_email_address", "subject", row_del)
for (var i = 0; i < row_del.length; i++)
{
sheet.deleteRow(row_del[i]);
}
}
The code that I have written picks up the row numbers that should be deleted but not all these rows are deleted in my first try. I should execute my script a number of times for these rows to be deleted.
If my code has an error, it should show up and if the logic is wrong, incorrect rows must be deleted. I encounter neither of these scenarios and I should just execute this function multiple times.
Is there something that I'm missing here?
When a row is deleted from a sheet, the rows below it get renumbered even as the script continues to run. If the script subsequently tries to also delete those rows, the result is unpredictable. For this reason, when deleting rows one should proceed from bottom to top. In your case, like so:
for (var i = row_del.length - 1; i>=0; i--) {
sheet.deleteRow(row_del[i]);
}
refer to lock function suggested by google team:
var lock = LockService.getScriptLock();
lock.waitLock(30000); // lock 30 seconds
//do whatever you want here
lock.releaseLock();
That way, you got your deleting job work once at a time! The system thread won't go on to other jobs until 30 seconds is up or releasing the lock.
google dev-document: https://developers.google.com/apps-script/reference/lock/lock
To delete blank rows from a single named sheet, assuming column 1 has data in valid rows.
Search and delete from highest row number to lowest row number.
// Deletes any row whose first column is blank
// WARNING: Assumes any valid row has data in column 1
function deleteBlankRows() {
var doc = SpreadsheetApp.getActiveSpreadsheet();
var sheet = doc.getSheetByName("Sheet3");
var lastRow = SpreadsheetApp.getActiveSheet().getLastRow();
for (var i = lastRow; i > 0; i--) {
var range = sheet.getRange(i,1);
var data = range.getValue();
if (data == '') {
sheet.deleteRow(i);
}
}
}
increment i only if you didn't delete a row
function del_F_rows(){
var i=1;
while(!sht_balanceHistory.getRange(i,1).isBlank()){
if(sht_balanceHistory.getRange(i,3).getValue()=="F")
sht_balanceHistory.deleteRow(i);
else
i=i+1;
}
}
You can just delete the rows alter the last row using the deleteRows function like this:
var maxRows = newsheet.getMaxRows();
var lastRow = newsheet.getLastRow();
if (maxRows-lastRow != 0)
{
newsheet.deleteRows(lastRow+1, maxRows-lastRow);
}
Update 2020
A faster and a more modern JavaScript approach would be to use forEach and reverse() to iterate backwards.
It makes more sense to flatten the values array since it concerns data of a single column.
Solution:
function myFunction() {
const doc = SpreadsheetApp.getActiveSpreadsheet();
const sheet = doc.getSheetByName("file.csv");
const values = sheet.getRange("N2:N").getValues().flat();
values.reverse().forEach((r,i)=>{
if (r=='del'){
sheet.deleteRow(values.length-i+1);
}
});
}
So, is this what it should look like?
function myFunction() {
var doc = SpreadsheetApp.getActiveSpreadsheet();
var sheet = doc.getSheetByName("Sheet1");
var values = sheet.getRange("A:A").getValues();
var row_del = new Array();
for(var i=0;i<values.length;i++)
{
if(values[i] == 'N'){
row_del.push(i+2); // This line was added for debugging purposes.
// sheet.deleteRow(i+2) was the line that was in this condition
// (i+2) is used because row 1 has headers and the range starts from 0.
}
}
// Logger.log(row_del);
// GmailApp.sendEmail("my_email_address", "subject", row_del)
for (var i = row_del.length - 1; i>=0; i--) { sheet.deleteRow(row_del[i]); }
}
Copy pasting from: https://gist.github.com/dDondero/285f8fd557c07e07af0e
Instead of looping through the rows twice, you can count how mwny rows have been deleted, to calculate the correct index for the row that you will delete next.
function deleteRows() {
var sheet = SpreadsheetApp.getActiveSheet();
var rows = sheet.getDataRange();
var numRows = rows.getNumRows();
var values = rows.getValues();
var rowsDeleted = 0;
for (var i = 0; i <= numRows - 1; i++) {
var row = values[i];
if (row[0] == 'delete' || row[0] == '') { // This searches all cells in columns A (change to row[1] for columns B and so on) and deletes row if cell is empty or has value 'delete'.
sheet.deleteRow((parseInt(i)+1) - rowsDeleted);
rowsDeleted++;
}
}
};
This is a script I used to delete everything but certain values, but can be easiliy modified to keep certain strings and runs a lot faster than looping through all the data filters out and deletes the values you want to remove and bulk deletes. my data had about 10000 rows so loop would have taken forever.
function DeleteCertainRows(){
columnCopy("D","Z","YourSheetName");//Copy from your target column in this case D to another column in this case Z
replaceInSheet("Z","YourSheetName","keep String 1","keep");//find and replace the value with what you want to keep
replaceInSheet("Z","YourSheetName","keep String 2","keep");//Can repeat for additional values
DeleteValueInColumn("Z","YourSheet","keep");//filters and deletes all other values Column is case sensitive and cant go past Z
};
function replaceInSheet(repColumn,sheetname, to_replace, replace_with) {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetname);
var values = sheet.getRange(repColumn+'1:'+repColumn+sheet.getMaxRows());
var textFinder = values.createTextFinder(to_replace);
var replaceall = textFinder.replaceAllWith(replace_with);
};
function columnCopy(copyfrm,copyto,sheetname){
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetname);
var copyrange = sheet.getRange(copyfrm+'1:'+copyfrm+sheet.getMaxRows());
var pasterange = sheet.getRange(copyto+'1:'+copyto+sheet.getMaxRows());
copyrange.copyTo(pasterange);
};
function DeleteValueInColumn(colStr, sheetname, deleteval){
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetname);
var filterRange=sheet.getDataRange();
var myFilter = filterRange.createFilter();
var CriteriaBuild = SpreadsheetApp.newFilterCriteria();
var Criteria = CriteriaBuild.whenTextDoesNotContain(deleteval);//change to whenTextContains to delete your string value instead of everything else
var myCriteria = Criteria.build();
var str = colStr;
var myCol = parseInt(str.charCodeAt(0) - 64);
Logger.log(myCol);
myFilter.setColumnFilterCriteria(myCol, myCriteria);
var deleterange=sheet.getRange('2:'+sheet.getLastRow());
sheet.deleteRows(deleterange.getRow(), deleterange.getNumRows());
myFilter.remove();
};
Another method is to sort records and delete all at once.
My following code will sort the data with N/A value and will delete all rows at once.
function DeleteErrorLine2() {
var SS = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("ORDER SHEET"); //Get Open Lines Sheet
var lastRow = SS.getLastRow();
var range = SS.getRange(2, 1, lastRow-1, 33); //get range
range.sort({column: 2, ascending: false}) // filter data descending
var cell = SS.getRange('B1'); // values stored in cell B
var ct = 1; // starting row
while(cell.offset(ct, 0).getValue() == "#N/A" ) {
ct++;
}
if(ct!=1){
ct = ct - 1 // minus 1 to get the last row
SS.deleteRows(2, ct)
}
range.sort({column: 2, ascending: true}) // filter data again ascending
}