How to simplify and add sort in google spreadsheets? - google-apps-script

I'm using this code to move completed to one sheet but everything else from completed back to current. Is there a way to make it quicker like everything that isn't completed move back? Also is there a way to add sort by column 4 in the same code for the current sheet?
function onEdit() {
// moves a row from a sheet to another when a magic value is entered in a column
// adjust the following variables to fit your needs
// see https://productforums.google.com/d/topic/docs/ehoCZjFPBao/discussion
var sheetNameToWatch = "Current";
var columnNumberToWatch = 4;
var valueToWatch = "Completed";
var sheetNameToMoveTheRowTo = "Completed";
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = SpreadsheetApp.getActiveSheet();
var range = sheet.getActiveCell();
if (sheet.getName() == sheetNameToWatch && range.getColumn() == columnNumberToWatch && range.getValue() == valueToWatch) {
var targetSheet = ss.getSheetByName(sheetNameToMoveTheRowTo);
var targetRange = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
sheet.getRange(range.getRow(), 1, 1, sheet.getLastColumn()).moveTo(targetRange);
sheet.deleteRow(range.getRow());
}
var sheetNameToWatch = "Completed";
var columnNumberToWatch = 4;
var valueToWatch = "At Risk";
var sheetNameToMoveTheRowTo = "Current";
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = SpreadsheetApp.getActiveSheet();
var range = sheet.getActiveCell();
if (sheet.getName() == sheetNameToWatch && range.getColumn() == columnNumberToWatch && range.getValue() == valueToWatch) {
var targetSheet = ss.getSheetByName(sheetNameToMoveTheRowTo);
var targetRange = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
sheet.getRange(range.getRow(), 1, 1, sheet.getLastColumn()).moveTo(targetRange);
sheet.deleteRow(range.getRow());
}
var sheetNameToWatch = "Completed";
var columnNumberToWatch = 4;
var valueToWatch = "Hold";
var sheetNameToMoveTheRowTo = "Current";
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = SpreadsheetApp.getActiveSheet();
var range = sheet.getActiveCell();
if (sheet.getName() == sheetNameToWatch && range.getColumn() == columnNumberToWatch && range.getValue() == valueToWatch) {
var targetSheet = ss.getSheetByName(sheetNameToMoveTheRowTo);
var targetRange = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
sheet.getRange(range.getRow(), 1, 1, sheet.getLastColumn()).moveTo(targetRange);
sheet.deleteRow(range.getRow());
}
var sheetNameToWatch = "Completed";
var columnNumberToWatch = 4;
var valueToWatch = "Placed";
var sheetNameToMoveTheRowTo = "Current";
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = SpreadsheetApp.getActiveSheet();
var range = sheet.getActiveCell();
if (sheet.getName() == sheetNameToWatch && range.getColumn() == columnNumberToWatch && range.getValue() == valueToWatch) {
var targetSheet = ss.getSheetByName(sheetNameToMoveTheRowTo);
var targetRange = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
sheet.getRange(range.getRow(), 1, 1, sheet.getLastColumn()).moveTo(targetRange);
sheet.deleteRow(range.getRow());
}
var sheetNameToWatch = "Completed";
var columnNumberToWatch = 4;
var valueToWatch = "Safe";
var sheetNameToMoveTheRowTo = "Current";
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = SpreadsheetApp.getActiveSheet();
var range = sheet.getActiveCell();
if (sheet.getName() == sheetNameToWatch && range.getColumn() == columnNumberToWatch && range.getValue() == valueToWatch) {
var targetSheet = ss.getSheetByName(sheetNameToMoveTheRowTo);
var targetRange = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
sheet.getRange(range.getRow(), 1, 1, sheet.getLastColumn()).moveTo(targetRange);
sheet.deleteRow(range.getRow());
}
var sheetNameToWatch = "Completed";
var columnNumberToWatch = 4;
var valueToWatch = "Other";
var sheetNameToMoveTheRowTo = "Current";
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = SpreadsheetApp.getActiveSheet();
var range = sheet.getActiveCell();
if (sheet.getName() == sheetNameToWatch && range.getColumn() == columnNumberToWatch && range.getValue() == valueToWatch) {
var targetSheet = ss.getSheetByName(sheetNameToMoveTheRowTo);
var targetRange = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
sheet.getRange(range.getRow(), 1, 1, sheet.getLastColumn()).moveTo(targetRange);
sheet.deleteRow(range.getRow());
}
}

Related

I used google script editor to set up a structure where a row was deleted & transferred to diff sheet based on checkboxes. Some rows are disappearing

Hi I'm a complete newbie when it comes to google scripts so any help is appreciated. The script is working fine, however some rows are disappearing (getting deleted) and not being moved to the second sheet. Can anyone help out.
Script used:
function onEdit(event) {// assumes source data in sheet named main
// target sheet of move to named Completed
// getColumn with check-boxes is currently set to column 4 or D
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = event.source.getActiveSheet();
var r = event.source.getActiveRange();
if(s.getName() == "Acquisition" && r.getColumn() == 1 && r.getValue() == true) {
var row = r.getRow();
var numColumns = s.getLastColumn();
var targetSheet = ss.getSheetByName("FirstTraining");
var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
s.getRange(row, 1, 1, numColumns).moveTo(target);
s.deleteRow(row);
} else if(s.getName() == "FirstTraining" && r.getColumn() == 1 && r.getValue() == false) {
var row = r.getRow();
var numColumns = s.getLastColumn();
var targetSheet = ss.getSheetByName("Acquisition");
var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
s.getRange(row, 1, 1, numColumns).moveTo(target);
s.deleteRow(row);
}
if(s.getName() == "FirstTraining" && r.getColumn() == 2 && r.getValue() == true) {
var row = r.getRow();
var numColumns = s.getLastColumn();
var targetSheet = ss.getSheetByName("Incubation");
var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
s.getRange(row, 1, 1, numColumns).moveTo(target);
s.deleteRow(row);
} else if(s.getName() == "Incubation" && r.getColumn() == 2 && r.getValue() == false) {
var row = r.getRow();
var numColumns = s.getLastColumn();
var targetSheet = ss.getSheetByName("FirstTraining");
var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
s.getRange(row, 1, 1, numColumns).moveTo(target);
s.deleteRow(row);
}
if(s.getName() == "Incubation" && r.getColumn() == 3 && r.getValue() == true) {
var row = r.getRow();
var numColumns = s.getLastColumn();
var targetSheet = ss.getSheetByName("Graduated");
var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
s.getRange(row, 1, 1, numColumns).moveTo(target);
s.deleteRow(row);
} else if(s.getName() == "Graduated" && r.getColumn() == 3 && r.getValue() == false) {
var row = r.getRow();
var numColumns = s.getLastColumn();
var targetSheet = ss.getSheetByName("Incubation");
var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
s.getRange(row, 1, 1, numColumns).moveTo(target);
s.deleteRow(row);
}
}
enter image description here
It works fine when clicked one by one but bulk clicks dont get moved and rows are disappearing randomly

How can I fix require Value In Range

I have this script that works perfectly in some of my spreadsheets but fails on others.
I have code that is too long, or crashes. Please help me shorten the code
Can someone help and explain me this?
Here is the code:
function setDataValid_(range, sourceRange) {
var rule = SpreadsheetApp.newDataValidation().requireValueInRange(sourceRange, true).build();
range.setDataValidation(rule);
}
function onEdit() {
var aSheet = SpreadsheetApp.getActiveSheet();
var aCell = aSheet.getActiveCell();
var aColumn = aCell.getColumn();
if (aColumn == 4 && aSheet.getName() == 'Tháng 6') {
var range = aSheet.getRange(aCell.getRow(), aColumn + 1);
var sourceRange = SpreadsheetApp.getActiveSpreadsheet().getRangeByName(aCell.getValue());
setDataValid_(range, sourceRange)
}
else if (aColumn == 5 && aSheet.getName() == 'Tháng 6') {
var range = aSheet.getRange(aCell.getRow(), aColumn + 1);
var sourceRange = SpreadsheetApp.getActiveSpreadsheet().getRangeByName(aCell.getValue());
setDataValid_(range, sourceRange)
}
if (aColumn == 4 && aSheet.getName() == 'Tháng 7') {
var range = aSheet.getRange(aCell.getRow(), aColumn + 1);
var sourceRange = SpreadsheetApp.getActiveSpreadsheet().getRangeByName(aCell.getValue());
setDataValid_(range, sourceRange)
}
else if (aColumn == 5 && aSheet.getName() == 'Tháng 7') {
var range = aSheet.getRange(aCell.getRow(), aColumn + 1);
var sourceRange = SpreadsheetApp.getActiveSpreadsheet().getRangeByName(aCell.getValue());
setDataValid_(range, sourceRange)
}
if (aColumn == 4 && aSheet.getName() == 'Tháng 8') {
var range = aSheet.getRange(aCell.getRow(), aColumn + 1);
var sourceRange = SpreadsheetApp.getActiveSpreadsheet().getRangeByName(aCell.getValue());
setDataValid_(range, sourceRange)
}
else if (aColumn == 5 && aSheet.getName() == 'Tháng 8') {
var range = aSheet.getRange(aCell.getRow(), aColumn + 1);
var sourceRange = SpreadsheetApp.getActiveSpreadsheet().getRangeByName(aCell.getValue());
setDataValid_(range, sourceRange)
}
if (aColumn == 4 && aSheet.getName() == 'Tháng 9') {
var range = aSheet.getRange(aCell.getRow(), aColumn + 1);
var sourceRange = SpreadsheetApp.getActiveSpreadsheet().getRangeByName(aCell.getValue());
setDataValid_(range, sourceRange)
}
else if (aColumn == 5 && aSheet.getName() == 'Tháng 9') {
var range = aSheet.getRange(aCell.getRow(), aColumn + 1);
var sourceRange = SpreadsheetApp.getActiveSpreadsheet().getRangeByName(aCell.getValue());
setDataValid_(range, sourceRange)
}
}
Here is an prototype of how I would do it. I can't guarantee it because your date set if pretty complex.
I use the event object e to identify the active sheet, range and value.
Notice I use Range.offset() because you want to set the value in the column next to the one edited.
function onEdit(e) {
let sheets = ['Tháng 6','Tháng 7','Tháng 8','Tháng 9'];
let sheet = e.range.getSheet();
if( sheets.indexOf(sheet.getName()) < 0 ) return;
let column = e.range.getColumn();
if( ( column === 4 ) || ( column === 5 ) ) {
let range = e.range.offset(0,1);
let sourceRange = e.source.getRangeByName(e.value);
setDataValid_(range, sourceRange);
}
}

select multiple sheets with the same range

I have the code below that works perfectly, but I need to alter it to get data from the same range (D5) but from multiple sheets. At the moment, it gets the data from 'Cancellations' only. How would I get it to select D5 from say 'Sheet2', or 'Sheet3' as well.
I have included half of the code from the function I currently have. As you can see it's very repatative.
function transferList() {
///////////////////////////////////////////Export value 1/////////////////////////////////////////////////////
var columnIndex = 1; // Column
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sourceSheet = ss.getSheetByName("Cancellations");
var sourceData = sourceSheet.getRange("D5").getValue();
var targetSS = ss.getSheetByName("Deduction Breakdown");
var a1Range = targetSS.getRange(2,columnIndex);
var targetRange;
if (a1Range.offset(1,0).getValue() !== "") targetRange = a1Range.getNextDataCell(SpreadsheetApp.Direction.DOWN).offset(1,0);
else targetRange = targetSS.getRange(3,columnIndex);
targetRange.setValue(sourceData);
var columnIndex = 1; // Column
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sourceSheet = ss.getSheetByName("Unanswered");
var sourceData = sourceSheet.getRange("D5").getValue();
var targetSS = ss.getSheetByName("Deduction Breakdown");
var a1Range = targetSS.getRange(2,columnIndex);
var targetRange;
if (a1Range.offset(1,0).getValue() !== "") targetRange = a1Range.getNextDataCell(SpreadsheetApp.Direction.DOWN).offset(1,0);
else targetRange = targetSS.getRange(3,columnIndex);
targetRange.setValue(sourceData);
var columnIndex = 1; // Column
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sourceSheet = ss.getSheetByName("Queries");
var sourceData = sourceSheet.getRange("D5").getValue();
var targetSS = ss.getSheetByName("Deduction Breakdown");
var a1Range = targetSS.getRange(2,columnIndex);
var targetRange;
if (a1Range.offset(1,0).getValue() !== "") targetRange = a1Range.getNextDataCell(SpreadsheetApp.Direction.DOWN).offset(1,0);
else targetRange = targetSS.getRange(3,columnIndex);
targetRange.setValue(sourceData);
///////////////////////////////////////////Export value 2/////////////////////////////////////////////////////
var columnIndex = 4; // Column
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sourceSheet = ss.getSheetByName("Cancellations");
var sourceData = sourceSheet.getRange("D6").getValue();
var targetSS = ss.getSheetByName("Deduction Breakdown");
var a1Range = targetSS.getRange(5,columnIndex);
var targetRange;
if (a1Range.offset(1,0).getValue() !== "") targetRange = a1Range.getNextDataCell(SpreadsheetApp.Direction.DOWN).offset(1,0);
else targetRange = targetSS.getRange(6,columnIndex);
targetRange.setValue(sourceData);
var columnIndex = 4; // Column
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sourceSheet = ss.getSheetByName("Unanswered");
var sourceData = sourceSheet.getRange("D6").getValue();
var targetSS = ss.getSheetByName("Deduction Breakdown");
var a1Range = targetSS.getRange(5,columnIndex);
var targetRange;
if (a1Range.offset(1,0).getValue() !== "") targetRange = a1Range.getNextDataCell(SpreadsheetApp.Direction.DOWN).offset(1,0);
else targetRange = targetSS.getRange(6,columnIndex);
targetRange.setValue(sourceData);
var columnIndex = 4; // Column
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sourceSheet = ss.getSheetByName("Queries");
var sourceData = sourceSheet.getRange("D6").getValue();
var targetSS = ss.getSheetByName("Deduction Breakdown");
var a1Range = targetSS.getRange(5,columnIndex);
var targetRange;
if (a1Range.offset(1,0).getValue() !== "") targetRange = a1Range.getNextDataCell(SpreadsheetApp.Direction.DOWN).offset(1,0);
else targetRange = targetSS.getRange(6,columnIndex);
targetRange.setValue(sourceData);
///////////////////////////////////////////Export value 3////////////////////////////////////////////////////////
var columnIndex = 7; // Column
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sourceSheet = ss.getSheetByName("Cancellations");
var sourceData = sourceSheet.getRange("D7").getValue();
var targetSS = ss.getSheetByName("Deduction Breakdown");
var a1Range = targetSS.getRange(5,columnIndex);
var targetRange;
if (a1Range.offset(1,0).getValue() !== "") targetRange = a1Range.getNextDataCell(SpreadsheetApp.Direction.DOWN).offset(1,0);
else targetRange = targetSS.getRange(6,columnIndex);
targetRange.setValue(sourceData);
var columnIndex = 7; // Column
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sourceSheet = ss.getSheetByName("Unanswered");
var sourceData = sourceSheet.getRange("D7").getValue();
var targetSS = ss.getSheetByName("Deduction Breakdown");
var a1Range = targetSS.getRange(5,columnIndex);
var targetRange;
if (a1Range.offset(1,0).getValue() !== "") targetRange = a1Range.getNextDataCell(SpreadsheetApp.Direction.DOWN).offset(1,0);
else targetRange = targetSS.getRange(6,columnIndex);
targetRange.setValue(sourceData);
var columnIndex = 7; // Column
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sourceSheet = ss.getSheetByName("Queries");
var sourceData = sourceSheet.getRange("D7").getValue();
var targetSS = ss.getSheetByName("Deduction Breakdown");
var a1Range = targetSS.getRange(5,columnIndex);
var targetRange;
if (a1Range.offset(1,0).getValue() !== "") targetRange = a1Range.getNextDataCell(SpreadsheetApp.Direction.DOWN).offset(1,0);
else targetRange = targetSS.getRange(6,columnIndex);
targetRange.setValue(sourceData);
///////////////////////////////////////////Export value 4///////////////////////////////////////////////////////////
var columnIndex = 10; // Column
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sourceSheet = ss.getSheetByName("Cancellations");
var sourceData = sourceSheet.getRange("D9").getValue();
var targetSS = ss.getSheetByName("Deduction Breakdown");
var a1Range = targetSS.getRange(5,columnIndex);
var targetRange;
if (a1Range.offset(1,0).getValue() !== "") targetRange = a1Range.getNextDataCell(SpreadsheetApp.Direction.DOWN).offset(1,0);
else targetRange = targetSS.getRange(6,columnIndex);
targetRange.setValue(sourceData);
var columnIndex = 10; // Column
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sourceSheet = ss.getSheetByName("Unanswered");
var sourceData = sourceSheet.getRange("D9").getValue();
var targetSS = ss.getSheetByName("Deduction Breakdown");
var a1Range = targetSS.getRange(5,columnIndex);
var targetRange;
if (a1Range.offset(1,0).getValue() !== "") targetRange = a1Range.getNextDataCell(SpreadsheetApp.Direction.DOWN).offset(1,0);
else targetRange = targetSS.getRange(6,columnIndex);
targetRange.setValue(sourceData);
var columnIndex = 10; // Column
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sourceSheet = ss.getSheetByName("Queries");
var sourceData = sourceSheet.getRange("D9").getValue();
var targetSS = ss.getSheetByName("Deduction Breakdown");
var a1Range = targetSS.getRange(5,columnIndex);
var targetRange;
if (a1Range.offset(1,0).getValue() !== "") targetRange = a1Range.getNextDataCell(SpreadsheetApp.Direction.DOWN).offset(1,0);
else targetRange = targetSS.getRange(6,columnIndex);
targetRange.setValue(sourceData);
Here is a full script that should do what you are asking:
/** #OnlyCurrentDoc */
function transferList() {
var columnIndex = 1; // Column
var columnIndexIncr = 3; //columnIndex increment per each target range
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sourceSheets = ["Cancellations", "Unanswered", "Queries"]; //Sheet names
var targetSS = ss.getSheetByName("Deduction Breakdown"); //Target sheet
var targetRanges = ["D5", "D6", "D7", "D9"]; //Target ranges
var sourceData;
var a1Range;
var a1Row;
var targetRange;
for(var range of targetRanges) {
for(var i in sourceSheets) {
if(range == "D5") {a1Row = 2} else {a1Row = 5};
sourceData = ss.getSheetByName(sourceSheets[i]).getRange(range).getValue();
if(sourceData == "N") {
a1Range = targetSS.getRange(a1Row, columnIndex);
if (a1Range.offset(1,0).getValue() !== "") targetRange = a1Range.getNextDataCell(SpreadsheetApp.Direction.DOWN).offset(1,0);
else targetRange = targetSS.getRange((a1Row+1),columnIndex);
targetRange.setValue(sourceData);
}
}
columnIndex += columnIndexIncr;
};
};
Please let me know if you have any issues with this. I tested this script in a test sheet and it should work. Please let me know if this was not the intended result
multiple sheets same range
function transferList() {
const ss = SpreadsheetApp.getActive();
const dl = [{srcname:'Cancellations',srcrng:'D5',dstname:'Deduction Breakdown',dstrng:'A2'}];//fill in the remaining options
dl.forEach(o => {
let ssh = ss.getSheetByName(o.srcname);
let srg = ssh.getRange(o.srcrng);
let sda = srg.getValue();
let dsh = ss.getSheetByName(o.dstname);
let drg = dsh.getRange(o.dstrng);
if(drg.offset(1,0).getValue() !== "") {
drg = drg,getNextDataCell(SpreadsheetApp.Direction.DOWN).offset(1,0);
} else {
drg = drg.offset(1,0);
}
drg.setValue(sda);
})
}

Google Apps Script - getLastRow where certain columns are empty

I have the following Google Apps Script which copies data from the source sheet and pastes it into another sheet. It currently looks for the last empty row and last empty column of the target sheet. I now have certain columns in the target sheet populated with formula to deal with the pasted data. The target sheet column range is A:K and has formula in columns I:K. in Can someone help me with new code to look for the last row where columns A:H are empty?
function onEdit(e) {
var spreadsheet = e.source;
var sheet = spreadsheet.getActiveSheet();
var sourcesheetname = "SOP Register"
var range = e.range;
var sheet = range.getSheet();
var row = range.getRow();
var column = range.getColumn();
var editedColumn = range.getColumn();
var editedRow = range.getRow();
var column = 7;
var date = range.getValue();
if(Object.prototype.toString.call(date) === '[object Date]' && editedColumn == column && editedRow > 2 && sheet.getName() == sourcesheetname) {
var targetsheetname = "Internal Audit Register";
var target = e.source.getSheetByName(targetsheetname);
var numCols = sheet.getLastColumn();
var values = sheet.getRange(row, 1, 1, numCols).getValues()[0];
values.splice(9) //Up to and including column I
values.splice(7, 1) //Remove column H
values.splice(2, 3); //Keep columns all columns and leave out columns C, D & E
var lastRow = target.getLastRow();
var lastCol = target.getLastColumn();
values.unshift("SOP"); //Append "SOP" to column A
target.appendRow(values); // Append new row
sheet.hideColumns(6,2);}}//End of onEdit Functions
After Tanaike's post I have added his function getLastRow(sheet) and all of my onEdit(e) script below:
function getLastRow(sheet) {
const values = sheet.getRange("A1:H" + sheet.getLastRow()).getDisplayValues();
let lastRow = 0;
for (let r = values.length - 1; r >= 0 ; r--) {
if (!lastRow && !values[r].every(e => e == "")) {
lastRow = r + 1;
break
}
}
return lastRow;
}
// Cut Employees Left from Unit Standards sheet and paste in Unit Standards - Employees Left sheet
function onEdit(e) {
var ss = e.source;
var sheet = ss.getActiveSheet();
var sheetName = "Unit Standards"
var range = e.range;
var editedColumn = range.getColumn();
var editedRow = range.getRow();
var column = 4;
var date = range.getValue();
// Object.prototype.toString.call(date) === '[object Date]' --> checks if value is date
// editedColumn == column && editedRow > 4 --> checks if edited cell is from 'Date Left'
// sheet.getName() == sheetName --> checks if edited sheet is 'Unit Standards'
if(Object.prototype.toString.call(date) === '[object Date]' && editedColumn == column && editedRow > 4 && sheet.getName() == sheetName) {
var numCols = sheet.getLastColumn();
var row = sheet.getRange(editedRow, 1, 1, numCols).getValues();
var destinationSheet = ss.getSheetByName("Unit Standards - Employees Left");
// Get first empty row:
var emptyRow = destinationSheet.getLastRow() + 1;
// Copy values from 'Unit Standards'
destinationSheet.getRange(emptyRow, 1, 1, numCols).setValues(row);
sheet.deleteRow(editedRow);
sheet.hideColumns(column); }
// Copy and paste from Events/Incidents sheet to Vehicle Damage sheet
{var range = e.range;
var sheet = range.getSheet();
var row = range.getRow();
var column = range.getColumn();
var sourcesheetname = "Events/Incidents";
var checkbox = range.getValue();
if (sheet.getName() == sourcesheetname && column == 25 && row > 2 && checkbox == true) {
var targetsheetname = "Vehicle Damage";
var target = e.source.getSheetByName(targetsheetname);
var numCols = sheet.getLastColumn();
var values = sheet.getRange(row, 1, 1, numCols).getValues()[0];
values.splice(17)
values.splice(8, 8)
values.splice(5, 1)
values.splice(3, 1); // Removing undesired values
var lastRow = target.getLastRow();
var lastCol = target.getLastColumn();
target.appendRow(values); }// Append new row
//SOP Internal Audit Required CheckBox if True
{var range = e.range
var sheet = range.getSheet();
var row = range.getRow();
var column = range.getColumn();
var sourcesheetname = "SOP Register";
var checkbox = range.getValue();
if (sheet.getName() == sourcesheetname && column == 5 && row > 2 && checkbox == true) {
sheet.showColumns(6,2);
sheet.getRange("F3").activate();}
// Copy and paste from SOP Register sheet to Internal Audit sheet
{var spreadsheet = e.source;
var sheet = spreadsheet.getActiveSheet();
var sourcesheetname = "SOP Register"
var range = e.range;
var sheet = range.getSheet();
var row = range.getRow();
var column = range.getColumn();
var editedColumn = range.getColumn();
var editedRow = range.getRow();
var column = 7;
var date = range.getValue();
if(Object.prototype.toString.call(date) === '[object Date]' && editedColumn == column && editedRow > 2 && sheet.getName() == sourcesheetname) {
var targetsheetname = "Internal Audit Register";
var target = e.source.getSheetByName(targetsheetname);
var numCols = sheet.getLastColumn();
var values = sheet.getRange(row, 1, 1, numCols).getValues()[0];
values.splice(9) //Up to and including column I
values.splice(7, 1) //Remove column H
values.splice(2, 3); //Keep columns all columns and leave out columns C, D & E
var lastRow = getLastRow(target);
var lastCol = target.getLastColumn();
values.unshift("SOP"); //Append "SOP" to column A
target.appendRow(values); // Append new row
sheet.hideColumns(6,2);}}}}}//End of onEdit Functions
This is my desired outcome and to paste the new data in cells A5:F5 but the current script is pasting it into cells A6:F6 and cells I6:K6 are empty:
Unwanted Outcome
A Sample of the source sheet and the target sheet are below:
Sample Spreadsheet
I believe your goal as follows.
You have a sheet which has the values in the columns "A" to "K". And the columns "I" to "K" have the formulas.
You want to retrieve the numbers of last row in the range of "A:H".
For this, how about this answer? In this answer, I prepare a function for retrieving the last row you want.
Modified script:
Please add the following function. This function returns the values of lastRow by inputting a sheet object.
function getLastRow(sheet) {
const values = sheet.getRange("A1:H" + sheet.getLastRow()).getDisplayValues();
let lastRow = 0;
for (let r = values.length - 1; r >= 0 ; r--) {
if (!lastRow && !values[r].every(e => e == "")) {
lastRow = r + 1;
break
}
}
return lastRow;
}
And, in order to use above function in your script, please modify as follows.
From:
var lastRow = target.getLastRow();
To:
var lastRow = getLastRow(target);
By this, lastRow can be retrieved in the range of "A:H".
Reference:
every()
Added 1:
About one more modification, please test the following modification. In your current script, lastRow retrieved by getLastRow(target) is not used and values is appended to the last row of the sheet by appendRow. So in order to use lastRow, please modify as follows. In this case, from your updated question, please modify getRange("A1:H" + sheet.getLastRow()) to getRange("A1:F" + sheet.getLastRow()) for the function of getLastRow.
From:
var lastRow = getLastRow(target);
var lastCol = target.getLastColumn();
values.unshift("SOP"); //Append "SOP" to column A
target.appendRow(values); // Append new row
To:
var lastRow = getLastRow(target);
var lastCol = target.getLastColumn();
values.unshift("SOP");
target.getRange(lastRow + 1, 1, 1, values.length).setValues([values]); // Modified
Added 2:
You have a sheet which has the values in the columns "A" to "K". And the columns "I" to "K" have the formulas.
You want to retrieve the numbers of last row in the range of "A:H".
And also, you want to copy the formulas of the columns "I" to "K" to the same row which puts the values.
For this, please modify your script as follows. In this case, please use above getLastRow().
From:
var lastRow = getLastRow(target);
var lastCol = target.getLastColumn();
values.unshift("SOP"); //Append "SOP" to column A
target.appendRow(values); // Append new row
To:
var lastRow = getLastRow(target);
var lastCol = target.getLastColumn();
values.unshift("SOP");
target.getRange(lastRow + 1, 1, 1, values.length).setValues([values]); // Modified
if (lastRow >= 3) target.getRange(lastRow, 9, 1, 3).copyTo(target.getRange(lastRow + 1, 9, 1, 3), SpreadsheetApp.CopyPasteType.PASTE_FORMULA); // Added
In this case, at least, it is required to have the row 3 has the formulas at the columns "I" to "K".
If the row 3 has surely the formulas at the columns "I" to "K", I think that you can also use if (lastRow >= 3) target.getRange(3, 9, 1, 3).copyTo(target.getRange(lastRow + 1, 9, 1, 3), SpreadsheetApp.CopyPasteType.PASTE_FORMULA);.

How to move rows from one spreadsheet tab to another tab based on MULTIPLE CONDITIONS using Google Apps Script?

I want to move rows from one spreadsheet tab to another tabs (within same spreadsheet) based on MULTIPLE conditions using Google Apps Script.
I have multiple tabs for different team members, in which their corresponding lead details are logged. After calling the leads, I move that particular rows to different tabs (Bad leads, Closed leads and Sold leads). I have been able to move rows from each member tabs to different lead statuses back and forth with the help of script.
from Kushal tab to Closed leads tab <==> from Closed leads tab to Kushal tab
from XYZ tab to Bad leads tab <==> from Bad leads tab to XYZ tab
and so on.
Basically, I know how to move rows based on only one condition; whether the lead status is either Bad or Closed or Sold or Processing.
But my problem is: I have multiple member tabs from where multiple lead rows can be transferred to any one of the tabs (Bad, Closed or Sold) and I want to revert back the rows to the member tabs where it originally came from, based on Team Member column value (Kushal, XYZ, ABC, etc.).
if lead status = processing and team member = Kushal,
then, move row from Bad leads tab to Kushal tab.
else if lead status = processing and team member = XYZ,
then, move that row from Closed leads tab to XYZ tab.
else if lead status = processing and team member = ABC,
then, move that row from Sold leads tab to ABC tab.
Can anyone please help me with this?
Please let me know if my question is not understandable.
Here is the script I have that transfers one row to multiple tabs back and forth, based on only one condition.
function onEdit(event) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = event.source.getActiveSheet();
var r = event.source.getActiveRange();
if(s.getName() == "lead list" && r.getColumn() == 4 && r.getValue() == "Closed Leads")
{
var row = r. getRow();
var numColumns = s.getLastColumn();
var targetSheet = ss.getSheetByName("closed leads");
var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
s.getRange(row, 1, 1, numColumns).moveTo(target);
s.deleteRow(row);
}
else if(s.getName() == "lead list" && r.getColumn() == 4 && r.getValue() == "Bad Leads")
{
var row = r. getRow();
var numColumns = s.getLastColumn();
var targetSheet = ss.getSheetByName("bad leads");
var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
s.getRange(row, 1, 1, numColumns).moveTo(target);
s.deleteRow(row);
}
else if(s.getName() == "lead list" && r.getColumn() == 4 && r.getValue() == "Sold")
{
var row = r. getRow();
var numColumns = s.getLastColumn();
var targetSheet = ss.getSheetByName("sold");
var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
s.getRange(row, 1, 1, numColumns).moveTo(target);
s.deleteRow(row);
}
else if(s.getName() == "closed leads" && r.getColumn() == 4 && r.getValue() == "Pending")
{
var row = r. getRow();
var numColumns = s.getLastColumn();
var targetSheet = ss.getSheetByName("lead list");
var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
s.getRange(row, 1, 1, numColumns).moveTo(target);
}
else if(s.getName() == "closed leads" && r.getColumn() == 4 && r.getValue() == "Bad Leads")
{
var row = r. getRow();
var numColumns = s.getLastColumn();
var targetSheet = ss.getSheetByName("bad leads");
var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
s.getRange(row, 1, 1, numColumns).moveTo(target);
s.deleteRow(row);
}
else if(s.getName() == "closed leads" && r.getColumn() == 4 && r.getValue() == "Sold")
{
var row = r. getRow();
var numColumns = s.getLastColumn();
var targetSheet = ss.getSheetByName("sold");
var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
s.getRange(row, 1, 1, numColumns).moveTo(target);
s.deleteRow(row);
}
else if(s.getName() == "bad leads" && r.getColumn() == 4 && r.getValue() == "Pending")
{
var row = r. getRow();
var numColumns = s.getLastColumn();
var targetSheet = ss.getSheetByName("lead list");
var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
s.getRange(row, 1, 1, numColumns).moveTo(target);
s.deleteRow(row);
}
else if(s.getName() == "bad leads" && r.getColumn() == 4 && r.getValue() == "Closed Leads")
{
var row = r. getRow();
var numColumns = s.getLastColumn();
var targetSheet = ss.getSheetByName("closed leads");
var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
s.getRange(row, 1, 1, numColumns).moveTo(target);
s.deleteRow(row);
}
else if(s.getName() == "bad leads" && r.getColumn() == 4 && r.getValue() == "Sold")
{
var row = r. getRow();
var numColumns = s.getLastColumn();
var targetSheet = ss.getSheetByName("sold");
var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
s.getRange(row, 1, 1, numColumns).moveTo(target);
s.deleteRow(row);
}
else if(s.getName() == "sold" && r.getColumn() == 4 && r.getValue() == "Pending")
{
var row = r. getRow();
var numColumns = s.getLastColumn();
var targetSheet = ss.getSheetByName("lead list");
var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
s.getRange(row, 1, 1, numColumns).moveTo(target);
s.deleteRow(row);
}
else if(s.getName() == "sold" && r.getColumn() == 4 && r.getValue() == "Bad Leads")
{
var row = r. getRow();
var numColumns = s.getLastColumn();
var targetSheet = ss.getSheetByName("bad leads");
var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
s.getRange(row, 1, 1, numColumns).moveTo(target);
s.deleteRow(row);
}
else if(s.getName() == "sold" && r.getColumn() == 4 && r.getValue() == "Closed Leads")
{
var row = r. getRow();
var numColumns = s.getLastColumn();
var targetSheet = ss.getSheetByName("closed leads");
var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
s.getRange(row, 1, 1, numColumns).moveTo(target);
s.deleteRow(row);
}
}