Home > other >  How can I manually change a cell's contents when I have a script to automatically set that cell
How can I manually change a cell's contents when I have a script to automatically set that cell

Time:07-18

I have created the below script:

function onEdit() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sh = ss.getSheetByName("Sheet1");
var cell = sh.getRange("A1:A1");
var cells = sh.getRange("B1:B1");

 if( cell.getValue() !="" ) {
 cells.setValue(new Date()); 

 }
 if( cell.getValue()==="" ) {
 cells.clearContent();
 }
}

This code can insert a new date into cell B1 when data is entered into cell A1. It can also remove the date from cell B1 when I clear the data from cell A1. But if I want to manually change the date in cell B1 when cell A1 has data entered, how can I do that?

CodePudding user response:

You try this function

function onEdit(e) {let cellDate=e.source.getActiveSheet().getRange('B1');
    if(e.range.getA1Notation()=='A1'&&e.source.getSheetName()=='Sheet1')
       {if(e.value==null) cellDate.clearContent();
        else cellDate.setValue(new Date());}
}

Note: This function checks if the changed cell is A1, then makes the change to B1. So you can completely change B1 after you have changed A1

  • Related