(This is a follow up question of - How to Run Script on Multiple Spreadsheet Tabs)
My question: When I'm dragging/copy-pasting the data over a range of adjacent cells(in the same column), only the first entry is producing a time-stamp output.
What I am doing with the script: I am using onEdit to help me add a timestamp in 'timestamp' column, when someone edit 'status' column (most of the time, 'status' column). And these columns are not always side by side.
For example:
Background of the spreadsheet I am working on:
What I found on stackoverflow so far:
Unfortunately I am too new to script and have no ideal how to fix my code after reading articles above.
The code I am using:
function onEdit(e) {
addTimestamp(e);
}
function addTimestamp(e){
var row = e.range.getRow();
var col = e.range.getColumn();
if(e.source.getActiveSheet().getName() === "Apple" && col === 22 && row >= 2){
var time = Utilities.formatDate(new Date(), 'Asia/Tokyo', 'yyyy/MM/dd');
e.source.getActiveSheet().getRange(row,25).setValue(time);}
if(e.source.getActiveSheet().getName() === "Banana" && col === 55 && row >= 2){
var time = Utilities.formatDate(new Date(), 'Asia/Tokyo', 'yyyy/MM/dd');
e.source.getActiveSheet().getRange(row,56).setValue(time);}
I believe your goal as follows.
time
to the one side right of the edited cell.range
of the event object. The value of range
of event object is like "range":{"columnEnd":#,"columnStart":#,"rowEnd":#,"rowStart":#}
. I thought that this might be used.When above points are reflected to your script, it becomes as follows.
Please modify your function of addTimestamp
as follows. Please copy and paste the following script and save it. When you use this script, please edit the cell and copy and paste the values to the checking column of the sheet. By this, the script is run.
function addTimestamp(e) {
var obj = {"Apple": 23, "Banana": 55}; // Please set your condition. The key and value are the sheet name and the cheking column number, respectively.
var range = e.range;
var sheet = range.getSheet();
var sheetName = sheet.getName();
var rowStart = range.rowStart;
var rowEnd = range.rowEnd;
var columnStart = range.columnStart;
var columnEnd = range.columnEnd;
if (obj[sheetName] && columnStart == columnEnd && columnStart == obj[sheetName] && rowStart >= 2) {
var time = Utilities.formatDate(new Date(), 'Asia/Tokyo', 'yyyy/MM/dd');
sheet.getRange(rowStart, obj[sheetName] + 1, rowEnd - rowStart + 1).setValue(time);
}
}
var obj = {"Apple": 23, "Banana": 55};
.