How to merge two scripts to have them run simultaneously

265 views
Skip to first unread message

KB

unread,
Dec 5, 2022, 4:10:11 PM12/5/22
to Google Apps Script Community
Hi there, newbie over here. 

I am trying to organize a sheet for my client list and am having difficulties merging these two codes together so they run simultaneously. I am aware I cannot have two onEdit functions, I am just really stuck and not sure how to have them both run together. Both codes run smoothly separately, however just not together no matter how many ways I try to merge them. Code 1 moves the entire row to the second sheet when the status in column "I" when the option is selected as "Closed." The second script is to sort column J on sheet 1 (labelled open) ascending: false(Z-A) which is to make all dates in that column in order from the more recent date.

Code 1
function onEdit(e) { 

const src = e.source.getActiveSheet(); 
const r = e.range; if (r.columnStart != 9 || r.rowStart == 1 || e.value == src.getName()) return; 
const dest = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(e.value); src.getRange(r.rowStart,1,1,9).moveTo(dest.getRange(dest.getLastRow()+1,1,1,9)); src.deleteRow(r.rowStart); 

 } 

Code 2

var ss = SpreadsheetApp.getActiveSpreadsheet(); var sheet = ss.getSheetByName("Opened"); var range = sheet.getRange("J2:J100"); 

function onEdit(e) { 
range.sort([{column: 10, ascending: false}]); 
}

Here's the link to an example of the sheet I'm working on:

Any help would be greatly appreciated, you would make my job so much easier!

Jon Couch

unread,
Dec 5, 2022, 6:49:33 PM12/5/22
to google-apps-sc...@googlegroups.com
KB, I'm still learning myself, but rather than trying to merge the two scripts, can you create a script that reads the onEdit and calls the first script and then the second?

function onEdit(){
 code1();
 code2();
}

Someone else will have a more elegant solution, but that's my down and dirty idea.

Good luck,
Jon


--
You received this message because you are subscribed to the Google Groups "Google Apps Script Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-script-c...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-script-community/aebec6b7-9543-4f5e-8c83-1eb5fdf1ec01n%40googlegroups.com.

cbmserv...@gmail.com

unread,
Dec 6, 2022, 12:01:49 AM12/6/22
to google-apps-sc...@googlegroups.com

Jon’s suggestion is bang on. That is what you need to do assuming both work on the same spreadsheet.

cwl...@gmail.com

unread,
Dec 6, 2022, 7:38:38 AM12/6/22
to Google Apps Script Community
As long as they are doing different things to different areas of the spreadsheet, you should be fine. If they both touch the same data, structure, layout, formatting, etc., then they would obviously interfere with each other so you would want to wait until the first finishes before running the second.

CBMServices Web

unread,
Dec 6, 2022, 1:39:01 PM12/6/22
to google-apps-sc...@googlegroups.com
Not really. No need to worry about that as calling them one at a time guarantees one will finish before next starts.



KB

unread,
Dec 6, 2022, 3:47:10 PM12/6/22
to Google Apps Script Community
Thank you all for your help, 

This is what I came up with however the first function (moveRow) is still the only one that is working and not the Sort function. Am I missing something?

function onEdit(e){
  moveRow(e);
  sort(e);
}

function moveRow(e) {
  const src = e.source.getActiveSheet();
  const r = e.range;
  if (r.columnStart != 9 || r.rowStart == 1 || e.value == src.getName()) return;
  const dest = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(e.value);
  src.getRange(r.rowStart,1,1,10).moveTo(dest.getRange(dest.getLastRow()+1,1,1,10));
  src.deleteRow(r.rowStart);
}

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Opened");            
var range = sheet.getRange("J2:J100");                  
 
function sort(e)  {
  range.sort([{column: 10, ascending: False}]); 

}

KB

unread,
Dec 6, 2022, 3:56:26 PM12/6/22
to Google Apps Script Community
I FIXED IT!!. Thank you so much everyone for your help. I am so happy!

Sharath Gowda

unread,
Mar 15, 2023, 10:23:34 AM3/15/23
to Google Apps Script Community
Can you share the script as to how we can run 2 scripts simultaneously.

Thanks



Forex Trader

unread,
Mar 17, 2023, 2:59:54 AM3/17/23
to google-apps-sc...@googlegroups.com
You can combine the two scripts by merging the code from Code 2 into Code 1, like this:


function onEdit(e) {

const src = e.source.getActiveSheet();

const r = e.range;

if (r.columnStart != 9 || r.rowStart == 1 || e.value == src.getName()) return;

const dest = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(e.value);

src.getRange(r.rowStart,1,1,9).moveTo(dest.getRange(dest.getLastRow()+1,1,1,9));

src.deleteRow(r.rowStart);

// Sort column J on sheet "Opened"


var ss = SpreadsheetApp.getActiveSpreadsheet();

var sheet = ss.getSheetByName("Opened");

var range = sheet.getRange("J2:J100");

range.sort([{column: 10, ascending: false}]);

}

This combined script will move the entire row to the second sheet when the status in column "I" is set to "Closed," and then sort column J on the "Opened" sheet in descending order.

Note that you may need to adjust the range in Code 2 depending on the size of your spreadsheet.

Reply all
Reply to author
Forward
0 new messages