Google Spreadsheet: Dynamic Hyperlink Formula

I'm trying to create a dynamic HYPERLINK formula that will automatically create a link based on the sheet name in column A, but I don't know how (or if possible) to get the name based sheet URL.

Here's the setting:

  • Separate Google spreadsheet with multiple tabs
  • Table names: 1500, 1501, 1502, Consolidation.

In the Consolidation tab, I have two columns, column A is the name of the sheet, and column B is the HYPERLINK formula, which should open the corresponding sheet when clicked.

Is there a way to programmatically get the URL for a sheet based on the sheet name in column A? Perhaps I could use a script to populate column C with the url and then use the following formula: = HYPERLINK (C2, A2)?

Thanks for the help!

+5


source to share


3 answers


Surprised to see this as the best google search but no answer.

Anyway, here's the method I found works for me: Combine a hyperlink with values ​​from another column using &

, a basic example is shown below:



dynamiclly generate hyperlinks in Google Sheets

+1


source


If you're trying to automatically generate a direct url for specific sheets based on their name and don't want to use scripts, you're out of luck. Currently, the only way to directly link to specific sheets is to add the correct gid number to the spreadsheet url. The guide must be copied manually from the active sheet URL, or automatically retrieved using a custom scripted function.



+1


source


Since you're open to scripting, it looks like I've found a detailed guide on how to do this. https://www.benlcollins.com/spreadsheets/index-sheet/

function onOpen() {

  var ui = SpreadsheetApp.getUi();

  ui.createMenu('Index Menu')
      .addItem('Create Index', 'createIndex')
      .addItem('Update Index', 'updateIndex')
      .addToUi();
}


// function to create the index
function createIndex() {

  // Get all the different sheet IDs
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheets = ss.getSheets();

  var namesArray = sheetNamesIds(sheets);

  var indexSheetNames = namesArray[0];
  var indexSheetIds = namesArray[1];

  // check if sheet called sheet called already exists
  // if no index sheet exists, create one
  if (ss.getSheetByName('index') == null) {

    var indexSheet = ss.insertSheet('Index',0);

  }
  // if sheet called index does exist, prompt user for a different name or option to 
cancel
  else {

    var indexNewName = Browser.inputBox('The name Index is already being used, 
please choose a different name:', 'Please choose another name', 
Browser.Buttons.OK_CANCEL);

    if (indexNewName != 'cancel') {
      var indexSheet = ss.insertSheet(indexNewName,0);
    }
    else {
      Browser.msgBox('No index sheet created');
    }

  }

  // add sheet title, sheet names and hyperlink formulas
  if (indexSheet) {

    printIndex(indexSheet,indexSheetNames,indexSheetIds);

  }

}



// function to update the index, assumes index is the first sheet in the workbook
function updateIndex() {

  // Get all the different sheet IDs
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheets = ss.getSheets();
  var indexSheet = sheets[0];

  var namesArray = sheetNamesIds(sheets);

  var indexSheetNames = namesArray[0];
  var indexSheetIds = namesArray[1];

  printIndex(indexSheet,indexSheetNames,indexSheetIds);
}


// function to print out the index
function printIndex(sheet,names,formulas) {

  sheet.clearContents();

  sheet.getRange(1,1).setValue('Workbook Index').setFontWeight('bold');
  sheet.getRange(3,1,names.length,1).setValues(names);
  sheet.getRange(3,2,formulas.length,1).setFormulas(formulas);

}


// function to create array of sheet names and sheet ids
function sheetNamesIds(sheets) {

  var indexSheetNames = [];
  var indexSheetIds = [];

  // create array of sheet names and sheet gids
  sheets.forEach(function(sheet){
    indexSheetNames.push([sheet.getSheetName()]);
    indexSheetIds.push(['=hyperlink("#gid=' 
                        + sheet.getSheetId() 
                        + '","' 
                        + sheet.getSheetName() 
                        + '")']);
  });

  return [indexSheetNames, indexSheetIds];

}

      

0


source







All Articles