Is there a Google Sheets formula to put the name of the sheet into a cell?

后端 未结 9 1357
轮回少年
轮回少年 2020-12-30 18:59

The following illustration should help:

相关标签:
9条回答
  • 2020-12-30 19:06

    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

    0 讨论(0)
  • 2020-12-30 19:08

    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:

    where

    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

    0 讨论(0)
  • 2020-12-30 19:08

    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

    0 讨论(0)
  • 2020-12-30 19:20

    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
    
    0 讨论(0)
  • 2020-12-30 19:23

    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.

    0 讨论(0)
  • 2020-12-30 19:23

    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.

    0 讨论(0)
提交回复
热议问题