I'm looking for a script for google sheet that automatically inserts a timestamp for each row. and the date and time will be updated every time any of the rows gets edited. Timestamp will be put on column 1 and the rows can be the whole rows after the row 1, no specific number of rows. Please help I am very new at this. Need it badly. Thank you
You can use an onEdit trigger from Google Apps Script.
To do that, open a script bound to your spreadsheet by clicking Tools > Script editor, copy the following code and save the project:
function onEdit(e) {
var row = e.range.getRow();
if (row > 1) { // Check edited row is not first one
var formattedDate = Utilities.formatDate(new Date(), e.source.getSpreadsheetTimeZone(), "yyyy-MM-dd'T'HH:mm:ss'Z'"); // Format you want the timestamp, edit accordingly
e.range.getSheet().getRange(row, 1).setValue(formattedDate);
}
}
Reference:
Date
onEdit
Utilities.formatDate(date, timeZone, format)
Related
Im new to Google Sheets Scripts and I was trying to create a code to add Time to cell in an specific column using a button. I got the code down as shown below but I want the .getLastRow function to only apply to one especific column, not to find the very last row in the entire doc.
Any help?
var ss = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
function PreOpIN() {
ss.getRange(ss.getLastRow() + 1,9).setValue(new Date())
}
I have been able to write my code based on answers to other questions on here however, I am stuck. I am new to coding and teaching myself as I go. I have a spreadsheet that is filled when a form is submitted. There is a date that populates into column B (date of incident). I want to add 60 days to that date for a due date. I have been able to add the 60 days but for only one cell.
I have added a menu in the Ui that has a button to run the function and it works but for that cell. I want to be able to be on the cell that I want the due date added to and when I select it in the Ui have it fill in the date. I am guessing it has something to do with .getActiveCell but I am stuck.
function setDuedate()
{
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('2020');
var dateOfincident = new Date(sheet.getRange('B3').getValue()).getTime();
var dayInMs = 24*60*60*1000
var due = dateOfincident + (60*dayInMs)
sheet.getRange('L3').setValue(new Date(due));
I am writing a simple script that basically reads a value and send it to another sheet upon clicking a button. For testing purpose I wrote:
SpreadsheetApp.flush()
var sheet = localSheet.getSheetByName('Input');
var dateTimeRange = sheet.getRange(2,2);
var dateTimeValue = dateTimeRange.getDisplayValue();
localSheet.toast(readingValue, dataTypeValue)
In my spreadsheet setting, I set:
File->Spreadsheet settings->Calculation->On change and every minute
Cell in range 2,2 has
=now()
So, every minute, the value in cell 2,2 recalculates itself.
But when I run the script, the output value that I see is always the value of the last time I modified the sheet. As long as I don't interact with the sheet, the script is unable to read the new value. I tried adding flush(), doesn't work. Anyone have any idea?
Thank you.
I think that the reason of your issue is that the sheet is not updated. In your case, unfortunately, flush() cannot be used. In order to avoid this issue, how about the following workarounds? Please think of this as just one of several answers.
In your situation, when the sheet is edited, the value retrieving by getDisplayValue() is updated. In this answer, this is used.
Workaround 1:
In this workaround, the cell "B2" is overwritten by =NOW().
Modified script:
var localSheet = SpreadsheetApp.getActiveSpreadsheet();
var sheet = localSheet.getSheetByName('Input');
var dateTimeRange = sheet.getRange(2, 2);
var dateTimeValue = dateTimeRange.setFormula("=NOW()").getDisplayValue(); // Modified
localSheet.toast(dateTimeValue);
Workaround 2:
In this workaround, an empty cell is cleared. At sample script, the cell "B3" supposes the empty and this cell is not related to the main data. This cell is cleared. By this, let Spreadsheet know the sheet is edited.
Modified script:
var localSheet = SpreadsheetApp.getActiveSpreadsheet();
var sheet = localSheet.getSheetByName('Input');
var dateTimeRange = sheet.getRange(2, 2);
sheet.getRange("B3").clear(); // Added
var dateTimeValue = dateTimeRange.getDisplayValue();
localSheet.toast(dateTimeValue);
Value of "B2" on the sheet "Input" is updated even if the edited cell is different from the main sheet. So you can also use localSheet.getSheetByName("Sheet1").getRange("A1").clear() instead of sheet.getRange("B3").clear().
Note:
It seems that your script might not be the latest one.
Is localSheet var localSheet = SpreadsheetApp.getActiveSpreadsheet()?
readingValue and dataTypeValue are not declared. In this answer, dateTimeValue is used.
In above workarounds, the value is updated. But the retrieved value shows the value of 4 or 5 seconds faster every time. About this, I couldn't find to do the same value with "B2". I apologize for this situation.
If this was not useful for your situation, I apologize.
I am trying to create a code to Google Spreadsheets . In VBA code it is very simple. My goal is, when a particular cell is filled , another cell displays the date and time of completion .
In VBA:
Function DateTime()
DateTime = NOW
End Function
Based on your most recent comment stating that you want to call a custom function from the Google Sheet frontend, all you will need in your Script Editor (backend) is:
function timeStamp() {
return new Date();
}
You can then call that function from a formula in your Sheet, very similar to what you wrote already:
=IF( ISBLANK(A1), "", timeStamp() )
This is the code that will help you out of what you are trying to achieve.
Code below will add the date in the very next column of whichever field you will input a value into.
//onEdit is the default "on edit event handler" provided by google app script
function onEdit(e){
var spreadSheet = e.source;
var sheet = spreadSheet.getActiveSheet();
var range = e.range;
//only add the date in the next column if value in cell is not empty
if(range.getValue() != ""){
var col = range.getColumn();
var row = range.getRow();
//get the very next column
var newRange = sheet.getRange(row, col + 1);
newRange.setValue(new Date());
}
}
Hope this will help.
NB: Please note this code will work with scripts bound to google apps only.
Modified script .. works when run manually, setvalue(date) does not work when run on time trigger. Needs another tweak ... ideas please?
function getData() {
var queryString = Math.random();
var ss = SpreadsheetApp.openByUrl('spreadsheet URL');
var cellFunction = '=IMPORTHTML("stats URL' + queryString + '","table",1)';
var timezone = "GMT+1";
var timestamp_format = "EEE, MMM d '#' HH:mm"; // Timestamp Format.
var date = Utilities.formatDate(new Date(), timezone, timestamp_format);
Logger.log(ss.getName());
SpreadsheetApp.getActiveSheet().getRange('AH1').setValue(date);
SpreadsheetApp.getActiveSheet().getRange('AF1').setValue(cellFunction);
There is no "ActiveSheet" when running on time trigger, hence it becomes undefined, use one of these methods from SpreadsheetApp to retrive the spreadsheet:
open(file)
Spreadsheet Opens the spreadsheet that corresponds to the given
openById(id)
Spreadsheet Opens the spreadsheet with the given ID.
openByUrl(url)
Spreadsheet Opens the spreadsheet with the given url.
and after it get the sheet with one of the following methods:
getSheetByName(name) Returns a sheet with the given name.
getSheets()[ indexOfTheSheet ] Returns a sheet with the given 0 based index