Google Sheets script conflict - two onEdits [duplicate] - google-apps-script

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

Related

Create a manual trigger to Import data from same cell in google sheet 1 to new row and cell in google sheet 2 [duplicate]

I'm new to the world of javascript & google apps script and I'm struggling to get a row of data to move from one spreadsheet to another. I have it moving a row from sheet to sheet within the same spreadsheet but not across spreadsheets.
I know I need to use SpreadsheetApp.openById("insertID") rather than SpreadsheetApp.getActiveSpreadsheet() but I can't seem to make it work.
Here is the code I have that's working based on moving sheet to sheet, how do I modify it to move to a new spreadsheet?
function onEdit(event) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = event.source.getActiveSheet();
var r = event.source.getActiveRange();
if(s.getName() == "Timeline Submit" && r.getColumn() == 117 && r.getValue() == true) {
var row = r.getRow();
var numColumns = s.getLastColumn();
var targetSheet = ss.getSheetByName("WIP");
var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
s.getRange(row, 1, 1, numColumns).copyTo(target, {contentsOnly:true});
} else if(s.getName() == "WIP" && r.getColumn() == 117 && r.getValue() == false) {
var row = r.getRow();
var numColumns = s.getColumn();
var targetSheet = ss.getSheetByName("Timeline Submit");
var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
s.getRange(row, 1, 1, numColumns).copyTo(target, {contentsOnly:true});
}
}
I couldn't figure out everything in the second section of your code but it had too many errors so I just guessed and some of it.
Don't forget to go create a trigger for this in the Script Editor Edits/Current Project Triggers.
function installableOnEdit(e) {
var sh=e.range.getSheet();
if(sh.getName()=="Timeline Submit" && e.range.columnStart==117 && e.value==true) {
var ss=SpreadsheetApp.openById('id')
var tsh= ss.getSheetByName("WIP");
var trg=tsh.getRange(tsh.getLastRow()+1,1);
sh.getRange(e.range.rowStart,1,1,sh.getLastColumn()).copyTo(trg,{contentsOnly:true})
}else if(sh.getName()=="WIP" && e.range.columnStart==17 && e.value==false) {
var ss=SpreadsheetApp.openById('id')
var tsh=ss.getSheetByName('Timeline Submit');
var trg=sh.getRange(tsh.getLastRow()+1,1);
sh.getRange(e.range.rowStart,1,1,sh.getLastColumn()).copyTo(trg,{contentsOnly:true});
}
}

Combining two onEdit() in single for Google Sheets App Script [duplicate]

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

Transferring range of rows to another tab, then clearing tab without removing formulas

In Google Sheets, I'm trying to transfer range A3:G13(if there is text; sometimes the entire range will not be used) from the Packing List tab to the Completed Transactions tab when G16 is set to "Complete" and then clear A3:G13 without removing the formulas. I have seen many scripts that transfer rows individually but I need them all to transfer onEdit of G15.
Thanks for your help!
https://docs.google.com/spreadsheets/d/1raVbOHsjwK1EHDnIO7jKo1oH4GQXsbfjBD2ormcO87I/edit?ts=5fd250e3#gid=0
Code:
function onEdit(event) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = event.source.getActiveSheet();
var r = event.source.getActiveRange();
if(s.getName() == "Packing List" && r.getColumn() == 7 && r.getValue() == "Complete") {
var range = r.getRange(A3:G13);
var numColumns = s.getLastColumn();
var targetSheet = ss.getSheetByName("Completed Transactions");
var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
s.getRange(row, 1, 1, numColumns).moveTo(target);
s.deleteRow(row);
You could do the following:
Retrieve the values from the source range with getValues, filter out the rows where column A is empty with filter, and set the filtered values with setValues.
Retrieve the formulas of the source range using getFormulas, and set them again with setFormulas after the content has been cleared.
Clear the content of the source range with clearContent.
Code snippet:
function onEdit(e) {
const rangeNotation = e.range.getA1Notation();
const editedSheet = e.range.getSheet();
if (editedSheet.getName() === "Packing List" && rangeNotation === "G15" && e.value === "Complete") {
const sourceRange = editedSheet.getRange("A3:G13");
const sourceFormulas = sourceRange.getFormulas();
const sourceData = sourceRange.getValues().filter(row => row[0] != "");
const targetSheet = e.source.getSheetByName("Completed Transactions");
const targetRange = targetSheet.getRange(targetSheet.getLastRow() + 1, 1, sourceData.length, sourceData[0].length);
targetRange.setValues(sourceData);
sourceRange.clearContent();
sourceRange.setFormulas(sourceFormulas);
}
}

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