What is setValue in Google Sheets Apps Script?
setValue writes a single value into one cell on a Range object. Pass a string, number, boolean, or Date and Apps Script stores it in the grid. It is the simplest write operation and appears constantly in triggers, menus, and small automations that update one field at a time.
When to use it
Use setValue for single-cell updates: stamp a timestamp, flip a status flag, write a computed ID into one cell, or clear one field with an empty string.
When to skip it
Skip setValue when updating many cells. Build a 2D array and use setValues once. Repeated setValue calls in large loops are slow and can hit execution limits.
How it works
-
1
Get a Range with getRange('E5') or offset from a known row.
-
2
Call range.setValue(value) with the data you want stored.
-
3
JavaScript Date objects usually become proper date values in the cell.
-
4
Use setNumberFormat after setValue if display format matters for your team.
-
5
In onEdit, avoid writing back to e.range without checking oldValue to prevent loops.
-
6
Test on a copy because script writes do not Undo with Ctrl+Z.
Examples in Google Sheets
Last updated stamp
onEdit sets the cell in column F on the same row to new Date() when column D changes to Done.
Reset a control cell
After processing, setValue on Settings!A1 back to READY so the next menu run knows the job finished.
Write an error message
catch block calls getRange('B1').setValue('API failed at ' + new Date()) for a visible status on the Dashboard tab.
Better Sheets resources
Common mistakes
-
setValue inside a loop over thousands of rows instead of batching with setValues.
-
Writing to the trigger cell in onEdit without guards, causing infinite re-fires.
-
Passing a string when the sheet expects a number, breaking downstream SUM formulas.
-
Overwriting formula cells with setValue and deleting logic users relied on.
-
Using setValue on merged cells in ways that confuse which master cell receives the value.
Frequently asked questions
- What is the difference between setValue and setValues?
- setValue writes one cell. setValues writes a 2D array to a matching rectangular range in one call.
- Can setValue write a formula?
- Yes. Pass a string starting with = and Sheets stores it as a formula. setFormula is clearer when you only set formulas.
- How do I clear a cell?
- Call setValue('') or clearContent on the range. clearContent also removes formatting.
- Does setValue trigger onEdit?
- Yes, when a user-visible edit path runs. Script writes from the same onEdit handler can re-fire the trigger if you are not careful.
- Can setValue set a checkbox?
- Pass true or false boolean for checkbox cells formatted as checkboxes.
- Why is my date wrong?
- Timezone and Date object time can shift display. Set timezone in script settings or write a date-only value intentionally.
- Can setValue run in simple onOpen?
- Yes. Simple triggers can write cells. Restricted services still need installable triggers and authorization.
- How do I set value and note together?
- Use setValue for the cell and setNote on the range for a comment. They are separate calls.
Related Tutorials
Watch how setValue works
Embed a Number in a Website from a Google Sheet
Send a Button from Google Sheets in an Email
MAKE Your Google Sheets Add-on WORK
Curate Google Sheets Easily - Automatic Bookmarklet Maker
How do I reference a different spreadsheet in Apps Script?
10 Google Sheets I Wish Someone Would Make
Related blog posts
Guides that explain setValue in more depth.
What's Better Than Google Sheets?
Nothing is better than Google Sheets. You just need to learn more about the unlimited power of Google Sheets. Functions, Formulas, Apps Script and more.
Read post →How to Use Google Sheets as a Website Database
Did you know you can use Google Apps Script to turn a Google Sheet into a free, auto-syncing database for your website? This guide shows you exactly how to set it up. You’ll prepare your spreadsheet, write a few lines of code and publish your site. Don’t worry if you’re not a developer, we’ll walk you through it. Why Use Google Sheets as a Database? For small projects, Google Sheets offers many advantages over traditional databases: * Free hosting and storage * Easy to edit data without...
Read post →Explore Coding in Google Sheets
Did you know you can actually code in Google Sheets?
Read post →Related terms
onEdit trigger
The onEdit trigger runs a script automatically when someone changes a cell in your spreadsheet. It is one of the simplest ways to make Sheets react to user input without clicking a custom menu. You define the function in Apps Script and bind it to the onEdit event for that file.
Read guide →Apps Script triggers
Triggers tell Apps Script when to run a function: on edit, on open, on a schedule, or when a form is submitted. Simple triggers use reserved names like onEdit. Installable triggers are created in the Apps Script Triggers page and can do more, including running as a specific user or on a timer.
Read guide →Apps Script loops
Loops in Apps Script let you repeat code over rows, columns, sheets, or API pages. You read ranges into arrays with getValues(), process each item in a for or forEach loop, then write results back in one setValues() call when possible. Batch reads and writes beat cell-by-cell updates for speed and quota limits.
Read guide →Google Sheets automation
Automation means work happens without repeating the same clicks every day: imports update, emails send, rows move, and dashboards refresh. In Sheets, automation usually stacks built-in features, Apps Script, and sometimes the Sheets API or add-ons. Start with the lightest tool that still solves the job.
Read guide →Done reading about setValue?
Membership unlocks 600+ tutorials, unlimited generators, and every template. Practical lessons. Zero fluff.
Need this once
Jump to a free tool or a single tutorial for this topic.
Learning Sheets for real
Unlock the full library, generators, and templates with membership.