Reputation: 11
I'm trying to create more than 1 timestamp but when y try to copy a function I cant because 'tf' is already in use. I get this error:
Syntax error: SyntaxError: Identifier 'tf' has already been declared line: 16 file: START TIMESTAMP.gs
This is what I have so far:
function onEdit(e) {
addTimestamp(e)
}
function addTimestamp(e) {
const activeSheet = e.source.getActiveSheet()
const modifiedCell = e.range
const tf = activeSheet.createTextFinder("Start")
tf.matchEntireCell(true)
const cellStart = tf.findNext()
const row = cellStart.getRow()
const col = cellStart.getColumn()
// guard statements
if(!activeSheet.getSheetName().toString().startsWith("TASKS")) return
if(!(modifiedCell.getColumn() === col && modifiedCell.getRow() > row)) return
if(e.value === "TRUE"){
modifiedCell.offset(0,1).setValue(new Date())
} else {
modifiedCell.offset(0,1).clearContent()
}
//SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getActiveCell().offset()
}
Upvotes: 0
Views: 36
Reputation: 64100
const is only intended to be used for variables that are set when they're declared. It's handy because it keeps you from making mistakes that you don't intend to make. If your going the change the values then use either var or let.
function onEdit(e) {
const sh = e.range.getSheet();
if(sh.getName().startsWith("TASKS")) return;
let tf = sh.createTextFinder("Start").matchEntireCell(true);
const cellStart = tf.findNext();
const row = cellStart.getRow();
const col = cellStart.getColumn();
if(!(e.range.columnStart == col && e.range.rowStart > row)) return;
if(e.value == "TRUE"){
e.range.offset(0,1).setValue(new Date())
} else {
e.range.offset(0,1).clearContent()
}
}
To add more timestamps:
function onEdit(e) {
const tsA=[{row:0,col:1}];//add more objects to get more timestamps
const sh = e.range.getSheet();
if(sh.getName().startsWith("TASKS")) return;
let tf = sh.createTextFinder("Start").matchEntireCell(true);
const cellStart = tf.findNext();
const row = cellStart.getRow();
const col = cellStart.getColumn();
if(!(e.range.columnStart == col && e.range.rowStart > row)) return;
if(e.value == "TRUE"){
let dt = new Date();
tsA.forEach(t=>{
e.range.offset(t.row,t.col).setValue(dt)
});
} else {
tsA.forEach(t=>{
e.range.offset(t.row,t.col).clearContent();
});
}
}
Upvotes: 1