This guide shows you how to write and run scripts in Google Sheets to automate tasks, fix errors, and create custom tools. You need a Google account and a web browser. This works on any device with internet access, including desktops, tablets, and phones.
What You'll Need
- A Google account. This is required to access Google Sheets and the script editor.
- A web browser (Chrome, Edge, Safari, Firefox, etc.).
- A Google Sheet with some data. You can create a new blank sheet or use an existing one.
- Basic familiarity with spreadsheets, like selecting cells or using formulas.
Step-by-Step
- Create or open your spreadsheet. Go to sheets.google.com and create a new file or open an existing one. The script runs on the current sheet, so ensure the data you want to manipulate is visible.
- Open the Apps Script editor. In the menu bar at the top of the window, click Extensions (or Add-ons on some interfaces). From the dropdown menu, select Apps Script. A new tab will open in your browser.
- Understand the editor interface. You will see a code editor on the left and a service documentation panel on the right. The default file name is usually
Code.gs. This is where you write your JavaScript code. - Write a simple script to log a message. Click inside the code editor. Delete any code that is there. Paste the following line of code into the editor:
Logger.log("Hello from Google Sheets!");
- Save your project. Click the floppy disk icon in the toolbar (or press Ctrl + S / Cmd + S). Google will prompt you to name your script project. Enter a name like My First Script and click OK.
- Run the script. Click the Run button in the toolbar. Google will ask for permission to run scripts. Click the Continue button, select your Google account, and then click Allow. This is a security feature; the script only runs with your permission.
- View the results. You will see a blue progress bar. Once completed, open the Logs tab in the Apps Script editor. You will see your message: Hello from Google Sheets!. This is how scripts communicate information back to you.
- Write a script to manipulate data. Let's do something practical. Assume your sheet has a column of numbers in cells
A1:A10. We want to multiply each number by 2. Go back to theCode.gseditor and replace the existing code with this function:
function doubleNumbers() {
var sheet = SpreadsheetApp.getActiveSheet();
var range = sheet.getRange("A1:A10");
var values = range.getValues();
for (var i = 0; i < values.length; i++) {
values[i][0] = values[i][0] * 2;
}
range.setValues(values);
}
- Save and run again. Click the Save icon. Then, click the Run button again. You will be prompted to authorize the script again because we are now interacting with the spreadsheet's data. Click Continue and Allow.
- Check your spreadsheet. Go back to your active Google Sheet. You will see that the numbers in column A have been doubled. The script successfully retrieved the values, modified them in memory, and wrote them back to the sheet.
- Create a custom function. Google Sheets allows you to write your own functions that look like native formulas (e.g.,
=MYFUNCTION(A1)). In the editor, add this new function at the bottom ofCode.gs:
function addDays(date, daysToAdd) {
return Utilities.formatDate(new Date(date), "America/New_York", "MM/dd/yyyy");
}
- Save the project. Click the save icon.
- Use the function in the sheet. Go to your spreadsheet. In any cell, type
=addDays(TODAY(), 5)and press Enter. The cell will display the date 5 days from today, formatted as MM/dd/yyyy. This demonstrates how to extend the capabilities of Google Sheets.
Common Mistakes
- Forgetting to authorize permissions. The first time you run a script, you must authorize it. If you skip this step, the script will fail with an error. You must click Allow in the authorization window for the script to have the permissions it needs.
- Not saving the project. If you don't save your code before running it, your changes will be lost. Always save your work before clicking the Run button.
- Using the wrong range. In the script, ensure the range string (e.g.,
"A1:A10") matches the actual location of your data in the sheet. If the range is empty or contains different data types, the script may fail or produce unexpected results. - Error handling. If your script encounters an error (like a typo in a variable name), the execution will stop immediately. It is good practice to read the error message in the Logs tab to understand what went wrong.
Tips
- Use comments. Add comments to your code to explain what it does. This makes it easier to read and debug later. Add a comment by typing two forward slashes (
//) before a line of code. - Use the documentation. The Apps Script editor has a built-in documentation panel. You can search for specific functions, like
SpreadsheetApporgetRange, to see what they do and what parameters they accept. - Test incrementally. Don't try to write a complex script all at once. Write a small part of it, save it, run it, and check the result. This makes it easier to find and fix mistakes.
- Understand the data structure. Remember that spreadsheet data is read as arrays of arrays. A single row of data is an array of values. A column of data is a 2D array with one column and multiple rows. The script above uses this structure to loop through the data.
Troubleshooting
If your script does not work as expected, follow these steps to debug it.
First, check the Logs tab in the Apps Script editor. The logs will show you the exact line of code that caused the error and the error message. This is the most important tool for fixing scripts.
If the script runs but does not change your sheet, verify that the range you specified in the code matches the data in your sheet. Also, ensure that the cells you are writing to are not protected by a sheet or range protection.
If you see an error saying that a function is not defined (e.g., SpreadsheetApp is not defined), you may have removed the necessary library or added the code to a different file than Code.gs. Make sure your code is in the main script file.
If you are unable to authorize the script, ensure you are logged into the correct Google account in your browser. The authorization prompt will only show the account you are currently signed in with.
Frequently Asked Questions
Can I share my script with others?
Yes, you can share your script project with specific people. Go to the Share button in the editor and enter their email addresses. However, they will still need to grant permission for the script to run on their own sheets.
Is Google Apps Script free?
Yes, Google Apps Script is a free service for most use cases. There are some limits on the execution time and the number of users who can run the script, but these limits are high for personal and small business use.
Can I use JavaScript libraries in my script?
You can use some JavaScript libraries, but you must add them to your project as libraries. Go to the Resources menu and select Libraries. You will need to find the unique ID provided by the library author and add it to your project.
Does the script run on mobile devices?
The script editor can be accessed on mobile devices, but it is primarily designed for desktop use. The interface is smaller, and some advanced features may not be available. However, you can create and run scripts on mobile, and the results will appear in your spreadsheet.
What happens if my script takes too long to run?
Google Apps Script has a time limit of 6 minutes for most scripts. If your script takes longer than 6 minutes, it will be stopped. To fix this, you need to optimize your script to reduce the number of operations it performs.