Combining two onEdit() in single for Google Sheets App Script [duplicate] - google-apps-script

This question already has an answer here:
Merging or Combining two onEdit trigger functions
(1 answer)
Closed 1 year ago.
Can someone point me in the right direction please?
I'm attempting to run two onEdit() which both send a range of cells to an archived sheet depending on whether a checkbox is ticked.
My code results in the entire row being moved and deleted rather than the specified cell range irregardless of which of the two checkboxes is selected as TRUE.
function onEdit(event) {
onEdit1();
onEdit2();
function onEdit1() {
// assumes source data in sheet named Active
// target sheet of move to named Found
// test column with yes/no is col 31
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = event.source.getActiveSheet();
var r = event.source.getActiveRange();
if(s.getName() == "WORKSHOP" && r.getColumn() == 31 && r.getValue() == true) {
var row = r.getRow();
var numColumns = 11
var targetSheet = ss.getSheetByName("ARCHIVE-DEFECTS");
var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
s.getRange(row, 23, 1, numColumns).moveTo(target);
s.deleteRow(row);
}
}
function onEdit2() {
// assumes source data in sheet named Active
// target sheet of move to named Found
// test column with yes/no is col 10
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = event.source.getActiveSheet();
var r = event.source.getActiveRange();
if(s.getName() == "WORKSHOP" && r.getColumn() == 10 && r.getValue() == true) {
var row = r.getRow();
var numColumns = 12
var targetSheet = ss.getSheetByName("ARCHIVE-INSP");
var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
s.getRange(row, 1, 1, numColumns).moveTo(target);
s.deleteRow(row);
}
}
}
Apologies I'm not very knowledgeable and am kind of learning by doing by readin threads in the forum. Cheers.

Try this
function onEdit(event) {
onEdit1(event);
onEdit2(event);
}
function onEdit1(event) {
_________
}
function onEdit2(event) {
_________
}
it seems that there is a lack og event parameter

Related

Google Sheets script conflict - two onEdits [duplicate]

This question already has an answer here:
Merging or Combining two onEdit trigger functions
(1 answer)
Closed last year.
I have two scripts that I need to work with on Google Sheets. The first one auto sorts all rows if I put a date on column 23. The other script moves the entire row to another sheet called completed when I check a box on row 28. Separate they work perfectly but when I add them to the same script one of them stops working. How can I tweak this?
function autoSort(){
const ss = SpreadsheetApp.getActiveSpreadsheet()
const ws = ss.getSheetByName("Installation")
const range = ws.getRange(2,1,ws.getLastRow()-1,28)
range.sort({column:23, descending:false})
}
function onEdit(e){
const row = e.range.getRow()
const column = e.range.getColumn()
if(!(column === 23 && row >= 2)) return
autoSort()
}
function onEdit(event) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = event.source.getActiveSheet();
var r = event.source.getActiveRange();
if(s.getName() == "Installation" && r.getColumn() == 28 && r.getValue() == "Completed") {
var row = r.getRow();
var numColumns = s.getLastColumn();
var targetSheet = ss.getSheetByName("Complete");
var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
s.getRange(row, 1, 1, numColumns).moveTo(target);
s.deleteRow(row);
}
}
I believe your goal is as follows.
Your 2 functions of onEdit work fine for each run. You want to merge these functions.
In this case, how about the following modification?
Modified script:
function onEdit(e) {
const r = e.range;
if (r.columnStart === 23 && r.rowStart >= 2) {
autoSort()
return;
}
const s = r.getSheet();
if (s.getSheetName() == "Installation" && r.columnStart == 28 && r.getValue() == "Completed") {
var row = r.rowStart;
var numColumns = s.getLastColumn();
var targetSheet = e.source.getSheetByName("Complete");
var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
s.getRange(row, 1, 1, numColumns).moveTo(target);
s.deleteRow(row);
}
}
Reference:
Event Objects

If cell contains X in the value move row

I'm having issues figuring out how to move rows if a cell contains X word. I'm trying to manage SKUs and I want all of the ones that start with "lep-" to move sheets.
They should all start with "lep" but have different numbers after ex. lep-4-35-2-24x30 , lep-3-28-3-16x20 , etc.
function onEdit(event) {
// assumes source data in sheet named SHOPIFY
// target sheet of move to named ORG
// test column with yes is col 17 or Q
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = event.source.getActiveSheet();
var r = event.source.getActiveRange();
if(s.getName() == "SHOPIFY" && r.getColumn() == 17 && r.getValue() == "lep") {
var row = r.getRow();
var numColumns = s.getLastColumn();
var targetSheet = ss.getSheetByName("ORG");
var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
s.getRange(row, 1, 1, numColumns).moveTo(target);
s.deleteRow(row);
}
}
Solution:
You can use includes() method as this method determines whether a string contains the characters of a specified string:
Instead of using r.getValue() == "lep", use r.getValue().includes("lep") in your if statement. I've tested your code with includes() method and it worked on my end.

How to stop code from moving two rows at a time when checkbox is true?

I'm working on a spreadsheet for my boss and I've been trying to add a feature where the entire row moves from Sheet1 to Sheet2 when the checkbox in column1 is checked. The code I'm using works, except sometimes it moves two rows at once even though only one box is checked. Here's my code:
function onEdit(event) {
// assumes source data in sheet named Sheet1
// target sheet of move to named Sheet2
// test column with yes/no is col 1 or A
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = event.source.getActiveSheet();
var r = event.source.getActiveRange();
if(s.getName() == "Sheet1" && r.getColumn() == 1 && r.getValue() == true) {
var row = r.getRow();
var numColumns = s.getLastColumn();
var targetSheet = ss.getSheetByName("Sheet2");
var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
s.getRange(row, 1, 1, numColumns).moveTo(target);
s.deleteRow(row);
}
}
I am NOT experienced with code at all. I got this from Joshua Doan's reply to someone else's question: Google script for move rows depending on checkbox - Google sheet
So my question is, how do I get it to stop moving two rows at once??
Thanks much!
function onEdit(e) {
var sh=e.range.getSheet();
if(sh.getName()!='Sheet1')return;
if(e.range.columnStart==1 && e.value=="TRUE") {
var row1=sh.getRange(e.range.rowStart,1,1,sh.getLastColumn());
var tsh=e.source.getSheetByName('Sheet2');
row1.moveTo(tsh.getRange(tsh.getLastRow()+1,1));
sh.deleteRow(e.range.rowStart);
}
}

Automatically Copy Rows to Another Sheet Based on Cell Value

I'm pretty new to scripts so I apologize for my potential errors in explaining my issue. I'm working on a spreadsheet that will copy a row to another sheet when it meets a certain criteria (or a certain value in a column) and a second function which will move the row to another sheet upon meeting a certain criteria.
Using related questions/answers, I was able to write a script that deletes a row upon meeting criteria "Outbound" in column 4, see myFunction1 below. For myFunction2, I'm trying to instead just copy the row to another sheet when it meets criteria "M" in column 15, but when I enter that value in column 15 it copies the wrong row information.
Hopefully that makes sense! Code below:
function onEdit(event) {
myFunction1(event);
myFunction2(event);
}
function myFunction1(event){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = event.source.getActiveSheet();
var r = event.source.getActiveRange();
if(s.getName() == "Trailers in Yard" && r.getColumn() == 4 && r.getValue() == "Outbound") {
var row = r.getRow();
var numColumns = s.getLastColumn();
var targetSheet = ss.getSheetByName("Outbound");
var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
s.getRange(row, 1, 1, numColumns).moveTo(target);
s.deleteRow(row);
}
}
function myFunction2(event){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = event.source.getActiveSheet();
var r = event.source.getActiveRange();
if(s.getName() == "Trailers in Yard" && r.getColumn() == 15 && r.getValue() == "M") {
var row = r.getRow();
var numColumns = s.getLastColumn();
var targetSheet = ss.getSheetByName("Moves");
var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
s.getRange(row, 1, 1, numColumns).copyTo(target);
}
}
Appreciate the assistance!
Try it this way:
function onEdit(e) {
//myFunction1(e);
myFunction2(e);
}
function myFunction2(e){
//e.source.toast('Flag1');
var sh=e.range.getSheet();
if(sh.getName()=="Sheet2" && e.range.columnStart==15 && e.range.getValue()=="M") {
//e.source.toast('Flag2');
var targetSheet=e.source.getSheetByName("Sheet1");
var target=targetSheet.getRange(targetSheet.getLastRow()+1,1);
sh.getRange(e.range.rowStart,1,1,sh.getLastColumn()).copyTo(target);
}
}
You'll have to change the sheet names.

My google scripts no longer runs

I previously used this google script below to move information from one sheet to another sheet based on conditional values in one column (column#12). However, it just stopped responding/running recently and I cannot figure out why. I played with the triggers, I tried to use it in a new script and changing up some of the names and column#, but nothing seems to work. Any suggestions on where I can look to find out what's going on?
function onEdit(e) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = ss.getActiveSheet();
var r = ss.getActiveRange();
if(s.getName() == "Phx-DFF" && r.getColumn() == 12 && r.getValue() == "no")
{
var row = r.getRow();
var numColumns = s.getLastColumn();
var targetSheet = ss.getSheetByName("NY-DFF");
if(targetSheet.getLastRow() == targetSheet.getMaxRows())
{
targetSheet.insertRowsAfter(targetSheet.getLastRow(), 5); //inserts 20 rows after last used row
}
var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
s.getRange(row, 1, 14, numColumns).moveTo(target);
s.deleteRow(row);
}
}
Have you tried to use logging or running the function in debug mode to see what exacly is happening when you run this function.
I want to suggest the follwing changes to the function as a lot of information is provided by the eventinfo JSON:
function onEdit(e) {
var ss = e.source;
var r = e.Range;
if(ss.getSheetName() == "Phx-DFF" && r.getColumn() == 12 && e.value == "no")
{
var s = ss.getActiveSheet();
var row = r.getRow();
var numColumns = s.getLastColumn();
var targetSheet = ss.getSheetByName("NY-DFF");
if(targetSheet.getLastRow() == targetSheet.getMaxRows())
{
targetSheet.insertRowsAfter(targetSheet.getLastRow(), 5); //inserts 20 rows after last used row
}//I dont think this is necessary, GAS should takes care of this auto. Remove this part
var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
s.getRange(row, 1, 14, numColumns).moveTo(target);
s.deleteRow(row);
}
}