Reputation: 289
I have this two script if the same structure, the only different is the following:
Create Date: Insert Date only if the column 1 (A) is modified.
Last Update: Insert Date only if somone modify Column 5 or 6.
I think is simple adding some else or null but I don't have the expertice to do it... If anyone can help I will appreciate soo much.
function CreationDate(event){
//Script Creation Date Timming
var actSht = event.source.getActiveSheet();
var activeCell = actSht.getActiveCell(); //Detec the ActiveCell
var column = activeCell.getColumn(); // Detect the Column of the ActiveCell
var colNums = [1]; //Coulmns, whose edit is considered
if(colNums.indexOf(column) == -1) return; //If column other than considered then return
var row = activeCell.getRow(); //Detect the ActiveRow
if(row < 2) return; //If header row then return
var date = Utilities.formatDate(new Date(), "GMT-3", "dd/MM/yyyy HH:mm"); // Function Date + Format
var r1 = event.source.getActiveRange().getRow();
//Note: Insert the Date in Create Date Column
actSht.getRange(r1, 7).setValue(date)
}
function LastUpdate(e){
//Script LastUpdate Timming
var actSht = e.source.getActiveSheet();
var activeCell = actSht.getActiveCell(); //Detec the ActiveCell
var column = activeCell.getColumn(); // Detect the Column of the ActiveCell
var colNums = [5,6]; //Coulmns, whose edit is considered
if(colNums.indexOf(column) == -1) return; //If column other than considered then return
var row = activeCell.getRow(); //Detect the ActiveRow
if(row < 2) return; //If header row then return
var date = Utilities.formatDate(new Date(), "GMT-3", "dd/MM/yyyy HH:mm"); // Function Date + Format
var r2 = e.source.getActiveRange().getRow();
//Note: Insert the Date in the Column 8
actSht.getRange(r2, 8).setValue(date);
}
Upvotes: 0
Views: 116
Reputation: 46802
Without paying a real attention to what your script is actually doing, here is a "literal translation/combination" of both scripts. Note that I inverted the conditions to avoid "returning" from the script and so be able to go to the second condition.
function combined_function(event){
var actSht = event.source.getActiveSheet();
var activeCell = actSht.getActiveCell(); //Detec the ActiveCell
var column = activeCell.getColumn(); // Detect the Column of the ActiveCell
var colNums = [1]; //Coulmns, whose edit is considered
if(colNums.indexOf(column) > -1) { //If column is the one we want then execute
var row = activeCell.getRow(); //Detect the ActiveRow
if(row >= 2){
var date = Utilities.formatDate(new Date(), "GMT-3", "dd/MM/yyyy HH:mm"); // Function Date + Format
var r1 = event.source.getActiveRange().getRow();
//Note: Insert the Date in Create Date Column
actSht.getRange(r1, 7).setValue(date)
}
};// end of first original function
colNums = [5,6]; //Coulmns, whose edit is considered
if(colNums.indexOf(column) > -1){ // same comment, condition inverted
var row = activeCell.getRow(); //Detect the ActiveRow
if(row >= 2){
var date = Utilities.formatDate(new Date(), "GMT-3", "dd/MM/yyyy HH:mm"); // Function Date + Format
var r2 = e.source.getActiveRange().getRow();
//Note: Insert the Date in the Column 8
actSht.getRange(r2, 8).setValue(date);
}
}
}
Upvotes: 1
Reputation: 756
These two functions are exactly the same with two exceptions:
A single function could easily do both:
function setTimeStamp(e){
// no magic numbers!
var createdColNums = [1]; //Columns whose edit is considered a new row
var updatedColNums = [5,6]; //Columns whose edit is considered an updated row
var createdStampColumn = 7;
var updatedStampColumn = 8;
var headerRow = 1;
var actSht = e.source.getActiveSheet();
var activeCell = actSht.getActiveCell(); //Detect the ActiveCell
var row = activeCell.getRow(); //Detect the active row
if(row == headerRow)
return; //If header row then return
var column = activeCell.getColumn(); // Detect the active column
var date = Utilities.formatDate(new Date(), "GMT-3", "dd/MM/yyyy HH:mm");
var columnToStamp;
if(createdColNums.indexOf(column) > -1)
columnToStamp = createdStampColumn;
else if(updatedColNums.indexOf(column) > -1)
columnToStamp = updatedStampColumn;
actSht.getRange(row, columnToStamp).setValue(date);
}
Note: if you name the function onEdit() and place it in a spreadsheet's script, it will automatically be called as a trigger, and it will not need to be authorized. In that case use
SpreadsheetApp.getActiveSpreadsheet().getActiveSheet()
in place of
e.source.getActiveSheet()
All that said, imho a better methodology would be to check for a value where the created time stamp goes, and if a value exists set the updated time stamp instead. Then you'd get correct output any time any column was edited:
function onEdit() {
// no magic numbers!
var createdStampColumn = 7;
var updatedStampColumn = 8;
var headerRow = 1;
var actSht = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var activeCell = actSht.getActiveCell(); //Detect the ActiveCell
var row = activeCell.getRow(); //Detect the active row
if(row == headerRow)
return; //If header row then return
var column = activeCell.getColumn(); // Detect the active column
var date = Utilities.formatDate(new Date(), "GMT-3", "dd/MM/yyyy HH:mm");
var columnToStamp;
if (actSht.getRange(row, createdStampColumn).getValue() == "")
columnToStamp = createdStampColumn;
else
columnToStamp = updatedStampColumn;
actSht.getRange(row, columnToStamp).setValue(date);
}
Upvotes: 1