— tomauger.com

Archive
Tips and Tricks

Named Ranges in Excel are pretty cool. They enable things like drop-down lists in your Data Validation. You can define a series of values in a column, give that column a name, and then you can refer to that range by name instead of its coordinates (ie: A1:B5).

One of the frustration with maintaining these lists is that as soon as you add a new value, you have to go back to Formulas > Name Manager and redefine the range to include the new value. To avoid this, you can create what’s called a Dynamic Range, by using a formula instead of a hard-coded set of coordinates. This is most often handled using the OFFSET( ) function as you’ll see below. Googling “Excel dynamic range” will return tons of results that all have variants of:

=OFFSET(Sheet1!$A$1, 0, 0, COUNTA($A:$A), 1)

The Basic Formula, Explained

‘OFFSET’ will return a range based on a STARTING POSITION, the ROW OFFSET (that gets added to the starting position), the COLUMN OFFSET (added to the starting position, the HEIGHT (number of rows down) and the WIDTH (number of columns across).

STARTING POSITION is usually the top left cell of your named range

ROW OFFSET is usually 0, since we have defined the starting position of our range

COLUMN OFFSET is also usually 0, for the same reason

HEIGHT is the number of rows to include in the range (here we’re using another formula – more on that below)

WIDTH is the number of columns to include in the range (must be at least 1)

Expanding the Range Dynamically

So clearly, OFFSET by itself doesn’t do that much for us – in fact, if you’re just going to plug static number into OFFSET, there’s actually no use for it – just plug in your range directly and leave it at that. The reason you use OFFSET is because it allows you to substitute any of its parameters (arguments) with some other formula. This is where the ‘dynamic’ part comes in.

The typical dynamic range formula you’ll find on the Interwebs uses COUNT( ) (for numerical columns) or COUNTA( ) for text columns. COUNT and its text cousin COUNTA count the number of non-blank cells. If we count the number of non-blank cells in a row and use that as the HEIGHT parameter of OFFSET, we get a range that starts at the starting coordinate and continues on up to the last cell that contains a text entry. In theory.

In actuality if you read the fine print, what you’ll get is a range of cells that starts at the first cell and continues down for as many cells as were counted by the COUNTA function. If there are blank cells in the middle of your range for some reason, this number will be wrong. That’s because the non-blank cells are not counted, so your range will be shorter than it should, and the last cells in the range will be left off. So be aware of this subtlety.

Read More

For those of you that just want the straight goods, here’s how we delete everything EXCEPT the ‘backup’ directory:

find . -maxdepth 1 ! -name 'backup' ! -name '.*' | xargs rm -rf

Here’s a bit of a (simplistic) explanation:

  1. ‘find’ is the magic sauce here. We’re using it to recursively search through all the files (and directories) starting at ‘.’ (the current directory)
  2. the ‘!’ is the negation operator, which tells find that the operator that follow (-name) should actually perform a negative match (match everything that does NOT match this criteria)
  3. we also need to set up a negative match for ‘.’, ‘..’ etc, since find returns those files as well. Do note that one side effect here is that it won’t delete, for example, ‘.htacess’. You may need to modify this if you want to kill those ‘hidden’ files as well
  4. find is recursive, so even if it doesn’t attempt to delete the ‘backup’ directory, it will still traverse the ‘backup’ directory and delete all the files inside it, leaving an empty directory! To avoid this, we use -maxdepth 1 which effectively turns recursion off. We then make sure we recursively delete the files in the OTHER directories by using the -r flag on ‘rm’
  5. xargs is one way to ‘do’ something useful with the list of files and directories returned by find (find also has an -exec operator which will be able to do almost exactly the same thing, but I like xargs’ syntax better, personally). We pipe the output to xargs and then follow it with the command that we would want to run once per file/directory, in this case ‘rm -rf’

Have fun! And please backup your shit before trying this, cause one mis-step and you can trash a whole lotta stuff!

Read More

There’s probably a better way, but I do most of my svn commands from the root of the project (eg: svn update). I really hate fully qualifying a path when I want to add a new file to version control, when that file clearly has a unique name. Is it a sign of hacker laziness that I would rather type 30 extra characters of shell code rather than type 25 characters of path name?

 

find -name 'my_unique_file_name_to_add.php' | xargs svn add
Read More

Okay, this has been a holy grail for me for a while, and just came to a head today as I accidentally copied a database containing about 15 separate WordPress installations (with different table prefixes) into what was supposed to be a dedicated single WP database. Ouch.

A little while ago I blogged about how you can avoid this situation altogether by only mysqldumping selected tables based on a pattern match of their prefix. Well, guess who didn’t drink his own milk (is that even an expression)?

So there I was, with a database full of wp_ and wpclientA_ wpclientB_ zg_ etc. All I wanted was to keep the original wp_ tables and just drop the hell out of the rest of ‘em.

Read More

If you’re not familiar with the fancybox jQuery plugin, I highly recommend you check it out. Lightweight, portable and powerful it is your basic Lightbox-style gallery viewer that supports embedding of images, inline content, iframe content, AJAX derived content and even SWF files (without the need for an external library like SWFObject). Very sweet.

Recently on a Zeitguys project, we had a situation where we had a gallery of dozens of SWFs (thankfully) all the same size. We wanted to display a static graphic in place of each SWF as the page loaded, and then on click, load the appropriate SWF in place. All of this is pretty easy to do right out-of-the-fancybox, so to speak. But there was an added catch: the whole thing had to be built on progressive enchancement principles, so we needed a structure that first worked perfectly with JavaScript disabled. In other words, each anchor tag in our gallery has to have a link to a static HTML page that contains just that SWF embedded on it. Only once that structure was working, could we overlay the fancybox functionality.

The challenge is that the way fancybox (and most of its other “competitors”) work is by hijacking the content of the href attribute of the anchor tag to which fancybox is attached. This is a Beautiful Thing if you have a bunch of IMGs wrapped inside A tags. The anchor’s href points to the IMG source and BAM, if JavaScript is enabled and you’ve attached fancybox to those anchor tags, fancybox reads the href on click and generates the necessary DOM elements to display it in a nicely-formatted, centered box.

But with a gallery of “poster frame” images that have to point to legitimate HTML pages for when JavaScript (or fancybox) are unavailable, we need to roll up our sleeves.

Read More

Here’s a quickie. For those of you finger hackers out there, this one-liner will look through all available logs (including backups) and come up with a count of unique visitors. Just in case your built-in server stats aren’t working and you haven’t signed up with Google Analytics.

zcat access.log*| awk '{print $1}' | sort | uniq -c |sort -n | wc -l
Read More

Okay, I’ll admit that I’m no friend to Adobe Dreamweaver, preferring a “real” programmer’s IDE (though to be fair, I haven’t yet settled on which that’s going to be), but hey, we all work in teams and depending on the project, you may need to jump in bed with the enemy.

What Dreamweaver does (reasonably) well is a sort of version / ownership control (though it can’t handle multiple edits to a single file by multiple users). The principle: if you’re going to edit a file, you put a “lock” on it (ironically, removing the visual “lock” icon that shows that a file is NOT being used by anyone #uxfail). This is called “checking out” the file, and it becomes tagged with your name to let everyone else on the team know: “back off, work on something else.” When you’re done making changes on your local, you “check in” the file, which Puts it to the remote version and then unlocks it (ironically putting the “lock” icon back on it – good job Adobe UX team) so that anyone else can edit it (by checking it out themselves).

Great. So you work on the site for a number of hours, and have now checked out a pile of files all over the place. It’s time to check them back in and relinquish control. But there’s no “check in all my checked out files” menu item (as far as I can tell – comment if there is!). So what’s the solution?

Read More

Depending on your web hosting situation, you may find yourself having to share a database among multiple WordPress installations, or perhaps other tables that have nothing to do with WordPress whatsoever. This is usually not a significant issue for your day-to-day because WordPress allows you to prefix your WP tables to be anything you like.

The challenge comes with backup and migration, particularly if you’re doing it manually via the shell command line.

Suppose you have a whole bunch of WP installations on a staging server awaiting review, all sharing a database. Now client XYZ has approved the site and it’s time to go live. We’ll take a mysqldump of just those tables and migrate that to the new server.

mysqldump takes, in addition to the authentication (-u and -p – and possibly -h) and database arguments, can take a list of tables to be included in the export. Once you supply even one table, any table not named is ignored. The problem is, particularly if you use plugins that create their own tables, the fixed list of tables can be a moving target. Can’t we select all tables with a given prefix?

Read More

A real quickie for any Arras theme users out there. Okay, Arras is overly complicated and its actions are just plain convoluted. However, they were clever about implementing filter hooks where you need them to be.

Have you created a custom post type in WordPress and now you want to include it, along with other regular posts, in your home.php’s slideshow and featured articles list? Add this little snippet to your functions.php (or wherever you are defining your filters and actions):

add_filter('arras_slideshow_query', 'add_cpt_to_arras_queries'); 
add_filter('arras_featured_query', 'add_cpt_to_arras_queries'); 
 
function hdb_add_blog_to_arras_queries( $query_string ){ 
  // Arras gives us the query string in the &key=value format 
  parse_str( $query_string, $query_args );
  // but we need it in the Array format so we can add multiple post types 
  $query_args['post_type'] = array( 'post', OPINION_TYPE );
  // arras_render_posts accepts either format so we're good to go! 
  return $query_args; 
}

 

Read More

Okay folks. Seriously. I don’t like exposing my own stupidity, but here’s a quickie in case it can help anyone. Recently I was completely flabbergasted when, after hours (read: weeks) of developing a game, suddenly Flash (CS4) would hang whenever I did the ol’ CTRL+Enter. And it was just from adding a single line to my code: setScore(6). Nonesense!

After wasting a lot of time rebooting, checking my code, worrying about corrupted FLAs and the like, I discovered the problem was dumb programming error. The compiling was working; the EXECUTION of the SWF was actually hanging.

I had created a nearly recursive nightmare with a typical Event.ADDED gotcha.

Read More

Oh man, the title says it all. Or does it? There’s such a thing as trying too hard to Google-optimize your blog posts, I suppose.

The challenge: while Bourne shell scripting, you are executing a perl script or some other arbitrary command wherein you wish to use the backtick (`) character AS a backtick character. By default, in Bourne, the backtick executes a command.

When you put commands inside a heredoc quote (ie: do something << EOF … code goes here … EOF) apparently the entire contents of the heredoc are parsed. If it encounters a naked `, the code following that backtick will be evaled right away, not even in the context of the execution order of the heredoc. This can be problematic.  We need a way to escape that sucker so that it is NOT executed during the heredoc parse operation.

Read More

Pursuant to a previous post regarding Adobe Acrobat Reader network install, here’s the Flash Player installer that bypasses the Adobe DLM, for those of you that are as paranoid and micro-managerial as I.

Manual Installation instructions on Adobe’s website

The latest installers (for the time being – your mileage may vary depending on how long since I posted this update – Sept 4, 2011 – you’re reading this post!):
http://fpdownload.adobe.com/get/flashplayer/current/install_flash_player_ax.exe (Explorer, ActiveX version)
http://fpdownload.adobe.com/get/flashplayer/current/install_flash_player.exe

Read More