Last active
July 29, 2017 00:04
-
-
Save mstraughan86/e8a7fe0ea7a66c0529271baf071aeb3e to your computer and use it in GitHub Desktop.
Google App Script - Google Form - Insert Default Date and Time
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function defaultDateAndTime() { | |
// This gscript didn't work for a little bit. | |
// I renamed the function. I was also messing with the triggers and which functions they were referencing. | |
// I thought I got them all matched up, but it still didn't work right. | |
// Tried one last time. Deleted all triggers. Renamed this function. Recreated Trigger. Works. | |
var sheet = SpreadsheetApp.getActiveSheet(); | |
var lastRow = sheet.getLastRow(); | |
var dateColumn = 4; // Type this in manually if you know it. | |
var timeColumn = 5; // This as well. | |
/* | |
If you for some other reason seek an alternative method to find the date and time columns, here: | |
This will add at minimum 1 second of processing time to your script. | |
var lastColumn = sheet.getLastColumn() | |
var labelRow = sheet.getRange(1, 1, 1, lastColumn); | |
for (var column = 1, row = 1; column <= lastColumn; column++) { | |
var iterator = labelRow.getCell(row, column); | |
if(iterator.getValue()=='Date'){dateColumn = iterator.getColumn()} | |
if(iterator.getValue()=='Time'){timeColumn = iterator.getColumn()} | |
} | |
*/ | |
var dateCell = sheet.getRange(lastRow, dateColumn); | |
var timeCell = sheet.getRange(lastRow, timeColumn); | |
if(dateCell.getValue() == "") { dateCell.setValue(new Date()); } | |
if(timeCell.getValue() == "") { timeCell.setValue((new Date()).toLocaleTimeString().split(' ').splice(0,2).join(' ')); } | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment