Chapter 1 Recording and running macros ・ MISSION 1
Mission 1: Run a recorded macro
Task
This spreadsheet contains a macro called "Format headers" that makes the headers bold and colors them. Run it from "Extensions" → "Macros".
For practice, the table goes back to its starting state every time before the code runs (a real spreadsheet doesn't go back. Script changes can sometimes be undone with Ctrl+Z, but not all at once).
Chapter 1 startsRecording and running macros
Chapter 1 ・ MISSION 1
Run a recorded macro
This spreadsheet contains a macro called "Format headers" that makes the headers bold and colors them. Run it from "Extensions" → "Macros".
Reading the code
- /** @OnlyCurrentDoc */
- Declares that this script only touches this spreadsheet, so the permissions requested on the authorization screen are narrower.
- function myFunction() {
- The start of the macro. The menu name is "Format headers", but the function is called myFunction.
- var spreadsheet = SpreadsheetApp.getActive();
- Puts the spreadsheet that is open now into a box (variable) called spreadsheet.
- spreadsheet.getRange('A1:C1').activate();
- Selects A1 to C1.
- spreadsheet.getActiveRangeList().setFontWeight('bold')
- Makes the selected range bold.
- .setBackground('#fff2cc');
- Then sets the background to pale yellow.
Macros can be run from the list under "Extensions" → "Macros". Only the first time, you are asked whether the script may read and edit the spreadsheet. If you wrote the script yourself, it is fine to continue via "Advanced" and allow it.
How to get started with GAS and common code are covered in an article.What is GAS? Getting started and common code
The code you write runs in an isolated place inside this page. It can't reach outside the page or other sites.
* This is a simulation of how Google Sheets and Apps Script behave and may differ in some ways from the real thing.
* Google Sheets is a trademark of Google LLC. This site is not affiliated with Google.