Lonestar Dads
Lonestar Dads

Reputation: 15

Google Sheets change tab name based on cell value of a DIFFERENT tab

Ok so I currently am running this script which works perfect if you make an edit on the current tab you're wanting to change the sheet name for...

    function onEdit(e) {
if(e.range.getA1Notation() !== 'A1') return;
    var s = e.source.getActiveSheet()
            s.setName(s.getRange('A1')
                .getValue())
}

What I tried to do is use a query formula that will change the cell value based on the cell of a different tab. That didn't work, because it's not them making an edit. It's happening automatically when the cell in a different tab changes. Is there any script that would allow me to change the tab name based on the value of a cell from a different tab?

Maybe I need to be using OnChange instead? Does anyone have an example to make OnChange do the same thing?

Upvotes: 0

Views: 1335

Answers (1)

Muhammet Yunus
Muhammet Yunus

Reputation: 589

You should assign the page you'd like to rename as a variable as follows

const sheet2 = ss.getSheets()[1]

and call it.

Here is a simple example code:

const ss = SpreadsheetApp.getActiveSpreadsheet()
const sheet1 = ss.getSheetByName('Sheet1')
const sheet2 = ss.getSheets()[1]

function onEdit(e) {
  if (e.range.getSheet().getSheetId() == sheet1.getSheetId() 
  && e.range.getA1Notation() == 'A1') sheet2.setName(e.value)
}

And here is a working example sheet.

Upvotes: 1

Related Questions