Last active
July 19, 2022 01:51
-
-
Save meetpahul/a5881b9859c3a49b282e6931935d1a5f to your computer and use it in GitHub Desktop.
Use this code to add date in a specified column when a certain cell/range is edited.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Use this script to add date in a specific column when a spcecific range/cell is edited in the same row. | |
| // To run this script, you must also add an event-based trigger to be run on edit. | |
| function addDate(e) { | |
| // Open the Google Sheet file | |
| var ss = SpreadsheetApp.getActiveSpreadsheet(); | |
| // Open the active sheet where you want to add the date | |
| var sheet = ss.getActiveSheet(); | |
| // Selects the column where you are editing a cell | |
| var activeCellColumn = sheet.getActiveRange().getColumn(); | |
| // Selectes the row where you are editing a cell | |
| var activeCellRow = sheet.getActiveRange().getRow(); | |
| // try and catch will make sure your function runs without stopping even if there is any error | |
| try { | |
| // Check the conditions you want. Here, replace sheetName with the name of the sheet where you want to add date. sheetName should be a string - in inverted commas. | |
| // Also change the columNnumber1, ColumnNumber2, etc based on which you want to add date. This number should not be in inverted commas. For example, if you want to add date if you edited column C, column number will be 3. | |
| // You can also add conditions. && means AND and || means or. | |
| if (sheet.getSheetName()=="sheetName" && activeCellColumn==columnNumber1 || activeCellColumn==columnNumber2){ | |
| // Get today's date and time; you can change the format and timezone if needed. | |
| var date = Utilities.formatDate(new Date(), "GMT+5.30", "dd/MM/yyyy") | |
| // Here change the datecolumnNumber. Replace it with the number of column where you want to add date. The code below will add date in that column of active row. | |
| sheet.getRange(activeCellRow, dateColumnNumber).setValue(new Date()) | |
| } else {} | |
| } catch {Logger.log("error writing date")} | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment