91

The following illustration should help:

enter image description here

orschiro
  • 19,847
  • 19
  • 64
  • 95

10 Answers10

70

Here is what I found for Google Sheets:

To get the current sheet name in Google sheets, the following simple script can help you without entering the name manually, please do as this:

  1. Click Tools > Script editor

  2. In the opened project window, copy and paste the below script code into the blank Code window, see screenshot:

......................

function sheetName() {
  return SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getName();
}

Then save the code window, and go back to the sheet that you want to get its name, then enter this formula: =sheetName() in a cell, and press Enter key, the sheet name will be displayed at once.

See this link with added screenshots: https://www.extendoffice.com/documents/excel/5222-google-sheets-get-list-of-sheets.html

CalamitousCode
  • 1,324
  • 1
  • 14
  • 21
Katya Mutafchieva
  • 709
  • 1
  • 5
  • 3
  • Great answer. This also works for printing other sheets that are not active - so you don't have to worry about that, either. – Rob W Nov 27 '18 at 20:20
  • 12
    Caveat here is if you change the sheet name, the formula does not recalculate correctly :( – Rob W Nov 27 '18 at 20:37
  • Your function name doesn't correspond to your formula. One is camel cased and one isn't. The formula needs to be =sheetName() or your function needs to be renamed 'sheetname' – turtlepower Sep 29 '19 at 03:07
  • @turtlepower While it's nice to keep names in sync as far as casing goes, it doesn't actually affect the function of the formula here. It will match the script just fine. – Logarr Dec 30 '20 at 19:30
  • WhooHOO! Thanks. – h pic Mar 27 '21 at 18:08
  • 3
    Would it be possible to have this answer updated to match the new version of Google Sheets? – Canned Man Feb 20 '22 at 17:11
  • 1
    2 big issues: #1. The ACTIVE sheet/cell is not necessarily the one that contains the function. It could give you the value of a completely different cell. #2. Google Sheets will almost NEVER update it again. So whatever your answer is when you enter this formula into a cell, it will remain. If you rename your tab, the value in the cell still retains the OLD name of the tab. Incidentally, it's because of #2 that #1 isn't always an obvious problem. When you enter the formula into the cell, it is the active cell - and when you change the active cell, it doesn't change the value. – Trashman Apr 20 '22 at 19:15
  • @CannedMan - See my updated response above. Let me know if it works for you and sorry that I'm a year late! – Montag Jan 07 '23 at 20:28
31

You have 2 options, and I am not sure if I am a fan of either of them, but that is my opinion. You may feel differently:

Option 1: Force the function to run.

A function in a cell does not run unless it references a cell that has changed. Changing a sheet name does not trigger any functions in the spreadsheet. But we can force the function to run by passing a range to it and whenever an item in that range changes, the function will trigger.

You can use the below script to create a custom function which will retrieve the name:

function mySheetName() {
  var key = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getName();
  return key;
}

and in the cell place the following:

=mySheetName(A1:Z)

Now if any value in a cell in that passed range changes the script will run. This takes a second to run the script and sets a message in the cell each time any value is changed so this could become annoying very quickly. As already mentioned, it also requires a change in the range to cause it to trigger, so not really helpful on a fairly static file.

Option 2: Use the OnChange Event

While the run time feels better than the above option, and this does not depend on a value changing in the spreadsheet's cells, I do not like this because it forces where the name goes. You could use a Utilities sheet to define this location in various sheets if you wish. Below is the basic idea and may get you started if you like this option.

The OnChange event is triggered when the sheet name is changed. You can make the code below more sophisticated to check for errors, check the sheet ID to only work on a given sheet, etc. The basic code, however, is:

function setSheetName(e) {
  var key = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getName();
  SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getRange('K1').setValue(key);
}

Once you have saved the code, in the script editor set the Current Project's On Change Trigger to this function. It will write the sheet name to cell K1 on any change event. To set the trigger, select Current project's triggers under the Edit menu.

y0mbo
  • 4,582
  • 6
  • 41
  • 45
Karl_S
  • 3,364
  • 2
  • 19
  • 33
  • "The OnChange event is triggered when the sheet name is changed." Unfortunately, that doesn't seem to be the case anymore. Or, in my case that's not happening, though the function does work if any cell in the sheet is updated, of course. That may be good enough, so I appreciate this answer in any case. – Brandon Feb 10 '21 at 17:32
  • Ugh. I don't "like" either of these, either. But it seems Google has left us no choice. Option 1 at least kind of works for me. I just have a header at the top that I want to match the tab name. I make multiple tabs by duplicating, then renaming, then pasting the appropriate data in the table. By doing #1, it will recalculate the sheet name when I do the paste operation... as long as I make sure I do the tab rename FIRST. – Trashman Apr 20 '22 at 19:30
28

If you reference the sheet from another sheet, you can get the sheet name using the CELL function. You can then use regex to extract out the sheet name.

=REGEXREPLACE(CELL("address",'SHEET NAME'!A1),"'?([^']+)'?!.*","$1")

update: The formula will automatically update 'SHEET NAME' with future changes, but you will need to reference a cell (such as A1) on that sheet when the formula is originally entered.

JohnP2
  • 1,899
  • 19
  • 17
  • 2
    Lacks explanation @JohnP2 – Happy Bird Jan 05 '19 at 18:03
  • 1
    That is exactly what I looked for. Thank you. If I enter the sheet name this way, and the sheet name is updated, this formula is automatically updated to get the new sheet name. – Kartal Tabak Jan 24 '19 at 10:33
  • Even if the sheet name in the formula updates automatically, the resulting value doesn't. Even with "recalculate every minute" nothing happens (yes, I checked with the `NOW` function - only that updates every minute while the sheet name doesn't). _(comment continues in Luke's answer)_ – ADTC May 31 '19 at 10:45
  • 2
    Love this solution. – olisteadman Nov 25 '19 at 16:04
15

Not using script:

I think I've found a stupid workaround using =cell() and a helper sheet. Thus avoiding custom functions and apps script.

=cell("address",[reference]) will provide you with a string reference (i.e. "$A$1") to the address of the cell referred to. Problem is it will not provide the sheet reference unless the cell is in a different sheet!

So:

Solution

where

Helper column in helper sheet

This also works for named sheets. Then by all means adjust to work for your use case.

Source: https://docs.google.com/spreadsheets/d/1_iTD6if3Br6nV5Bn5vd0E0xRCKcXhJLZOQqkuSWvDtE/edit#gid=1898848593

EDIT: I've added another workaround in the document that makes use of =formulatext() and some traditional text functions. By referencing to a cell in the current sheet using it's full address, i.e. Sheet1A1 you are able to use formulatext() to extract only the sheet name.

a-burge
  • 1,535
  • 1
  • 13
  • 25
  • Good point. Probably shouldn't name sheets with special characters anyways. Using a space is already pushing it when it comes to linking stuff together. – a-burge Aug 31 '20 at 12:00
  • 2
    This is an excellent tip. Regarding your first tip: =regexreplace(cell( "address", !A1 ), "![^!]+$", "" ) will return '' and will be immune to sheet renaming but won't work in the local sheet. You'r other tip, based on formulatext works well in the local sheet but won't refresh on sheet renaming, unless you reload or change the formula itself. Thanks for these tips! – Jean-Rene Bouvier Feb 24 '22 at 08:31
13

Here is my proposal for a script which returns the name of the sheet from its position in the sheet list in parameter. If no parameter is provided, the current sheet name is returned.

function sheetName(idx) {
  if (!idx)
    return SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getName();
  else {
    var sheets = SpreadsheetApp.getActiveSpreadsheet().getSheets();
    var idx = parseInt(idx);
    if (isNaN(idx) || idx < 1 || sheets.length < idx)
      throw "Invalid parameter (it should be a number from 0 to "+sheets.length+")";
    return sheets[idx-1].getName();
  }
}

You can then use it in a cell like any function

=sheetName() // display current sheet name
=sheetName(1) // display first sheet name
=sheetName(5) // display 5th sheet name

As described by other answers, you need to add this code in a script with :

Tools > Script editor
Nicolas Janel
  • 3,025
  • 1
  • 28
  • 31
7

I got this to finally work in a semi-automatic fashion without the use of scripts... but it does take up 3 cells to pull it off. Borrowing from a bit from previous answers, I start with a cell that has nothing more than =NOW() it in to show the time. For example, we'll put this into cell A1...

=NOW()

This function updates automatically every minute. In the next cell, put a pointer formula using the sheets own name to point to the previous cell. For example, we'll put this in A2...

='Sheet Name'!A1

Cell formatting aside, cell A1 and A2 should at this point display the same content... namely the current time.

And, the last cell is the part I'm borrowing from previous solutions using a regex expression to pull the fomula from the second cell and then strip out the name of the sheet from said formula. For example, we'll put this into cell A3...

=REGEXREPLACE(FORMULATEXT(A2),"='?([^']+)'?!.*","$1")

At this point, the resultant value displayed in A3 should be the name of the sheet.

From my experience, as soon as the name of the sheet is changed, the formula in A2 is immediately updated. However that's not enough to trigger A3 to update. But, every minute when cell A1 recalculates the time, the result of the formula in cell A2 is subsequently updated and then that in turn triggers A3 to update with the new sheet name. It's not a compact solution... but it does seem to work.

Craig
  • 627
  • 7
  • 14
  • one thing I discovered today after playing around with this solution is that the regex expression does not work if your sheet name has a single quote mark in it. Say for instance you have "Joe's Sheet" as the name, the apostrophe after joe will screw up the expression. I'm not terribly savvy at regex... so perhaps someone else can come up with a suitable revision to handle such issues. – Craig Jul 07 '19 at 01:08
  • 1
    It's maddening that Google makes it this difficult to do such a simple thing. – tlewis3348 Apr 10 '21 at 12:08
  • This is definitely the best solution! At least for me. – Dr Xorile Oct 28 '21 at 19:46
7

An old thread, but a useful one... so here's some additional code.

First, in response to Craig's point about the regex being overly greedy and failing for sheet names containing a single quote, this should do the trick (replace 'SHEETNAME'!A1 with your own sheet & cell reference):

=IF(TODAY()=TODAY(), SUBSTITUTE(REGEXREPLACE(CELL("address",'SHEETNAME'!A1),"'?(.+?)'?!\$.*","$1"),"''","'", ""), "")

It uses a lazy match (the ".+?") to find a character string (squotes included) that may or may not be enclosed by squotes but is definitely terminated by bang dollar ("!$") followed by any number of characters. Google Sheets actually protects squotes within a sheet name by appending another squote (as in ''), so the SUBSTITUTE is needed to reduce these back to single squotes.

The formula also allows for sheet names that contain bangs ("!"), but will fail for names using bang dollars ("!$") - if you really need to make your sheet names to look like full absolute cell references then put a separating character between the bang and the dollar (such as a space).

Note that it will only work correctly when pointed at a different sheet from the one that the formula resides! This is because CELL("address" returns just the cell reference (not the sheet name) when used on the same sheet. If you need a sheet to show its own name then put the formula in a cell on another sheet, point it at your target sheet, and then reference the formula cell from the target sheet. I often have a "Meta" sheet in my workbooks to hold settings, common values, database matching criteria, etc so that's also where I put this formula.

As others have said many times above, Google Sheets will only notice changes to the sheet name if you set the workbook's recalculation to "On change and every minute" which you can find on the File|Settings|Calculation menu. It can take up to a whole minute for the change to be picked up.


Secondly, if like me you happen to need an inter-operable formula that works on both Google Sheets and Excel (which for older versions at least doesn't have the REGEXREPLACE function), try:

=IF(IFERROR(INFO("release"), 0)=0, IF(TODAY()=TODAY(), SUBSTITUTE(REGEXREPLACE(CELL("address",'SHEETNAME'!A1),"'?(.+?)'?!\$.*","$1"),"''","'", ""), ""), MID(CELL("filename",'SHEETNAME'!A1),FIND("]",CELL("filename",'SHEETNAME'!A1))+1,255))

This uses INFO("release") to determine which platform we are on... Excel returns a number >0 whereas Google Sheets does not implement the INFO function and generates an error which the formula traps into a 0 and uses for numerical comparison. The Google code branch is as above.

For clarity and completeness, this is the Excel-only version (which does correctly return the name of the sheet it resides on):

=MID(CELL("filename",'SHEETNAME'!A1),FIND("]",CELL("filename",'SHEETNAME'!A1))+1,255)

It looks for the "]" filename terminator in the output of CELL("filename" and extracts the sheet name from the remaining part of the string using the MID function. Excel doesn't allow sheet names to contain "]" so this works for all possible sheet names. In the inter-operable version, Excel is happy to be fed a call to the non-existent REGEXREPLACE function because it never gets to execute the Google code branch.

Angeltek
  • 71
  • 1
  • 2
  • When the sheet name contains Cyrillic characters, it is enclosed in single quotes. This formula removes them and it causes problems when the extracted name of the sheet is used in INDIRECT() function. – Andrej Adamenko Aug 30 '20 at 07:35
  • Enclosing the formula with `"'" & .... & "'"` fixes the problem and works for both - Latin and non-Latin characters – Andrej Adamenko Aug 30 '20 at 08:23
6

I have a sheet that is made to used by others and I have quite a few indirect() references around, so I need to formulaically handle a changed sheet tab name.

I used the formula from JohnP2 (below) but was having trouble because it didn't update automatically when a sheet name was changed. You need to go to the actual formula, make an arbitrary change and refresh to run it again.

=REGEXREPLACE(CELL("address",'SHEET NAME'!A1),"'?([^']+)'?!.*","$1")

I solved this by using info found in this solution on how to force a function to refresh. It may not be the most elegant solution, but it forced Sheets to pay attention to this cell and update it regularly, so that it catches an updated sheet title.

=IF(TODAY()=TODAY(), REGEXREPLACE(CELL("address",'SHEET NAME'!A1),"'?([^']+)'?!.*","$1"), "")

Using this, Sheets know to refresh this cell every time you make a change, which results in the address being updated whenever it gets renamed by a user.

Luke Allpress
  • 170
  • 1
  • 2
  • 8
  • 1
    The second snippet is a little better than JohnP2's answer, but it still doesn't update the sheet name fully automatically. After I change the sheet name, I have to force any cell in the sheet to recalculate (by editing a cell with a change). I could also change the recalculation settings to be "Every minute" which will also work, updating the sheet name at the mark of the minute. – ADTC May 31 '19 at 10:47
  • I agree, my formula used to work--but here's something worth knowing: my INDIRECT formulas no longer need an updated sheet name! Google Sheets behind the scenes remembers the old sheet name. The INDIRECT formulas are still working. – JohnP2 Jun 23 '19 at 03:55
  • I also just discovered that if the single referenced cell is deleted, the formula breaks. I referenced the entire sheet `'SHEET NAME'!1:25` and the issue seems to have resolved. As long as any of the range still exists, the formula continues to work. – TC76 Jul 13 '19 at 19:01
1

To match rare sheets names like:

Wow!
Oh'Really!
''!

use the formula:

=SUBSTITUTE(REGEXEXTRACT(CELL("address";Sheet500!A1);"'?((?U).*)'?!\$[A-Za-z]+\$\d+$");"''";"'")

or

=IF(NOW();SUBSTITUTE(REGEXEXTRACT(FORMULATEXT(A1);"='?((?U).*)'?![A-Za-z]+\d+$");"''";"'")) if A1 is formula reference to your sheet.

Max Makhrov
  • 17,309
  • 5
  • 55
  • 81
0

if you want to use build-in functions:

=REGEXEXTRACT(cell("address";'Sheet1'!A1);"^'(.*)'!\$A\$1$")

Explanation: cell("address";'Sheet1'!A1) gives you the address of the sheet, output is 'Sheet1'!$A$1. Now we need to extract the actual sheet name from this output. I'm using REGEXEXTRACT to match it by regex ^'(.*)'!\$A\$1$, but you can either use more/less specific regex or use functions like SUBSTITUTE or REPLACE

k-sever
  • 975
  • 10
  • 13