Skip to content

Instantly share code, notes, and snippets.

@mogsdad
Last active January 13, 2022 17:58
Show Gist options
  • Save mogsdad/1956f50b509b4f0e9ef44a6837fa52ae to your computer and use it in GitHub Desktop.
Save mogsdad/1956f50b509b4f0e9ef44a6837fa52ae to your computer and use it in GitHub Desktop.
Google Apps Script for Google Spreadsheet. This onEdit function will identify Jira issue keys that are alone in an edited cell, and replace them with a HYPERLINK function with a link to your Jira installation.
@AlexanderArvidsson
Copy link

AlexanderArvidsson commented Jan 13, 2022

Here is a version that handles every cell in the whole range selection rather than top-left cell:

// Jira-hyperlink from https://gist.github.com/mogsdad/1956f50b509b4f0e9ef44a6837fa52ae

const jiraUrl = "https://jira.example.com" // Top-level URL for our Jira instance


function onEdit(e) {
  // AppScript execution log errors because e is undefined, not sure why it tries to call with undefined event.
  if (!e) return;
  
  const values = e.range.getValues();
  values.forEach((row, i) => {
    row.forEach((content, j) => {
      const re = /^ *[a-z][a-z0-9_]+-\d{1,6} *$/gi;

      if (re.test(content)) {
        const cell = e.range.getCell(i + 1, j + 1);
        cell.setFormula('HYPERLINK("'+jiraUrl+'/browse/%BUGNUM%";"%BUGNUM%")'.replace(/%BUGNUM%/g,content.toUpperCase().trim()));
      }
    });
  });
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment