Skip to content

Instantly share code, notes, and snippets.

@Lbatson
Last active August 29, 2015 14:04
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 Lbatson/90316d9cc3124434b595 to your computer and use it in GitHub Desktop.
Save Lbatson/90316d9cc3124434b595 to your computer and use it in GitHub Desktop.
var sheet = SpreadsheetApp.getActiveSheet(),
rows = sheet.getDataRange(),
numRows = rows.getNumRows(),
values = rows.getValues(),
startTimeCol = 3,
endTimeCol = 4,
re = ' at';
/**
* Retrieves all the rows in the active spreadsheet that contain data and converts Date/Time if necessary
*/
function fixDateTime() {
for (var i = 0; i < numRows; i++) {
var startVal = values[i][startTimeCol],
endVal = values[i][endTimeCol];
if (JSON.stringify(startVal).search(re) !== -1) {
sheet.getRange(i+1, startTimeCol+1).setValue(JSON.parse(JSON.stringify(startVal).replace(re, '')));
}
if (JSON.stringify(endVal).search(re) !== -1) {
sheet.getRange(i+1, endTimeCol+1).setValue(JSON.parse(JSON.stringify(endVal).replace(re, '')));
}
}
};
/**
* Adds a custom menu to the active spreadsheet, containing a single menu item
* for invoking the fixDateTime() function specified above.
* The onOpen() function, when defined, is automatically invoked whenever the
* spreadsheet is opened.
* For more information on using the Spreadsheet API, see
* https://developers.google.com/apps-script/service_spreadsheet
*/
function onOpen() {
fixDateTime();
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var entries = [{
name : "Fix Date/Time",
functionName : "fixDateTime"
}];
spreadsheet.addMenu("Script Center Menu", entries);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment