0
  1. Question I have 3 scripts running on my spreadsheet. Only one is working now. I will post the code below. Only one of the functions are working. 2 of the codes are timestamps and one is an edit Auto Archive code.

I have attached a photo of the 3 scripts I have. One is named Code.gs, archive.gs, Date for Date Removed.gs

Function below not working

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 1 or A                     
var ss = SpreadsheetApp.getActiveSpreadsheet();                     
var s = event.source.getActiveSheet();                      
var r = event.source.getActiveRange();                      
                        
if(s.getName() == "ACTIVITY REPORT" && r.getColumn() == 1 && r.getValue() == true) {                        
var row = r.getRow();                       
var numColumns = s.getLastColumn();                     
var targetSheet = ss.getSheetByName("Archive");                     
var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);                     
s.getRange(row, 1, 1, numColumns).moveTo(target);                       
s.deleteRow(row);                       
  } 
}   
    

2nd function not working

function onEdit(e){
  var ss=SpreadsheetApp.getActiveSpreadsheet();
  var sh=ss.getActiveSheet();
  
  if(sh.getName()!='Archive')
    return;
  
  var rg=sh.getActiveCell();
  
  if(rg.getRow()<=1)
    return;
  
  if(rg.getColumn()!=11)
    return;
  
  SpreadsheetApp.flush();
  
  if(rg.getValue()=='NP CND A ACCEPTED' || rg.getValue()== 'NP BACKGROUND')
    rg.offset(0, 16).setValue(new Date());
}       
    

3rd Function that is working

function onEdit(){
  var ss=SpreadsheetApp.getActiveSpreadsheet();
  var sh=ss.getActiveSheet();
  
  if(sh.getName()!='Archive')
    return;
  
  var rg=sh.getActiveCell();
  
  if(rg.getRow()<=1)
    return;
  
  if(rg.getColumn()!=11)
    return;
  
  SpreadsheetApp.flush();
  
  if(rg.getValue()=='NP CND A ACCEPTED' || rg.getValue()== 'NP BACKGROUND'|| rg.getValue()== 'HIRED INTERN-SP' || rg.getValue()== 'HIRED SDR-SP' || rg.getValue()== 'HIRED' || rg.getValue()== 'HIRED INTERN' || rg.getValue()== 'HIRED SDR'  || rg.getValue()== 'HIRED SDR' || rg.getValue()== 'NP AS'  || rg.getValue()== 'NP LDR'  || rg.getValue()== 'NP CND A AR' || rg.getValue()== 'NP CND A OFFER'  || rg.getValue()== 'NO SHOW'  || rg.getValue()== 'NP ORL'  || rg.getValue()== 'NP CND A Zoom Int'  || rg.getValue()== 'NP CND A 1 FL')
    rg.offset(0, 14).setValue(new Date());
}

Here is the screenshot of the error I am getting when implementing the code a different way. It says unexpected token in line 9.

Rubén
  • 34,714
  • 9
  • 70
  • 166
Laura W.
  • 1
  • 3
  • I have projects that have hundreds of scripts. The only requirement is that they all need to have unique names even if they are in different files. – Cooper Apr 08 '22 at 17:40
  • You have two onEdit() functions. That is not allowed. The one that loads first will be the only one called – Cooper Apr 08 '22 at 17:41
  • @Cooper So the screenshot shows the different scripts right? Within each of those scripts is an onEdit function. Where are you saying two isnt allowed? Example: Script/Project 1 is called code.gs, The second one is called archive.gs. Each of those has an onEdit function in it. Are you saying I cant do that? My script called Date for Date Removed.gs is working. I'm sorry. I am still very new to this and learning. Thank you for your patience. – Laura W. Apr 08 '22 at 17:48

1 Answers1

0

The screenshot red square shows the files and they are there just to make managing scripts a little easier. But in all of those files there can be only one onEdit() function and in fact all of the functions in all of the files within a project must have unique names.

All of the files are in the same project and all of the functions in a project must have a unique name.

You can have more the an one project in a given container. The each must have functions with unique names but they could have the same function names from one project to the next

You will probably have to tweak these a lot to get them to work:

function onEdit(e) {
  const sh = e.range.getSheet();
  if (sh.getName() == "ACTIVITY REPORT" && e.range.columnStart == 1 && e.values == "TRUE") {
    var tsh = e.source.getSheetByName("Archive");
    var target = tsh.getRange(tsh.getLastRow() + 1, 1);
    sh.getRange(e.range.rowStart, 1, 1, sh.getLastColumn()).moveTo(target);
    sh.deleteRow(e.range.rowStart);
  }
  if (sh.getName() == 'Archive' && e.range.rowStart > 1 && e.range.columnStart == 11 && (e.value == 'NP CND A ACCEPTED' || e.value == 'NP BACKGROUND') {
    SpreadsheetApp.flush();
    e.range.offset(0, 16).setValue(new Date());
  }
  if (sh.getName() == 'Archive' && e.range.rowStart > 1 && e.range.columnStart == 11) {
    SpreadsheetApp.flush();
    if (e.range.getValue() == 'NP CND A ACCEPTED' || e.range.getValue() == 'NP BACKGROUND' || e.range.getValue() == 'HIRED INTERN-SP' || e.range.getValue() == 'HIRED SDR-SP' || e.range.getValue() == 'HIRED' || e.range.getValue() == 'HIRED INTERN' || e.range.getValue() == 'HIRED SDR' || e.range.getValue() == 'HIRED SDR' || e.range.getValue() == 'NP AS' || e.range.getValue() == 'NP LDR' || e.range.getValue() == 'NP CND A AR' || e.range.getValue() == 'NP CND A OFFER' || e.range.getValue() == 'NO SHOW' || e.range.getValue() == 'NP ORL' || e.range.getValue() == 'NP CND A Zoom Int' || e.range.getValue() == 'NP CND A 1 FL')
      e.range.offset(0, 14).setValue(new Date());
  }
}
Cooper
  • 59,616
  • 6
  • 23
  • 54
  • So, what should my next step be? Create separate projects? If so, how do I connect those to this one spreadsheet? – Laura W. Apr 08 '22 at 18:27
  • 1
    No. Combine onedits into one function or change the name of some of them and make them installable – Cooper Apr 08 '22 at 18:28