You can replace dates in Google Sheets using a Google Apps Script that runs when the spreadsheet opens or on demand. This method is necessary when standard Find and Replace is too slow for thousands of rows or when you need to apply complex logic, like changing "2023-01-01" to the first Monday of that month. You will need access to the Google Apps Script editor and a basic understanding of JavaScript syntax to write the code.
What You'll Need
- A Google account with access to Google Sheets.
- A spreadsheet containing the data where dates need to be changed.
- A browser (Chrome, Edge, Safari, Firefox) to access the Google Apps Script editor.
- A basic understanding of how to copy and paste code into a script editor.
Step-by-Step
- Open your spreadsheet. Navigate to the Google Sheets file where you want to replace dates.
- Open the Apps Script editor. In the top menu, go to Extensions > Apps Script. This will open a new tab or a side panel with the script editor.
- Create a new script file. If a file named Code.js is not already open, click the "+" icon or "New Script" in the left sidebar. Name it appropriately, such as DateReplacer.js.
- Paste the replacement function. Delete any existing code in the editor and paste the following function. This function looks at the active sheet and iterates through every cell in the used range. If a cell contains a date string in the format YYYY-MM-DD, it replaces it with the corresponding month and year.
- Save the script. Click the floppy disk icon in the toolbar labeled Save. The project should now have a version number.
- Run the function. Click the green "Run" button in the toolbar. Google will ask for permission to run the script. Click Review Permissions. If this is the first time running the script, you will see a message that the script hasn't been verified. Click Advanced, then go to Go to (Script Name) (unsafe). Finally, click Allow to grant the script access to your active spreadsheet.
- Verify the changes. Return to your Google Sheet. The dates in the cells that were processed should now be in the MM/YYYY format. You can manually adjust the script code if you need a different format, such as YYYY-MM or MM-DD-YYYY.
- Optionally, set up a trigger. To have the script run automatically every time the spreadsheet is opened, go back to the Apps Script editor. Click the clock icon labeled Triggers in the left sidebar. Click the blue + Add Trigger button in the bottom right corner. Configure the settings as follows:
- Choose which function to run: replaceDatesInSheet
- Choose which deployment should run: Head
- Select event source: From spreadsheet
- Select event type: On open
// Function to replace dates
function replaceDatesInSheet() {
var sheet = SpreadsheetApp.getActiveSheet();
var range = sheet.getDataRange();
var values = range.getValues();
// Loop through each row and column
for (var i = 0; i < values.length; i++) {
for (var j = 0; j < values[i].length; j++) {
var cellValue = values[i][j];
// Check if the cell is a date type or contains a date string
if (cellValue instanceof Date) {
// Get the month and year
var month = cellValue.getMonth() + 1;
var year = cellValue.getFullYear();
// Create the replacement string
var replacement = month + "/" + year;
// Update the cell
values[i][j] = replacement;
}
}
}
// Write the updated values back to the sheet
range.setValues(values);
}
Common Mistakes
One of the most common mistakes when working with Google Apps Script is assuming that the cell contains a string. In the example code above, we check for the Date type using instanceof Date. If your cells contain text that looks like a date, such as "Jan 1, 2023", the script will skip them. You must ensure your data is either formatted as a real date or that the script logic accounts for text strings. Another common error is not saving the script before running it. If you don't hit the Save button, your changes to the code will not be reflected when you click Run.
Tips
Always test your script on a small sample of data first. Instead of running it on the entire used range of a massive spreadsheet, select a smaller range of cells and modify the code to target only those cells. This allows you to verify the output format and logic before applying it to your entire dataset. If you need to run the script on multiple sheets in the same workbook, you can modify the code to loop through SpreadsheetApp.getActiveSpreadsheet().getSheets() instead of using getActiveSheet().
Troubleshooting
If the script runs but does not change any dates, check the data type of the cells. Open a cell that should have been changed and look at the format in the toolbar. If it is formatted as Text, the instanceof Date check will fail. You can change the cell format to Date in the toolbar, or modify the script to include a regex check for date patterns. If you receive an error saying you do not have permission to run the script, ensure you have clicked Review Permissions and selected Advanced > Go to (Script Name) (unsafe). If the Allow button does not appear, clear your browser cookies and cache for the domain, then try running the script again. Finally, if the script is too slow, try limiting the range of cells it processes. Processing tens of thousands of cells at once can cause the script to time out, especially on older computers or slow internet connections.
Frequently Asked Questions
How do I change dates from one format to another, like MM/DD/YYYY to DD/MM/YYYY?
You can use the JavaScript Date object methods getDate() and getMonth() to extract the day and month. Then, construct a new string using the desired order and the current year from getFullYear(). The script examples provided above demonstrate how to extract and recombine these values.
Can I replace dates only in specific columns?
Yes, you can modify the nested loops to check the column index (j) before processing the cell. For example, add an if statement like if (j === 0) { // process cell } to target only the first column.
Why does the script run but the values don't change?
This usually happens if the cell format is set to "Text" instead of a Date format. The script checks for the Date type, which is different from a string. Ensure your data is in a date format supported by Google Sheets.
Is it safe to run a script every time I open the sheet?
It is generally safe, provided you only run scripts that you have written or that you have verified come from a trusted source. Google displays a warning when you first run a script, allowing you to review the permissions it requests.
Can I use this script to replace dates in a CSV file I downloaded?
No, because CSV files are downloaded as text files, not interactive Google Sheets documents. This script only works within the Google Sheets web application or the Google Sheets desktop app, which interact with the Google Drive API.