Skip to content

Instantly share code, notes, and snippets.

@redbar0n
Last active April 30, 2020 14:37
Show Gist options
  • Save redbar0n/a17b099bcffe8bd09be6ed9dd38957ac to your computer and use it in GitHub Desktop.
Save redbar0n/a17b099bcffe8bd09be6ed9dd38957ac to your computer and use it in GitHub Desktop.
Google Sheets - How to format cells through code in Google Sheets. Option 2: When user initiates your script
// Add custom menu to spreadsheet
function onOpen() {
var ui = SpreadsheetApp.getUi();
ui.createMenu('Run custom script')
.addItem('Set background of a script-declared cell (A1)', 'setBackgroundOfScriptDeclaredCell')
.addToUi();
}
// This does not set the color of a user-selected cell like using getActiveCell does, but it necessarily relies on getActiveSheet(), since ranges are defined as referencing cells within sheets.
function setBackgroundOfScriptDeclaredCell() {
var sheet = SpreadsheetApp.getActiveSheet();
var range = sheet.getRange("A1");
range.setBackground("red");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment