r/GoogleAppsScript • • Sep 14 '22

Question Help With script please!

Hello, I would like to have this spread sheet preform two on edit commands. I want entry's to auto sort based on the date in column 6 and also give a time stamp in column 7 when column 6 is edited or new line is created. the two scripts I found online work fine by themselves but not together. I want them to both worth as the same time and also i want to get rid on the sheet specific criteria for the autosort function and for it to affect the whole document not just individual sheets. can anyone help me?

function onEdit(e) { var row = e.range.getRow(); var col = e.range.getColumn(); if(col == 1){ e.source.getActiveSheet().getRange(row,2).setValue(new Date()); } }

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("YM");             // SHEET NAME
var range = sheet.getRange("A2:O");                  // RANGE TO SORT

function onEdit(e)  {
range.sort([{column: 6, ascending: true}]);        // COLUMN NUMBER TO SORT
}

6 Upvotes

6 comments sorted by

View all comments

2

u/DistributionNo997 Sep 15 '22

For both scripts to work at the same time, combine all code in you're two onedit functions into one, seperate function (or ideally, two seperate functions). Then call this function within the OnEdit function.

For the sorting to work across all sheets within the spreadsheet, create something like the following:

var ss = SpreadSheet.getActive(); var sheets = ss.getsheets(); for (var s in sheets) {

var rangeToSort = sheets[i].getRange(A2:O);

// rest of function }

2

u/DistributionNo997 Sep 15 '22

Wrote it will taking a shit on my phone, just copy your range.sort in the last line

1

u/OkDevelopment6063 Sep 15 '22

var ss = SpreadSheet.getActive(); var sheets = ss.getsheets(); for (var s in sheets) {

var rangeToSort = sheets[i].getRange(A2:O);

// rest of function }

ok so I have never taken any courses in java script so I'm doing a lot of trial and error with proper formatting. If its not too much trouble for you could you show me what it looks like to combine both the time stamp function and the auto sort function? and I can copy paste? lol I'm sorry I have just been struggling with this problem for a couple days. column 6 is the sort by and column 7 is where the time stamp goes.

1

u/DistributionNo997 Sep 15 '22

I might have some time tomorrow, will send it to ya if succesfull.

1

u/OkDevelopment6063 Sep 15 '22

wow awesome you rock!!!! thank you so much internet hero!

1

u/OkDevelopment6063 Sep 20 '22

any chance you had a chance to try it out?