Here's how to do it:
If you want that a column name S.No too get automatically incremented, so here is the code
- Open your Google Sheet.
- Go to Extensions > Apps Script. This will open the Apps Script editor.
- Delete any code in the script editor and paste the following code:
<script type="text/javascript">
function onEdit(e) {
var sheet = e.source.getActiveSheet();
var range = e.range;
// Check if the edited cell is in column B
if (range.getColumn() == 2) {
var row = range.getRow();
var cell = sheet.getRange(row, 5); // Column E
if (range.getValue() !== '') {
// Set the current date in column E if column B is edited
cell.setValue(new Date());
} else {
// Clear the date in column E if column B is cleared
cell.clearContent();
}
}
}
<script>
<script type="text/javascript">
function onEdit(e) {
var sheet = e.source.getActiveSheet();
var range = e.range;
// Check if the edited cell is in column B
if (range.getColumn() == 2) {
var row = range.getRow();
var dateCell = sheet.getRange(row, 5); // Column E
var serialCell = sheet.getRange(row, 1); // Column A
if (range.getValue() !== '') {
// Set the current date in column E if column B is edited
dateCell.setValue(new Date());
// Set the serial number in column A if it is empty
if (serialCell.getValue() === '') {
// Find the last filled serial number and increment by 1
var lastSerialNumber = sheet.getRange(row - 1, 1).getValue();
serialCell.setValue(lastSerialNumber + 1);
}
} else {
// Clear the date in column E if column B is cleared
dateCell.clearContent();
// Clear the serial number in column A if column B is cleared
serialCell.clearContent();
}
}
}
<script>