Table of Contents
Use Apps Script when you want Google Sheets to take an action, such as inserting a fixed timestamp, formatting selected rows, or running a report on a schedule. Start with a copy of your spreadsheet and a single menu command. A built-in formula is simpler for values that should recalculate on their own.
Add a menu that inserts a fixed date
In a spreadsheet you can edit, open Extensions > Apps Script. Paste the following code, save the project, and reload the spreadsheet. Select an empty cell, then choose My Menu > Insert Current Date. Review any authorization prompt before running it.
function onOpen() {
SpreadsheetApp.getUi()
.createMenu('My Menu')
.addItem('Insert Current Date', 'insertCurrentDate')
.addToUi();
}
function insertCurrentDate() {
const cell = SpreadsheetApp.getActiveSheet().getCurrentCell();
if (!cell || !cell.isBlank()) {
SpreadsheetApp.getUi().alert('Select an empty cell first.');
return;
}
cell.setValue(new Date()).setNumberFormat('yyyy-mm-dd');
}
The cell holds a date value displayed as year-month-day. It won’t change the next morning. To show the current date every day, use =TODAY() instead. Check the spreadsheet’s time zone if the date seems wrong near midnight.
Choose a formula for a due-date calculation
If the due date is in A2, use =IF(A2="","",INT(A2)-TODAY()). Negative means overdue, zero means due today, and positive is the number of days left. Use a custom function for calculations that genuinely require JavaScript. A custom function returns a value to the sheet and should not be used to edit unrelated cells.
Build scheduled or cross-app tasks separately
A menu command can run an authorized task, such as copying text from an accessible Google Doc into a dedicated sheet. An installable time-driven trigger can run a job on a schedule. Use an explicit spreadsheet and destination range for unattended work instead of depending on a selected cell. Start with test data and check who owns the trigger and which permissions it requests. See Google’s Sheets scripting guide and its guide to simple triggers.
Reader Comments 0
Sign in with email or Google to join the discussion.