Skip to content

Instantly share code, notes, and snippets.

@rupertsworld
Created April 21, 2021 12:48
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rupertsworld/17277e24b656cfbedc43c9c8d451d6d4 to your computer and use it in GitHub Desktop.
Save rupertsworld/17277e24b656cfbedc43c9c8d451d6d4 to your computer and use it in GitHub Desktop.
Notes on the Google Sheets Apps Script API

Pretty much all queries start off as: SpreadsheetApp.getActiveSheet()

Class: Sheet

getRange(row, column, numRows, numColumns)

Class: Range

Google Documentation

Event: onSelectionChange

/**
 * The event handler triggered when the selection changes in the spreadsheet.
 * @param {Event} e The onSelectionChange event.
 */
function onSelectionChange(e) {
  // Set background to red if a single empty cell is selected.
  var range = e.range;
  if(range.getNumRows() === 1 
      && range.getNumColumns() === 1 
      && range.getCell(1, 1).getValue() === "") {
    range.setBackground("red");
  }
}

Linking

const sheet = SpreadsheetApp.getActiveSheet();
const rangeToBeLinked = sheet.getRange('A2');
const rangeToAddLink = sheet.getRange('D32')
const richText = SpreadsheetApp.newRichTextValue()
    .setText('Click to go to ' + rangeToBeLinked.getA1Notation())
    .setLinkUrl('#gid=' + sheet.getSheetId() + '&range=' + 'A' + rangeToBeLinked.getRow())
    .build();
rangeTwo.setRichTextValue(richText );

Link hash URLs allow you to jump to other sheets (link to this):

#gid=824217278&range=A4:A6

And get sheet ID with SpreadsheetApp.getActiveSheet().getSheetID()

(Stack Overflow)

Alternatively can set formula for hyperlink (less desirable):

cell.setFormula('=HYPERLINK("http://www.google.com/","Google")');


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