Skip to content

Instantly share code, notes, and snippets.

@dingram
Created November 29, 2011 17:43
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 dingram/1405675 to your computer and use it in GitHub Desktop.
Save dingram/1405675 to your computer and use it in GitHub Desktop.
Date highlight script for Google Docs
function numToCol(c) {
var r = '';
if (c > 25) {
r = String.fromCharCode('A'.charCodeAt(0) + parseInt(c / 26, 10) - 1);
}
r = r + String.fromCharCode('A'.charCodeAt(0) + parseInt(c % 26, 10));
return r;
}
function getCellForDate_(d) {
if (!d) d = new Date();
// not a weekday? highlight nothing
if (d.getDay() == 0 || d.getDay() > 5) {
return '';
}
// E = 04-Jul = 0
// F = 05-Jul = 1
// G = 06-Jul = 2
// H = 07-Jul = 3
// I = 08-Jul = 4
// J = [WEEKEND]
// K = 11-Jul = 6
// Q = 18-Jul = 12
// W = 25-Jul = 18
// ...
// so... (E+ ((days since 04-Jul) - ((days since 04-Jul) % 7))) ?
var d_ref = new Date(2011, 6, 4, 6, 0, 0);
var d_now = new Date(d.getFullYear(), d.getMonth(), d.getDate(), 8, 0, 0);
var days_since = (d_now.getTime() - d_ref.getTime()) / ( 24 * 3600 * 1000 );
var col_offset = days_since - parseInt(days_since / 7, 10);
var col = numToCol(4+col_offset);
return col+'2';
}
function highlightTodayOnSheet(sheet) {
var vals = sheet.getRange('E2:ZZ2').getValues();
var colors = sheet.getRange('E2:ZZ2').getBackgroundColors();
for (var i=0, l=vals[0].length; i<l; ++i) {
colors[0][i] = vals[0][i] ? 'white' : '#C2D1F0';
}
sheet.getRange('E2:ZZ2').setBackgroundColors(colors);
var today_cell = getCellForDate_();
if (today_cell) {
sheet.getRange(today_cell).setBackgroundColor('#ff0');
}
SpreadsheetApp.flush();
}
function highlightToday(e) {
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var sheets = spreadsheet.getSheets();
// NB: skip sheet zero, as it's the example sheet
for (var i=1, l=sheets.length; i < l; ++i) {
highlightTodayOnSheet(sheets[i]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment