简体   繁体   English

是否有 Google 表格公式可以将表格名称放入单元格中?

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

The following illustration should help:下图应该会有所帮助:

在此处输入图像描述

Here is what I found for Google Sheets:这是我在 Google 表格中找到的内容:

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:要在 Google 表格中获取当前表格名称,以下简单脚本可以帮助您无需手动输入名称,请按以下步骤操作:

  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.然后保存代码窗口,回到要取名字的工作表,然后在一个单元格中输入这个公式: =sheetName() ,然后按回车键,工作表名称将立即显示出来。

See this link with added screenshots: https://www.extendoffice.com/documents/excel/5222-google-sheets-get-list-of-sheets.html请参阅此链接并添加屏幕截图: https : //www.extendoffice.com/documents/excel/5222-google-sheets-get-list-of-sheets.html

You have 2 options, and I am not sure if I am a fan of either of them, but that is my opinion.您有 2 个选择,我不确定我是否是其中任何一个的粉丝,但这是我的意见。 You may feel differently:你可能会有不同的感受:

Option 1: Force the function to run.选项 1:强制函数运行。

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选项 2:使用 OnChange 事件

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.当工作表名称更改时触发 OnChange 事件。 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:您可以使下面的代码更复杂以检查错误,检查工作表 ID 以仅在给定工作表上工作等。但是,基本代码是:

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.保存代码后,在脚本编辑器中将 Current Project 的 On Change Trigger 设置为此函数。 It will write the sheet name to cell K1 on any change event.它将在任何更改事件中将工作表名称写入单元格 K1。 To set the trigger, select Current project's triggers under the Edit menu.要设置触发器,请在“编辑”菜单下选择“当前项目的触发器”。

If you reference the sheet from another sheet, you can get the sheet name using the CELL function.如果您从另一张工作表中引用该工作表,则可以使用 CELL 函数获取工作表名称。 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.更新:公式将自动更新“工作表名称”以进行未来更改,但您需要在最初输入公式时引用该工作表上的单元格(例如 A1)。

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

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):首先,为了回应 Craig 关于正则表达式过于贪婪并且对于包含单引号的工作表名称失败的观点,这应该可以解决问题(用您自己的工作表和单元格引用替换 'SHEETNAME'!A1 ):

=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.它使用惰性匹配(“.+?”)来查找一个字符串(包括引号),该字符串可能被或可能不被引号包围,但肯定以 bang 美元(“!$”)结尾,后跟任意数量的字符. 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. Google 表格实际上通过附加另一个引号(如 '')来保护工作表名称中的引号,因此需要 SUBSTITUTE 将这些引号减少回单引号。

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).该公式还允许包含刘海 ("!") 的工作表名称,但对于使用 bang 美元 ("!$") 的名称将失败 - 如果您确实需要使工作表名称看起来像完整的绝对单元格引用,则输入bang 和美元之间的分隔字符(例如空格)。

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.这是因为 CELL("address" 在同一工作表上使用时仅返回单元格引用(而不是工作表名称)。如果您需要工作表显示其自己的名称,请将公式放在另一张工作表的单元格中,将其指向你的目标表,然后从目标表中引用公式单元格。我的工作簿中经常有一个“元”表来保存设置、通用值、数据库匹配标准等,所以这也是我放置这个公式的地方。

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.正如其他人在上面多次说过的那样,如果您将工作簿的重新计算设置为“更改时和每分钟”,您可以在文件|设置|计算菜单中找到,Google 表格只会注意到工作表名称的更改。 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:其次,如果像我一样,您碰巧需要一个适用于 Google Sheets 和 Excel 的可互操作的公式(对于旧版本至少没有 REGEXREPLACE 函数),请尝试:

=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.这使用 INFO("release") 来确定我们在哪个平台上...... Excel 返回一个 > 0 的数字,而 Google Sheets 没有实现 INFO 函数并生成一个错误,公式将其捕获为 0 并用于数值比较。 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):为清晰和完整起见,这是仅限 Excel 的版本(它确实正确返回了它所在的工作表的名称):

=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.它在 CELL("filename" 的输出中查找 "]" 文件名终止符,并使用 MID 函数从字符串的剩余部分中提取工作表名称。Excel 不允许工作表名称包含 "]" 所以这是有效的对于所有可能的工作表名称。在可互操作的版本中,Excel 很高兴能够调用不存在的 REGEXREPLACE 函数,因为它永远不会执行 Google 代码分支。

Not using script:不使用脚本:

I think I've found a stupid workaround using =cell() and a helper sheet.我想我找到了一个使用=cell()和帮助表的愚蠢解决方法。 Thus avoiding custom functions and apps script.从而避免自定义函数和应用程序脚本。

=cell("address",[reference]) will provide you with a string reference (ie "$A$1") to the address of the cell referred to. =cell("address",[reference])将为您提供一个字符串引用(即“$A$1”)到所引用单元格的地址。 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资料来源: https : //docs.google.com/spreadsheets/d/1_iTD6if3Br6nV5Bn5vd0E0xRCKcXhJLZOQqkuSWvDtE/edit#gid=1898848593

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.我终于在不使用脚本的情况下以半自动方式工作……但它确实需要 3 个单元格才能完成。 Borrowing from a bit from previous answers, I start with a cell that has nothing more than =NOW() it in to show the time.从以前的答案中借用一点,我从一个单元格开始,该单元格仅包含 =NOW() 以显示时间。 For example, we'll put this into cell A1...例如,我们将其放入单元格 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...例如,我们将把它放在 A2...

='Sheet Name'!A1

Cell formatting aside, cell A1 and A2 should at this point display the same content... namely the current time.撇开单元格格式不谈,此时单元格 A1 和 A2 应该显示相同的内容……即当前时间。

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...例如,我们将其放入单元格 A3 ...

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

At this point, the resultant value displayed in A3 should be the name of the sheet.此时,A3 中显示的结果值应该是工作表的名称。

From my experience, as soon as the name of the sheet is changed, the formula in A2 is immediately updated.根据我的经验,只要更改工作表的名称,A2 中的公式就会立即更新。 However that's not enough to trigger A3 to update.然而,这还不足以触发 A3 更新。 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.但是,当单元格 A1 重新计算时间时,每分钟都会更新单元格 A2 中的公式结果,然后又会触发 A3 使用新的工作表名称进行更新。 It's not a compact solution... but it does seem to work.这不是一个紧凑的解决方案......但它似乎确实有效。

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.我使用了 JohnP2(如下)中的公式,但遇到了问题,因为它在更改工作表名称时没有自动更新。 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.这可能不是最优雅的解决方案,但它迫使 Sheets 关注此单元格并定期更新它,以便它捕获更新的工作表标题。

=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.使用此功能,表格知道每次进行更改时都会刷新此单元格,这会导致地址在用户重命名时更新。

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 .说明: cell("address";'Sheet1'!A1)为您提供工作表的地址,输出为'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我正在使用REGEXEXTRACT通过正则表达式^'(.*)'!\\$A\\$1$匹配它,但是您可以使用更多/更少特定的正则表达式或使用诸如SUBSTITUTEREPLACE 之类的函数

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. =IF(NOW();SUBSTITUTE(REGEXEXTRACT(FORMULATEXT(A1);"='?((?U).*)'?![A-Za-z]+\\d+$");"''";"'"))如果A1是对您的工作表的公式引用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 是否有 Google Sheets 公式可以将文件名放入单元格中? - Is there a Google Sheets formula to put the name of the filename into a cell? 使用单元格在 Google 表格的公式中引用表格名称 - Using A Cell To Reference The Name Of Sheet In A Formula For Google Sheets 谷歌表格公式引用另一个包含工作表名称和范围的单元格值 - Google Sheets formula to reference to another cell value that contains a sheet name & range 用于增加工作表编号而不是Google工作表中单元格编号的公式 - Formula to increment value of sheet no and not the cell number in google sheets 从下拉选择 Google 表格中将“表格名称”添加到公式地址 - Add the 'sheet name' to a formula address from a dropdown selection Google sheets Google表格-根据字符串输入在公式中包含表格名称 - Google Sheets - Include Sheet Name in formula depending on string input Google表格-费用表的公式 - Google Sheets - formula for expense sheet 谷歌工作表很容易把一个相似的工作表名称放在一个变量中 - Google sheets easily put a similar sheet name in a variable "Google Sheets 动态公式,用于在多个工作表中获取工作表名称和汇总单元格值" - Google Sheets dynamic formula to fetch sheet names and sum cell values across multiple sheets 如何使用主工作表中单元格中的工作表名称来引用谷歌工作表中的工作表? - How do I reference a sheet in google sheets by using name of the sheet name from a cell within the main sheet?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM