I'm trying to have a timestamp appear in a column whenever data is added to a sheet. I've had some success with the following script:
function onEdit(e) {
var colToWatch = 2, colToStamp = 1;
if (e.range.columnStart !== colToWatch) return;
var writeVal = e.value ? new Date() : '';
e.source.getActiveSheet()
.getRange(e.range.rowStart, colToStamp)
.setValue(writeVal);
}
My issue is, every time the text in col 2 is edited, the timestamp changes to the current time.
My hope it to have a timestamp that shows when the text was originally added (so it can be organized by that date in another sheet). Other people will have access to this sheet and may change something by accident and cause changes in the sheet organized by date.
I'm new to scripting, is it possible to have an onEdit only run the first time data is added? It seems like onChange() might be able to help me, but I haven't been able to find anything.
Basically you want to terminate if the timestamp cell is already filled.
if (e.source
.getActiveSheet()
.getRange(e.range.rowStart, colToStamp)
.getValue()) {
return;
}
Related
I don't think I explained it well, column L is where I can log what I have done - with a date and time, so say I send a message, I would log MESSAGE SENT then say I make a call I could log CALLED and any info I wanted to remember - each entry being date and time stamped
In the image you can see the comment field - all i want is that to pop up when I click on the cell and be able to add to the comment field
You can use the onSelectionChange(e) trigger and check if the selected cell(s) fall into your criteria, if so then insert comment.
https://developers.google.com/apps-script/guides/triggers
You could use this sample script to set a comment on an edited cell:
function onEdit(e){
var range = e.range;
range.setNote('Last modified: ' + new Date());
}
However this requires that you edit the content of the cell to trigger the onEdit() function. Which you can modify instead to trigger the function manually (or assign the script to an image to act as a button) like so.
function setComment() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
ss.getRange('L14:L16').setNote('Last modified: ' + new Date());
}
Reference:
https://developers.google.com/apps-script/reference/spreadsheet/range#setNote(String)
https://developers.google.com/apps-script/guides/triggers#onedite
SoI'm using Google Sheets and I have an Activecampaign integration that adds a new row when a new user subscribe. I would like to add, with the user info, the current day - so I can know when people got in my list.
I have 4 tabs. The one that I want the day is called "leadData".
I tried this code, but it's not working:
function onChange(e) {
var sheet = e.source.getSheetByName("leadData")
columnToWatch = 1,
columnToStamp = 7, //change all of these to your needs...1 is column A, 2 is column B, etc
excluded = ["General Info", "Campaigns", "Automations"]; //add names of sheets/tabs to this list. The script will not work on these sheets.
if (e.range.columnStart !== columnToWatch || !e.value || excluded.indexOf(sheet.getName()) > -1) return;
sheet.getRange(e.range.rowStart, columnToStamp)
.setValue(new Date()).setNumberFormat("MM/dd HH:mm");
}
How can I solve it?
Answer:
The source and range fields are not part of the event object for onChange triggers. You must specify the Sheet and rows you want directly.
More Information:
As per the documentation on event objects, the Google Sheets onChange() trigger is an installable trigger and does not have the source nor the range fields in the event object that it gets passed.
Code Modifications:
You need to specify the sheet directly. Change the following line
var sheet = e.source.getSheetByName("leadData");
to:
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("leadData");
and change
sheet.getRange(e.range.rowStart, columnToStamp)
.setValue(new Date()).setNumberFormat("MM/dd HH:mm");
to
sheet.getRange(sheet.getDataRange().getNumRows(), columnToStamp)
.setValue(new Date()).setNumberFormat("MM/dd HH:mm");
References:
Event Objects | Apps Script | Google Developers
Installable Triggers | Apps Script | Google Developers
I have a table in Google Sheets in the format:
A B C
Day Date inventory demand
Day2 Date2 inventory demand
etc.
Others are required to fill in inventory and demand every day. Thus, it would be helpful if they open the sheet they jump always to the current date. This could be done over HYPERLINK or code. However, as I am informed onOpen works for the editor, however not for viewers. As this is currently the case. When I open the file I jump to the current date, however people viewing and editing the file per link do not.
Could somebody please help me? Thank you.
I also do not understand, why creating a cell that jumps to the current date as an alternative does not work.
I tried various variations of
=HYPERLINK("l i n k&range=B"&MATCH("TODAY",B1:B1500,0),"Jump to today")
or
=HyperLink("LINK&range=B" &Match(Today(),B6:B,1),"JUMP to Today")
// jump to current date
function onOpen() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var range = sheet.getRange("B:B");
var values = range.getValues();
var day = 24*3600*1000;
var today = parseInt((new Date().setHours(0,0,0,0))/day);
var ssdate;
for (var i=0; i<values.length; i++) {
try {
ssdate = values[i][0].getTime()/day;
}
catch(e) {
}
if (ssdate && Math.floor(ssdate) == today) {
sheet.setActiveRange(range.offset(i,0,1,1));
break;
}
}
}
try like this:
=HYPERLINK("#gid=0&range=B"&MATCH(TODAY(); B6:B; 0)+5; "zu heute")
I found the options: Edit Triggers
To manually create an installable trigger through a dialog in the script editor, follow these steps:
From the script editor, choose Edit > Current project's triggers.
Click the link that says: No triggers set up. Click here to add one now.
Under Run, select the name of function you want to trigger.
Under Events, select either Time-driven or the Google App that the script is bound to (for example, From spreadsheet).
Select and configure the type of trigger you want to create (for example, an Hour timer that runs Every hour or an On open trigger).
Optionally, click Notifications to configure how and when you are contacted by email if your triggered function fails.
Click Save.
Google Explanation
I am new to Google Sheets, and I have a Google Sheet that I have set up to dynamically place the present date in cell A1 and the time in cell A2. The sheet is "published to the web", and "Settings/Calculation" is set to Recalculate change every minute.
That all works fine, but I want to be able to read these values from the sheet using an API call. Also works perfectly, the FIRST TIME. Unfortunately, every time I try to call it again, I get the same answer as the first time, even a day later.
I'm using:
=int(hour(now()))&":"&int(minute(now()))&" "&int(SECOND(now()))
as the formula. I should also add that it's a JSON file that I'm reading and it is updating properly on the actual sheet.
I'm sure that I am missing something. Can someone please tell me what it is?
Thanks in advance.
may be you are not reading the JSON correctly. This give me correct result every time I run it.
function myFunction(){
var url = "https://spreadsheets.google.com/feeds/cells/1TtXe1JXKsxHKUWb3bqniHkLQB0Po1fSUqsiib2yMv90/1/public/values?alt=json";
try{
var sh = SpreadsheetApp.getActive().getSheetByName("Sheet1");
var response = UrlFetchApp.fetch(url)
var str = response.getContentText();
var data = JSON.parse(response);
var entry = data.feed.entry;
sh.getRange(1, 1).setValue(entry[0].content.$t);
sh.getRange(1, 2).setValue(entry[1].content.$t);
}catch(e){
Logger.log(e);
}
}
You need to check the size of "entry" before reading it, I just wanted to show that it works.
Thanks
I wrote some scripts using the script editor to automate some google spread-sheet tasks.
I would like to do a few things after the user interacts with my users sheets, .e.g update some background sheets that start with _.
I wrote the following:
function onEdit(e){
var range = e.range;
if (e.source.getActiveSheet().getName()[0]!='_'){
//It is an user edit!
UpdateOtherHiddenTables()
}
};
My problem is that UpdateOtherHiddenTables() takes quite a while, like 2 mins, and it is triggered on any user edit so it is not ideal.
How would you make sure that after a user has interacted with a sheet, that UpdateOtherHiddenTables() is called, but not too often?
I would use Script properties in combination with a timed trigger. The on-Edit trigger only records the fact that the Spreadsheet was edited:
function recordEdit() {
var sp = PropertiesService.getScriptProperties();
sp.setProperty("edited", "yes");
}
This function needs to be run by an installable trigger, simple onEdit won't provide the authorization necessary to modify script properties.
The UpdateOtherHiddenTables function is set to run every 10 minutes, or every hour, or whatever interval you want. It checks whether a refresh is needed.
function UpdateOtherHiddenTables() {
var sp = PropertiesService.getScriptProperties();
if (sp.getProperty("edited") == "yes") {
// update stuff
sp.setProperty("edited", "no");
}
}
By the way: onEdit is only triggered by user edits. A script changing the values in a spreadsheet will not fire that trigger.
I'm using string values instead of Boolean, because Properties stringifies everything. Storing false gets you back the string "false", which is truthy...