Is there a custom function or script that returns gid of a specific sheet in Google Sheets?

跟風遠走 提交于 2021-01-24 09:39:10

问题


I have been poring through the vast sea of knowledge on the Internet to get what I need but to no avail.

Context

In a single Google Sheets file, I have one master sheet and various other sheets named after the last names of people.

In the master sheet, I created a cell in which I can type the name of any person. I would like to look up sheets for multiple individuals, so my search is dynamic. Anyway, let's call this cell the SEARCH CELL.

Right next to the SEARCH CELL, I have another cell in which I would like to add a hyperlink to another sheet which bears the last name entered in the SEARCH CELL. Let's call this cell with the hyperlink the HYPERLINK CELL. So, when I click on the hyperlink in this cell, I would like to jump to the sheet with the last name of the individual that matches what had been entered in the SEARCH CELL.

The help I need is with this HYPERLINK CELL.

=hyperlink("#gid=               ")

How do I extract the gid of each sheet and enter in the blank space in the code above?

I am told that there is no Google Sheets function for this, so a custom function or script is needed. Can anyone please provide one that works (i.e. runs without errors) when it is entered in the Script Editor?


回答1:


script:

function SHEETLIST() {
try {
  var sheets = SpreadsheetApp.getActiveSpreadsheet().getSheets()
  var out = new Array( sheets.length+1 ) ;
  out[0] = [ "NAME" , "#GID" ];
  for (var i = 1 ; i < sheets.length+1 ; i++ ) out[i] = 
  [sheets[i-1].getName() , sheets[i-1].getSheetId() ];
  return out
}
catch( err ) {
  return "#ERROR!" }}

formula:

=SHEETLIST()

turn sheet names into active hyperlinks:

=ARRAYFORMULA(HYPERLINK("#gid="&
 QUERY(INDEX(SHEETLIST();;2); "offset 1"); 
 QUERY(INDEX(SHEETLIST();;1); "offset 1")))

and if you want to bind it to manual input you can use VLOOKUP like

=ARRAYFORMULA(IFNA(VLOOKUP(A1, HYPERLINK("#gid="&
 QUERY(INDEX(SHEETLIST();;2); "offset 1"); 
 QUERY(INDEX(SHEETLIST();;1); "offset 1")); 1; 0)))

where A1 is your "search cell"



来源:https://stackoverflow.com/questions/58711457/is-there-a-custom-function-or-script-that-returns-gid-of-a-specific-sheet-in-goo

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!