问题
For Google sheet workbooks that have several sheets, I create a Table of Contents sheet that lists all of the sheets in the workbook to increase ease of use for users. I have looked for an add-on, macro, or script that can speed up the process. No dice. Any ideas for how to automate the process of creating a new sheet that lists the names of all of the other sheets (One sheet name per cell) and then automatically links the cell to that sheet?
回答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/59428356/g-sheets-table-of-contents-script