Skip to content

Instantly share code, notes, and snippets.

@bubba-h57
Last active September 26, 2019 16:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bubba-h57/de28b74c1dba1c2ef9f1c3f26d90ee33 to your computer and use it in GitHub Desktop.
Save bubba-h57/de28b74c1dba1c2ef9f1c3f26d90ee33 to your computer and use it in GitHub Desktop.
So, you want a Google App Script function that will allow you to insert the current date time stamp, but then that date time stamp should not ever change again?
/**
* This is your `=START()` function.
* It will make a call to the `handleTimestamp_(propertyName)`
* Which does the real work.
*/
function START(){
SpreadsheetApp.getActiveRange().getA1Notation()
return handleTimestamp_('starttimestamep'.concat(SpreadsheetApp.getActiveRange().getA1Notation()))
}
/**
* This is your `=END()` function.
* It will make a call to the `handleTimestamp_(propertyName)`
* Which does the real work.
*/
function END(){
return handleTimestamp_('endtimestamep'.concat(SpreadsheetApp.getActiveRange().getA1Notation()))
}
function RESET_START(){
SpreadsheetApp.getActiveRange().getA1Notation()
PropertiesService.getScriptProperties().deleteProperty('starttimestamep'.concat(SpreadsheetApp.getActiveRange().getA1Notation()))
}
function RESET_END(){
SpreadsheetApp.getActiveRange().getA1Notation()
PropertiesService.getScriptProperties().deleteProperty('endtimestamep'.concat(SpreadsheetApp.getActiveRange().getA1Notation()))
}
/**
* This is where the real work begins. Notice first that we pass
* in the property name that we are going to work with.
*/
function handleTimestamp_(propertyName)
{
// First, we check to see if the property exists. You can think
// of properties like global variables. They will sorta be there
// as long as your spreadsheet is. Simply, behinds the scenes where
// no one can see them.
if (! PropertiesService.getScriptProperties().getProperty(propertyName)){
// Well, if we got here, the property doesn't exist.
var timestamp = new Date()
// Easy money, we set the property to the current Date/Time
PropertiesService.getScriptProperties().setProperty(propertyName, timestamp)
}
// Regardless of what happened above, we now get the property, which was either
// just created, or was created a long time ago, and return that.
return new Date(PropertiesService.getScriptProperties().getProperty(propertyName))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment