0
votes

I partially know the name of 2 sheets I need to rename when the cell B2 change in each sheet:

  • One in called csv_nat and it should be renamed to csv_nat_30_11_2019 when cell B2 in sheet changes (it is a date format like 30/11/2019)

  • The other one has the exact same structure and is called csv_alt

My main issue is the following. When the tab name has already changed, I need to locate the sheet from its root name (csv_nat) and I have not figured out how.

My limited and starting code for now is:

function onEdit(e){
  if(e.range.getA1Notation() != "B2") return;
  e.source.getActiveSheet().setName("csv_nat_"+e.value);
}

Besides, does the onEdit function work for any change or only when user edits the cell?

1
Looking at your existing code, it seems you only want to edit the sheet that was edited. You're kind of already doing that (e.range.getSheet() would be better), so why exactly do you need to locate the sheet by tab name?Diego

1 Answers

0
votes

If you want to locate your csv_nat or csv_alt sheets, you can use regex in this case.

Try this example

if sheet contains csv_nat or csv_alt it will alert you:

var ss = SpreadsheetApp.getActiveSheet();
if(ss.getSheetName().match(/csv_nat|csv_alt/)){
  SpreadsheetApp.getUi().alert("Working Sheet is found")
}

Reference