0

I have 2 functions that I can get to work individually, but I have know idea how to combine them and have them work concurrently.

The first function pulls the value from A1 (which references a cell in another sheet) and returns it to B1. The second takes the value form B1 and renames the tab.

 function onEdit(e){
 var app = SpreadsheetApp;
 var ss = app.getActiveSpreadsheet();
 var sh = activeSheet = ss.getActiveSheet();

 var newName = activeSheet.getRange(1,1).getValue();
 activeSheet.getRange(1,2).setValue(newName);

}
function onEdit(e) {
  if(e.range.rowStart === 1 && e.range.columnStart === 2) {
  e.source.getActiveSheet().setName(e.value);
 }

}

From what I've found online, I'm guessing an if statement would help but I'm not sure how to implement it.

Rubén
  • 34,714
  • 9
  • 70
  • 166

1 Answers1

1
function onEdit(e) {
  var sh = e.range.getSheet();
  sh.getRange(1, 2).setValue(sh.getRange(1, 1).getValue());
  if (e.range.rowStart == 1 && e.range.columnStart == 2) {
    sh.setName(e.value);
  }
}
Cooper
  • 59,616
  • 6
  • 23
  • 54
  • Thank you for responding. I tried that and got the following error: Execution log 5:33:03 PM Notice Execution started 5:33:03 PM Error TypeError: Cannot read properties of undefined (reading 'range') onEdit @ Code.gs:2 – Mike Pannell Jan 05 '23 at 01:34
  • You cannot run functions like this without supplying the onEdit trigger event object. Just go to the appropriate sheet and make you edits to debug the code. – Cooper Jan 05 '23 at 02:31
  • I'm sorry, I'm very new to app script, when I debug it says "e" and "sh" are undefined. Can you point me in the right direction? – Mike Pannell Jan 05 '23 at 18:41
  • Yes because these types of script run on triggers that are generated by user edits and those triggers provide an object which is placed in the first parameter of the function declaration. So if you try to run the function from the script editor or a menu item it won't work because you must also supply the event object. If you don't understand what I'm talking about then I'd recommend going too a javascript reference an starting learning about how functions work. – Cooper Jan 05 '23 at 20:01