MayaGans
MayaGans

Reputation: 1845

Creating A Googlesheet Hyperlink using GoogleScripts

Using the code below I am able to generate a hyperlink to each page within my googlesheet, with the name of that sheet:

/**
 * Gets the Sheet Name of a selected Sheet.
 *
 * @param {number} option 0 - Current Sheet, 1  All Sheets, 2 Spreadsheet filename
 * @return The input multiplied by 2.
 * @customfunction
 */

function SHEETNAME(option) {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getActiveSheet()
  var thisSheet = sheet.getName();
  var thisgid = sheet.getSheetId();

  //Current option Sheet Name
  if(option === 0){
    return thisSheet;

  //All Sheet Names in Spreadsheet
  }else if(option === 1){

    var sheet1 = [];
    ss.getSheets().forEach(function(val){
      sheet1.push(`=HYPERLINK("#gid=${val.getSheetId()}","${val.getName()}")`)
    });
    return sheet1;  

  //The Spreadsheet File Name
  }else if(option === 2){
    return ss.getName();

  //Error  
  }else{
    return "#N/A";
  };
};

 function GETLINK(option) {
   var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(option);
if (sheet != null) {
  return(sheet.getId());
};
 };

This returns a column of appropriate functions in each cells but does not evaluate the function. If I copy the function into another cell it does evaluate?!

=HYPERLINK("#gid=XXXXXXXXXXXXX","SHEET1")

I've tried also tried adding HTML but this also becomes a string and is not evaluated

sheet1.push(`<a href="#gid=${val.getSheetId()}">${val.getName()}</a>)

Upvotes: 0

Views: 84

Answers (1)

Tanaike
Tanaike

Reputation: 201378

  • When the custom formula of =SHEETNAME(0) is put to a cell, you want to put the sheet name of the active sheet on the Spreadsheet.
  • When the custom formula of =SHEETNAME(1) is put to a cell, you want to put the formulas of =HYPERLINK("#gid=XXXXXXXXXXXXX","SHEET1") for all sheets.
  • When the custom formula of =SHEETNAME(2) is put to a cell, you want to put the Spreadsheet name of the active Spreadsheet.

If my understanding is correct, how about this answer?

Issue and workaround:

Unfortunately, in the current specification of Google side, the formula cannot be put to a cell by the custom formula. When the formula is put to a cell by the custom formula, the formula is put as the string value. I think that this is the reason of your issue.

As a workaround, I would like to propose to put the formula of =HYPERLINK("#gid=XXXXXXXXXXXXX","SHEET1") using the OnEdit event trigger when the custom formula of =SHEETNAME(#) is put to a cell. I think that by this workaround, your goal can be achieved.

When your script is modified, please modify as follows.

Modified script:

In this case, the simple trigger can be used. So please copy and paste the following script to the script editor, and save the script. In order to use this script, as a test, please put =SHEETNAME(1) to a cell.

function onEdit(e) {
  const spreadsheet = e.source;
  const range = e.range;
  const sheet = range.getSheet();
  const formula = range.getFormula();
  const f = formula.match(/\=SHEETNAME\((\d+)\)/i);
  if (f && f.length > 0) {
    if (f[1] == "0") {
      range.setValue(sheet.getSheetName());
    } else if (f[1] == "1") {
      var formulas = spreadsheet.getSheets().map(val => [`=HYPERLINK("#gid=${val.getSheetId()}","${val.getName()}")`]);
      range.offset(0, 0, formulas.length, 1).setFormulas(formulas);
    } else if (f[1] == "2") {
      range.setValue(spreadsheet.getName());
    }
  }
}
  • In this case, there is the function of SHEETNAME() in your script editor. So when =SHEETNAME(1) is put to a cell, at first, the function of SHEETNAME() is run, and then, the function of onEdit is automatically run by the OnEdit event trigger. If you don't want to show the values from SHEETNAME(), please replace the function of SHEETNAME() as follows.

      const SHEETNAME = () => "";
    

Note:

  • In this case, when you directly run the function of onEdit at the script editor, an error occurs. Please be careful this.
  • This script is run under V8.

References:

Upvotes: 2

Related Questions