I'd like to use 4 different timestamps in a sheet based on columns titled timestamp 1 / timestamp 2. When the box is checked then the timestamp moves into the sequence column. Here is my script for the first chart. How can I make it for all four charts on that page? https://docs.google.com/spreadsheets/d/11AQMvhWbzgmLLPbdf94nW7D_Kkb1aQefrU755AOhkJo/edit?usp=sharing
function onEdit(event) {
const timezone = 'GMT-5';
const timestamp_format = 'HH:mm:ss'; // Timestamp Format.
const updateColName = 'Time Stamp 1';
const timeStampColName = 'Sequence 1';
const sheet = event.source.getSheetByName('Experiment - Training w/ Duration'); // Name of the sheet where you want to run this script.
const actRng = event.source.getActiveRange();
const editColumn = actRng.getColumn();
const index = actRng.getRowIndex();
const headers = sheet.getRange(1, 1, 1, sheet.getLastColumn()).getValues();
const dateCol = headers[0].indexOf(timeStampColName);
let updateCol = headers[0].indexOf(updateColName); updateCol += 1;
if (dateCol > -1 && index > 1 && editColumn == updateCol) { // only timestamp if 'Last Updated' header exists, but not in the header row itself!
const cell = sheet.getRange(index, dateCol + 1);
const date = Utilities.formatDate(new Date(), timezone, timestamp_format);
cell.setValue(date);
}
}
Related
I found this amazing Script. It put a timestamp based on "another Column"
Could someone help make it to clear timestamp when "another column" also cleared >> Done thankyou for idkfurw
here is the final script after update from idkfurw
function onEdit(event)
{
var timezone = "GMT+7";
var time_format = "dd-MM-yyyy"; // Timestamp Format.
var updateColName = "Pasien";
var timeStampColName = "Tgl Keluar";
var sheet = event.source.getSheetByName('Form'); //Name of the sheet where you want to run this script.
var actRng = event.source.getActiveRange();
var editColumn = actRng.getColumn();
var index = actRng.getRowIndex();
var headers = sheet.getRange(1, 1, 1, sheet.getLastColumn()).getValues();
var dateCol = headers[0].indexOf(timeStampColName);
var updateCol = headers[0].indexOf(updateColName); updateCol = updateCol+1;
if (dateCol > -1 && index > 1 && editColumn == updateCol)
{ // only timestamp if 'Last Updated' header exists, but not in the header row itself!
if (sheet.getRange(index, updateCol).isBlank()) {
sheet.getRange(index, dateCol + 1).clearContent();
}
else
var cell = sheet.getRange(index, dateCol + 1);
var date = Utilities.formatDate(new Date(), timezone, time_format);
cell.setValue(date);
}
}
if (dateCol > -1 && index > 1 && editColumn == updateCol)
{ // only timestamp if 'Last Updated' header exists, but not in the header row itself!
if (sheet.getRange(index, updateCol).isBlank()) {
sheet.getRange(index, dateCol + 1).clearContent();
}
else {
var cell = sheet.getRange(index, dateCol + 1);
var date = Utilities.formatDate(new Date(), timezone, time_format);
cell.setValue(date);
}
}
I am having 10 Sheets for making entries and I want to update Current Time and Date in Column last Modified when changes have been made in Column Cum, i was able to do so but editing Cum in any Sheets (all sheets are Identical) adds current Date and Time to Last Modified Column of All Sheets,I also tried to change name of Last Modified Column for Different Sheets still It does the Same I am Adding Script for two sheet.
First I tried using these but it does Update Column Last Modified in all sheets:
function onEdit(event)
{
var timezone = "IST";
var timestamp_format = "dd-MM-yyyy hh:mm:ss";
var updateColName = "(Cum)";
var timeStampColName = "Last Modified";
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('C.Entry - 1');
var actRng = SpreadsheetApp.getActiveSpreadsheet().getActiveRange();
var editColumn = actRng.getColumn();
var index = actRng.getRowIndex();
var headers = sheet.getRange(1, 1, 1, sheet.getLastColumn()).getValues();
var dateCol = headers[0].indexOf(timeStampColName);
var updateCol = headers[0].indexOf(updateColName); updateCol = updateCol+1;
if (dateCol > -1 && index > 2 && editColumn == updateCol) { // only timestamp if 'Last Updated' header exists, but not in the header row itself!
var cell = sheet.getRange(index, dateCol + 1);
var date = Utilities.formatDate(new Date(), timezone, timestamp_format);
cell.setValue(date);
}
}
I tried changing Column Name for Different Sheets in different sheets:
function onEdit(event)
{
var timezone = "IST";
var timestamp_format = "dd-MM-yyyy hh:mm:ss";
var updateColName = "(Cum)";
var timeStampColName = "Last Modified2";
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('C.Entry - 2');
var actRng = sheet.getActiveRange();
var editColumn = actRng.getColumn();
var index = actRng.getRowIndex();
var headers = sheet.getRange(1, 1, 1, sheet.getLastColumn()).getValues();
var dateCol = headers[0].indexOf(timeStampColName);
var updateCol = headers[0].indexOf(updateColName); updateCol = updateCol+1;
if (dateCol > -1 && index > 2 && editColumn == updateCol) { // only timestamp if 'Last Modified2' header exists, but not in the header row itself!
var cell = sheet.getRange(index, dateCol + 1);
var date = Utilities.formatDate(new Date(), timezone, timestamp_format);
cell.setValue(date);
}
}
Last Modified Column must update only if Cum Column in that Particular Sheet is updated, I am also wondering if I can Define A whole Range Instead of a Particular Column For Eg. Whenever Range A3:J3 is updated it must create new timestamp in Column Last Updated
Trying to create a trigger to add a timestamp to a column, if that row was edited at some point that day. Its for work, they feel that onEdit is too messy. I don't know either.
So, I have a trigger set up to run at 1pm every day. That part works. However, I don't know how to make the code for the timestamp to point to that triggered event, and I don't know how to write out that particular function. This is what I've got, it hits line ten and says the source is defined as null. Am I looking for the triggerUiD that it needs, or something else?
function myFunction(event) {
var timeZone = Session.getScriptTimeZone();
var timestamp_format = "MM-dd-yyy hh:mm:ss";
var updateColName = "Updated";
var timeStampColName = "Timestamp";
var source = SpreadsheetApp.getActive().getSheetByName("TestTimestamp");
var actRng = SpreadsheetApp.getActive().getActiveRange();
var editColumn = actRng.getColumn();
var index = actRng.getRowIndex();
var headers = SpreadsheetApp.getActive().getSheetByName("TestTimestamp").getRange(1, 1, 1, sheet.getLastColumn()).getValues();
var dateCol = headers[0].indexOf(timeStampColName);
var updateCol = headers[0].indexOf(updateColName); updateCol = updateCol+1;
if (dateCol > -1 && index > 1 && editColumn == updateCol) {
var cell = sheet.getRange(index, dateCol +1);
var date = Utilities.formateDate(new Date(), timezone, timestamp_format);
cell.setValue(date);
}
}
Line 10 sheet is undefined in var headers = SpreadsheetApp.getActive().getSheetByName("TestTimestamp").getRange(1, 1, 1, sheet.getLastColumn()).getValues();
You could rewrite it like this:
function myFunction(event) {
var ss=SpreadsheetApp.getActive();
var timeZone = Session.getScriptTimeZone();
var timestamp_format = "MM-dd-yyy hh:mm:ss";
var updateColName = "Updated";
var timeStampColName = "Timestamp";
var source = ss.getSheetByName("TestTimestamp");
var actRng = ss.getActiveRange();
var editColumn = actRng.getColumn();
var index = actRng.getRowIndex();
var sheet=ss.getSheetByName('TestTimestamp'');
var headers = sheet.getRange(1, 1, 1, sheet.getLastColumn()).getValues()[0];
var dateCol = headers.indexOf(timeStampColName);
var updateCol = headers.indexOf(updateColName); updateCol = updateCol+1;
if (dateCol > -1 && index > 1 && editColumn == updateCol) {
var cell = sheet.getRange(index, dateCol +1);
var date = Utilities.formateDate(new Date(), timezone, timestamp_format);
cell.setValue(date);
}
}
Trying to add an automatic timestamp to my spreadsheet. Anytime data is edited in Column "PM Status" I want an auto timestamp in column "Date Update".
function onEdit(event)
{
var timezone = "ET";
var timestamp_format = "MM-dd-yyyy"; // Timestamp Format.
var updateColName = "PM Status";
var timeStampColName = "Date Update";
var sheet = event.source.getSheetByName('All Leads'); //Name of the sheet where you want to run this script.
var actRng = event.source.getActiveRange();
var editColumn = actRng.getColumn();
var index = actRng.getRowIndex();
var headers = sheet.getRange(1, 1, 1, sheet.getLastColumn()).getValues();
var dateCol = headers[0].indexOf(timeStampColName);
var updateCol = headers[0].indexOf(updateColName); updateCol = updateCol+1;
if (dateCol > -1 && index > 1 && editColumn == updateCol) { // only timestamp if 'Last Updated' header exists, but not in the header row itself!
var cell = sheet.getRange(index, dateCol + 1);
var date = Utilities.formatDate(new Date(), timezone, timestamp_format);
cell.setValue(date);
}
}
function onEdit(event)
{
var sheet=event.source.getActiveSheet();
if(sheet.getName()=="All Leads")
{
var timezone = "ET";
var timestamp_format = "MM-dd-yyyy HH:mm:ss"; // Timestamp Format.
var updateColName = "PM Status";
var timeStampColName = "Date Update";
var actRng = event.range;
var editColumn = actRng.getColumn();
var actRow = actRng.getRow();
var headers = sheet.getRange(1, 1, 1, sheet.getLastColumn()).getValues();
var dateCol = headers[0].indexOf(timeStampColName) + 1;
var updateCol = headers[0].indexOf(updateColName) + 1;
if (dateCol > 0 && actRow > 1 && editColumn == updateCol)
{
var cell = sheet.getRange(actRow, dateCol);
var date = Utilities.formatDate(new Date(), timezone, timestamp_format);
cell.setValue(date);
}
}
}
I have a sheet in my Google spreadsheet that contains 5 cells, the first 3 contains only words while the last 2 contains time, specifically a timestamp.
cell 1 = data
cell 2 = data
cell 3 = data
cell 4 = time start
cell 5 = time ended
Now, what I want is when cell 1 is supplied with data, a timestamp will automatically appear in cell 4. And when cell 2 and cell 3 is supplied with data, a timestamp will be the new value for cell 5.
My friend give me a code, that should pasted in Script editor:
function readRows() {
var sheet = SpreadsheetApp.getActiveSheet();
var rows = sheet.getDataRange();
var numRows = rows.getNumRows();
var values = rows.getValues();
for (var i = 0; i <= numRows - 1; i++) {
var row = values[i];
Logger.log(row);
}
};
And
function onOpen() {
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var entries = [{
name : "Read Data",
functionName : "readRows"
}];
spreadsheet.addMenu("Script Center Menu", entries);
};
function timestamp() {
return new Date()
}
and this code is pasted in =IF(B6="","",timestamp(B6))cell 4 and this one =IF(D6="","",timestamp(C6&B6)) is on cell 5. in his example tracker its working. But when i copied it to mine, the output in cell 4 and cell 5 is the Date today and not the time.
can anyone help me? why does it output the date and not the time?
You can refer this tutorial, if this helps.
In the script code, change
var timestamp_format = "MM-dd-yyyy"; // Timestamp Format.
to
var timestamp_format = "MM-dd-yyyy hh:mm:ss"; // Timestamp Format.
This should probably help you.
I just came across this problem and I modified the code provided by Internet Geeks.
Their code works by updating a specified column, the timestamp is inserted in the same row in another specified column.
What I changed is that I separated the date and the time, because the timestamp is a string, not a date format. My way is useful for generating graphs.
It works by specifying the column to track for changes, and then creating an upDate and upTime columns for the date and time respectively.
function onEdit(event) {
var timezone = "GMT+1";
var date_format = "MM/dd/yyyy";
var time_format = "hh:mm";
var updateColName = "Резултат";
var DateColName = "upDate";
var TimeColName = "upTime";
var sheet = event.source.getActiveSheet(); // All sheets
// var sheet = event.source.getSheetByName('Test'); //Name of the sheet where you want to run this script.
var actRng = event.source.getActiveRange();
var editColumn = actRng.getColumn();
var index = actRng.getRowIndex();
var headers = sheet.getRange(1, 1, 1, sheet.getLastColumn()).getValues();
var dateCol = headers[0].indexOf(DateColName);
var timeCol = headers[0].indexOf(TimeColName);
var updateCol = headers[0].indexOf(updateColName);
updateCol = updateCol + 1;
if (dateCol > -1 && timeCol > -1 && index > 1 && editColumn == updateCol) {
// only timestamp if 'Last Updated' header exists, but not in the header row itself!
var cellDate = sheet.getRange(index, dateCol + 1);
var cellTime = sheet.getRange(index, timeCol + 1);
var date = Utilities.formatDate(new Date(), timezone, date_format);
var time = Utilities.formatDate(new Date(), timezone, time_format);
cellDate.setValue(date);
cellTime.setValue(time);
}
}
Hope this helps people.
Updated and simpler code
function onEdit(e) {
var sh = e.source.getActiveSheet();
var sheets = ['Sheet1']; // Which sheets to run the code.
// Columns with the data to be tracked. 1 = A, 2 = B...
var ind = [1, 2, 3].indexOf(e.range.columnStart);
// Which columns to have the timestamp, related to the data cells.
// Data in 1 (A) will have the timestamp in 4 (D)
var stampCols = [4, 5, 6]
if(sheets.indexOf(sh.getName()) == -1 || ind == -1) return;
// Insert/Update the timestamp.
var timestampCell = sh.getRange(e.range.rowStart, stampCols[ind]);
timestampCell.setValue(typeof e.value == 'object' ? null : new Date());
}
I made a slightly different version, based also on the code from Internet Geeks
In order to support multiple named sheets, and because Google Sheets Script doesn't currently support Array.prototype.includes(), I included the polyfill mentioned here
Also, in my version, the timestamp marks the date of creation of that row's cell, not the date of the last update as in the other scripts provided here.
function onEdit(event) {
var sheetNames = [
'Pounds £',
'Euros €'
]
var sheet = event.source.getActiveSheet();
if (sheetNames.includes(sheet.getName())){
var timezone = "GMT";
var dateFormat = "MM/dd/yyyy";
var updateColName = "Paid for ...";
var dateColName = "Date";
var actRng = sheet.getActiveRange();
var editColumn = actRng.getColumn();
var rowIndex = actRng.getRowIndex();
var headers = sheet.getRange(1, 1, 1, sheet.getLastColumn()).getValues();
var dateCol = headers[0].indexOf(dateColName) + 1;
var updateCol = headers[0].indexOf(updateColName) + 1;
var dateCell = sheet.getRange(rowIndex, dateCol);
if (dateCol > 0 && rowIndex > 1 && editColumn == updateCol && dateCell.isBlank())
{
dateCell.setValue(Utilities.formatDate(new Date(), timezone, dateFormat));
}
}
}
// https://stackoverflow.com/a/51774307/349169
// https://tc39.github.io/ecma262/#sec-array.prototype.includes
if (!Array.prototype.includes) {
Object.defineProperty(Array.prototype, 'includes', {
value: function(searchElement, fromIndex) {
if (this == null) {
throw new TypeError('"this" is null or not defined');
}
// 1. Let O be ? ToObject(this value).
var o = Object(this);
// 2. Let len be ? ToLength(? Get(O, "length")).
var len = o.length >>> 0;
// 3. If len is 0, return false.
if (len === 0) {
return false;
}
// 4. Let n be ? ToInteger(fromIndex).
// (If fromIndex is undefined, this step produces the value 0.)
var n = fromIndex | 0;
// 5. If n ≥ 0, then
// a. Let k be n.
// 6. Else n < 0,
// a. Let k be len + n.
// b. If k < 0, let k be 0.
var k = Math.max(n >= 0 ? n : len - Math.abs(n), 0);
function sameValueZero(x, y) {
return x === y || (typeof x === 'number' && typeof y === 'number' && isNaN(x) && isNaN(y));
}
// 7. Repeat, while k < len
while (k < len) {
// a. Let elementK be the result of ? Get(O, ! ToString(k)).
// b. If SameValueZero(searchElement, elementK) is true, return true.
if (sameValueZero(o[k], searchElement)) {
return true;
}
// c. Increase k by 1.
k++;
}
// 8. Return false
return false;
}
});
}