简体   繁体   中英

Rename a Google sheet tab based on cell

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM