Archive

Archive for October, 2013

Google Docs spreadsheet script: compare dates, highlight past dates, highlight weekends, and highlight today

October 30, 2013 Leave a comment
screenshot of Google spreadsheet with 3 custom highlighting functions applied

A Google spreadsheet with my 3 custom highlighting functions applied

ORIGINAL PROBLEM

I spent hours tonight struggling with this Google Apps script I wrote to serve as custom functions or advanced macros. I adapted it from this StackOverflow post. The main issue I ran into was comparing dates. Here is my research path, and hopefully, some indicator as to why it took so long:

STEPS and MISSTEPS TAKEN TO SOLVE

  • Looked for built-in function in Google spreadsheet to do conditional formatting on weekends. No weekend selector and no built-in way to conditionally format the whole row based on the contents of one cell. (time: 35 min)
  • Discovered the only way to do this was to write a Google Apps script. I’ve never heard of Google Script until today. Now I had to look up basic Google Script tutorials. (time: 1 hr 30 min)
  • Wrote and tested code to highlight weekends. Date functions worked fine. (time: 1 hr)
  • Wrote code to highlight dates older than today. Worked, but highlighted today’s date also, which is incorrect. Investigated this issue for almost 2 hours. (time: 1 hr 38 min)
  • Discovered that Google Apps script is based heavily on JavaScript. Was then able to research from a JavaScript perspective. Ok, I should be almost there right?-Wrong! The major stumbling block I ran into has to do with how JavaScript handles date comparisons. There were two main problems:
    1. The new Date() object was not comparing correctly to today’s date. The issue, I found, is well-known to many JavaScript developers: comparing two Date() objects includes the time component, down to the millisecond, so they are never going to be equivalent. (it’s issues like these that make working with libraries like jQuery and Backbone more desirable than raw JavaScript coding.)The solution to this problem was to remove the time component with toDateString(). (time: 30 min)
    2. But that still didn’t make the comparisons work. The issue had something to do with comparing two objects. The solution was given on StackOverflow as essentially converting date objects to strings before comparing. I found the way to do that was with the dot operator and the non-intuitive getTime() function. (time: 15 min)
  • There was still one more snag though. Apparently you can’t assign the getTime() component in a variable like this:
    var today_noTime = new Date(today.toDateString().getTime());
    

    because it just fails. I’m not sure of the technical reasons for this, but it is very annoying in practice.The ultimate solution was to use getTime() as a dot-method on your Date() objects in the direct comparison statement, like this:

    if (today_noTime.getTime() == date_noTime.getTime()) {
        Logger.log("****** Dates are EQUAL ******");
    }

    (time: 15 min)

CONCLUSION

It took around 5 hrs to finally find all the answers. Along the way, I struggled a bunch, learned a lot and am grateful for the experience. However, my one real frustration was that there was no one clear website saying “known issues with comparing time and date in JavaScript”–there was just no such thing. I hope that this post has explained some of these issues precisely enough that it will save you some time.

FINAL SCRIPT

Here is the final complete script:

/*
 * Creator: Eric Hepperle
 * Date:    10/29/13
 * Purpose: Google Script for Spreadsheet.
 *           - Shades rows with weekend dates in (column 2) light gray.
 *           - Shades rows for past days with dark gray.
 *           - Highlights today with yellow background.
 *
 * Notes:   Adapted from code found here:
 *           http://stackoverflow.com/questions/15730638/
 *           google-doc-spreedsheet-conditional-formatting-script
 *
 * Colors:
 *           #B8B8B8 = med gray; past dates.
 *           #E8E8E8 = light gray; weekends.
 */
function colorAll() {
    var sheet = SpreadsheetApp.getActiveSheet();
    var startRow = 6;
    var endRow = sheet.getLastRow();

    for (var r = startRow; r <= endRow; r++) {
        colorRow(r);
    }
}

function colorRow(r) {
    var sheet = SpreadsheetApp.getActiveSheet();
    var dataRange = sheet.getRange(r, 1, 1, 5);

    var data = dataRange.getValues();
    var row = data[0];

    // eric's vars
    var date = row[1]; // 2nd column
    var dayOfWeek = date.getDay();

    Logger.log("*** Logging dayOfWeek " + r + " now ***");
    Logger.log(dayOfWeek);

    // if dayOfWeek is Sat or Sun:
    if (dayOfWeek == 0 || dayOfWeek == 6) {
        if (row[0] === "") {
            dataRange.setBackgroundRGB(255, 255, 255);
        } else {
            dataRange.setBackground("#E8E8E8");
        }
    }

    var today = new Date();
    Logger.log("*** today ***");
    Logger.log(today);
    Logger.log("*** date ***");
    Logger.log(date);

    // remove time component from date for comparison
    today_noTime = new Date(today.toDateString());
    date_noTime = new Date(date.toDateString());

    /*
	Logger.log("today_noTime:");
	Logger.log(today_noTime);

	Logger.log("date_noTime:");
	Logger.log(date_noTime);

	if(today_noTime.getTime() == date_noTime.getTime()){
		Logger.log("****** Dates are EQUAL ******");
	}
    */

    // if date is older than today, mark it as such
    if (date_noTime.getTime() < today_noTime.getTime()) {
        dataRange.setBackground("#B8B8B8");
    }

    if (date_noTime.getTime() == today_noTime.getTime()) {
        dataRange.setBackground("yellow");
    }

    SpreadsheetApp.flush();
}

function onEdit(event) {
    var r = event.source.getActiveRange().getRowIndex();
    if (r >= 2) {
        colorRow(r);
    }
}
Advertisements

Hackers could Control your Home Webcam and Microphone from Afar

October 25, 2013 Leave a comment

CYBER ARMS - Computer Security

A recent surfacing of some old ransomeware malware has had some people really concerned. But viruses can be much more sinister (and creepy) than just encrypting your files and holding them ransom. What if hackers could come into the privacy of your home and use your webcam to spy on you?

Well, they can.

And they are.

We have firewalls, Anti-Virus programs and defense in depth to prevent people from getting access to our precious data. But what many don’t realize is how incredible simple it is for malware to turn on a victim’s webcam and microphone to record a person from almost anywhere in the world.

Let me give you an example.

What if a hacker sent a teen, your teen in fact, a malicious file? And what if your daughter ran it and it allowed the creep to access her webcam and watch her?

So in essence, the…

View original post 334 more words

Categories: Uncategorized

Research on Social Network Sites

October 24, 2013 Leave a comment

Americans Keeping Informed About Brands Via SocNets (Courtesy: ironpaper.com): brands-social-network-research-product-2013

Americans Keeping Informed About Brands Via SocNets (Courtesy: ironpaper.com)

socialMedia_Chart_applications

Top social networks (Courtesy: research.actiance.com)

Here is a detailed bibliography of social media/social networking resources, including academic papers:

Research on Social Network Sites.

 

10 cool ways to use Excel’s conditional formatting feature – TechRepublic

October 22, 2013 3 comments

Specifying an Order for Drawing Objects (Microsoft Excel)

October 15, 2013 Leave a comment
Categories: .Of Interest Tags:

Five Apps: Free project management alternatives – TechRepublic

October 15, 2013 Leave a comment

Quick Link: Headers and Footers in Word 2003 – Unlinking To Remove Header From Only One Page

October 11, 2013 Leave a comment

Linking and Unlinking Headers and Footers In Microsoft Word 2003:

Here are instructions on how to remove headers or footers from any single page in a multi-page Microsoft Word document.  This applies to Word 2003, but may work with other versions as well.

[Excerpt]

You can link and unlink headers and footers as long as your document contains section breaks.

Unlinking Word headers (courtesy: techforluddites.com)

Unlinking Word headers (courtesy: techforluddites.com)

  1. Insert a Next Page section break at the end of the section where you wish to start a new section.
  2. Click the Link to Previous button on the Header/Footer toolbar.
  3. Watch above the header or footer to see if the “Same as Previous” text is there, if it is click the Link to
  4. Previous button again to detach the new header or footer from the old one.

(courtesy: northlandcollege.edu)