Hyperlink to a specific sheet

后端 未结 6 1957
猫巷女王i
猫巷女王i 2021-02-08 01:45

I would like to open a specific sheet of a Google Sheets from a hyperlink in another spreadsheet.

I have different links in my master spreadsheet and each should have a

相关标签:
6条回答
  • 2021-02-08 02:17

    So what I understand from the OP is that you have one master spreadsheet that you want to have links to individual sheets, where one or more of those sheets may be in single or multiple spreadsheet files.

    The HYPERLINK function only turns a URL into a hyperlink and is really only useful when you want to have hypertext instead of just a link. If you enter the raw URL as the data, it's automatically turned into a hyperlink, so there's no additional work.

    As mentioned in other answers, the solution is to have the spreadsheet's URL then use the gid value to calculate the link to the desired sheet within the spreadsheet. You can write a simple app that collects all of the individual sheets' links and writes them into the master.

    Below are some snippets of pseudocode (Python) that can help you get started. I'm leaving out all the boilerplate auth code, but if you need it, see this blog post and this video. The code below assumes your API service endpoint is SHEETS.

    This reads a target spreadsheet to build links for each of its sheets:

    # open target Sheet, get all sheets & Sheet URL
    SHEET_ID = TARGET_SHEET_DRIVE_FILE_ID
    res = SHEETS.spreadsheets().get(spreadsheetId=SHEET_ID,
            fields='sheets,spreadsheetUrl').execute()
    sheets = res.get('sheets', [])
    url = res['spreadsheetUrl']
    
    # for each sheet, dump out its name & full URL
    for sheet in sheets:
        data = sheet['properties']
        print('** Sheet title: %r' % data['title'])
        print(' - Link: %s#gid=%s' % (url, data['sheetId']))
    

    Instead of printing to the screen, let's say you stored them in a (name, URL) 2-tuple array in your app, so bottom-line, it looks something like this list called sheet_data:

    sheet_data = [
        ('Intro', 'https://docs.google.com/spreadsheets/d/SHEET_ID/edit#gid=5'),
        ('XData', 'https://docs.google.com/spreadsheets/d/SHEET_ID/edit#gid=3'),
        ('YData', 'https://docs.google.com/spreadsheets/d/SHEET_ID/edit#gid=7')
    ]
    

    You can then write them to the master (starting from the upper-left corner, cell A1) like this:

    SHEET_ID = MASTER_SHEET_DRIVE_FILE_ID
    SHEETS.spreadsheets().values().update(
        spreadsheetId=SHEET_ID, range='A1',
        body={'values': sheet_data},
        valueInputOption='USER_ENTERED'
    ).execute()
    

    Some caveats when using gid:

    • The first default sheet created for you (Sheet1) always has a gid=0.
    • Any sheets you add after that will have a random gid.
    • Don't bank on a gid=0 for the 1st sheet in your spreadsheets however as you or someone else may have deleted the original default sheet, like my example above.

    If you want to see more examples of using the Sheets API, here are more videos I've made (along with posts that delve into each code sample):

    • Migrating SQL data to a Sheet plus code deep dive post
    • Formatting text using the Sheets API plus code deep dive post
    • Generating slides from spreadsheet data plus code deep dive post

    Then when you open up the master in the Sheets UI, you can clickthrough to any of the individual sheets, regardless of which spreadsheet files they're in. If you want them automatically opened by another app or script, most programming languages offer developers a ways to launch a web browser given the target URL. In Python, it would be the webbrowser module (docs):

    import webbrowser
    webbrowser.open_new(url) # or webbrowser.open_new_tab(url)
    
    0 讨论(0)
  • 2021-02-08 02:21

    You can use this custom script (Tools > Script Editor) function and connect it with e.g. custom drawing (Insert > Drawing... > Save and Close, then right click on new drawing> Assign Script... > "goToSheet2")

    function goToSheet2() {
      goToSheet("Sheet2");
    }
    
    function goToSheet(sheetName) {
      var sheet = SpreadsheetApp.getActive().getSheetByName(sheetName);
      SpreadsheetApp.setActiveSheet(sheet);
    }
    

    Update:
    In the newest version you can select cell and add link (Insert > Link) and select link to specific sheet directly:

    0 讨论(0)
  • 2021-02-08 02:23

    The HYPERLINK function can link to another sheet in the same workbook; if you observe the URL of the spreadsheet, at the end of it there is #gid=x where x is unique for each sheet.

    The problem is, it will open the sheet as a new instance of the spreadsheet in another tab, which is probably not desirable. The workaround would be to insert images or drawings as buttons, and assigning a script to them that will activate specific sheets.

    0 讨论(0)
  • 2021-02-08 02:23

    In case you want to create a link to another sheet which will open the sheet in the same browser tab here is what you want to do: 1. Get the id of the sheet. Check the link in your browser and you will see #gid=x where x is the sheet id 2. Then you want to set the formula (hyperlink) to the cell and make it show as a hyperlink SpreadsheetApp.getActiveSheet().getRange("A1").setFormula('=HYPERLINK("#gid=X","test")').setShowHyperlink(true); If you don't use setShowHyperlink(true) it will be shown as a regular text.

    This is basically a code version for the update provided by @rejthy above

    0 讨论(0)
  • 2021-02-08 02:28

    I personnaly did this based on what @rejthy said: In scripts I created this function:

    /**
     * Return the id of the sheet. (by name)
     *
     * @return The ID of the sheet
     * @customfunction
     */
    function GET_SHEET_ID(sheetName) {
        var sheetId = SpreadsheetApp.getActive().getSheetByName(sheetName).getSheetId();
        return sheetId;
    }
    

    and then in my sheet where I need the link I did this: =HYPERLINK("#gid="&GET_SHEET_ID("Factures - "&$B$1);"Année en cours")

    0 讨论(0)
  • 2021-02-08 02:35

    Alternatively, you can try creating a custom function. With the spreadsheet open, click the Tools menu, then Script editor.... Paste the code into the editor:

    /**
     * Gets the Sheet ID from Sheet Name
     *
     * @param {string} input The Sheet Name
     * @return The Sheet ID
     * @customfunction
     */
    function SHEETID(input) {
      var ss = SpreadsheetApp.getActiveSpreadsheet();
      var tab = ss.getSheetByName(input);
      return tab.getSheetId();
    }
    

    Save, refresh the spreadsheet, and then type in your custom function

    =SHEETID("Your Custom Sheet Name")
    
    =SHEETID(A1)
    

    And voila! The unique ID for the tab (in the current spreadsheet) is output. You can hyperlink to it by using the following formula:

    =HYPERLINK("#gid="&SHEETID(A1),"Link")
    
    0 讨论(0)
提交回复
热议问题