Posts tagged ‘development’

Adventures in C#: Excel automation

So I’ve been working a fair bit with C# and Excel automation, and I’ve picked up a few things from the whole experience so far - namely that the thing is really poorly put together and requires a whole lot of work to make things work nicely.

The emphasis there is on nicely. You can hack together something real quick in about 5 minutes that probably almost does what you want, and will be good enough if you just need the job done. However, the Excel COM uses about 3 different standards and each time you get to guess which one you’ll need to use.

Over the next few days [or weeks] I’m going to outline a few of the problems I encountered and how I went about fixing them:

Problem: The syntax for inserting a column and changing a column’s width is completely different

This might not seem to big of an issue until you realize that:

column.Insert(); exists, but:
column.Width = 10; or column.setWidth(10); or anything like that, does not exist.

The actual solutions to do these tasks are as follows. Note, they reference the variable oSheet is the Excel.Worksheet you’re working with. These samples assume a basic knowledge of C# and using the Excel COM.

Inserting a columm:

Excel.Range rng = (Excel.Range)oSheet.Cells[rowNumber, colNumber];
Excel.Range column = rng.EntireColumn;
column.Insert(Excel.XlInsertShiftDirection.xlShiftToRight, false);

This will insert a column to the left of row rowNumber.

Changing a column’s width:

((Excel.Range)oSheet.Columns["A", Type.Missing]).ColumnWidth = 10;

This will change column A’s width.

Notice that one method uses numeric identifiers for rows, while the other uses a string. Also, interestingly enough, its a bit of a pain to actual get a hold of a column using either method.

Facebook: No, I don’t want to take your quiz; I want random play.

It’s time for one of my favourite angry posts on the Social Utility That Could.

Facebook recently announced a new developer policy for applications publishing content to the Mini-Feed (which filter up to the global News Feed depending on quality.) The News Feed is the default page upon login, while the Mini-Feed is the area on your own profile, where people can see the recent things you’ve been up to - like changing your “Looking For” status to “Random play.” I no longer have the “random play” option myself; apparently at some point in the past, my employment or affiliation with the IBM or RIM networks triggered a flag that says that since I’ve worked for the man, I apparently no longer can openly advertise my desire to engage in non-committal, promiscuous activities.

Either that, or it read my “Male at the University of Waterloo” details, and summarily decided that my interests could not possibly contain meaningless, playful contact with the opposite sex. I’m amazed I wasn’t forced into looking for “like-minded friends who enjoy a spot of anime, ramen noodles and debating .999=1.” Instead of “random play”, I have to settle for the much less scandalous “networking” option, which my good friend Phil promptly mocked:

Networking is now an option in looking for?! Christ what’re you gonna do walk up to a chick and be like “hay can i interface w/ ur data p0rt lol if u get wut i mean kekeke”

I would not be surprised if someone at UW has actually tried this as a pickup line, although a much more likely attempt would involve LOLcats or the ORLY owl.

The long and short of this new Facebook policy is that applications can no longer report passive actions, such as users receiving new posts on their installation of SuperFunLuckyHappyWall. (Now with bonus Zwinky!) Observe:

New Post - FunWall

Rather, the new policy indicates that only actions taken by a user should be published. So if you posted a new Happycat using Internet Meme Generator 2.0, then it’d be fair game to see “Jake posted a new Happycat image macro” in the Mini-Feed. This should hopefully help clear up the state of items such as “Forty-seven (47) of your friends sent a Wet Willy to one another.” Facebook’s not naming names, but the two biggest offenders are Slide and RockYou. It should be a bit more pleasant to see things cleaned up.

As an aside, you can still find my favourite applications to block if you’re interested in improving your experience.

Eclipse PDT - one of many useful work tools

Just checked out the Eclipse SVN site and apparently the issue with dashes in new PHP filenames has been resolved. I upgraded my work copy to the latest nightly build and things seem to be going well. (The October 1st integration build might be better stability-wise, but I’ve had good luck with the nightly builds.)

For the record, after the upgrade, loading my workspace took about five minutes (Core 2 Duo @ 2.16GHz, 1GB RAM) with no discernable progress bar or CPU activity. Eclipse effectively looks like it’s frozen. Be patient; it’ll load eventually.

PDT, though, is about the single most useful tool I find for PHP development at work. The next front-runner is WinSCP, which has a really neat feature, Keep remote directory up to date:

Keep remote directory up to date

This feature lets you have complete synchronization between a remote SFTP server and your local Eclipse workspace. It also supports private key encryption.

As a pair to WinSCP, I also use PuTTY, with anywhere from one to five sessions open at a time. PuTTY offers a neat tunnelling option, which I’ve mentioned before.

You can also access internal servers using one of these tunnels; for example, if I want to connect to a remote desktop session on host 192.168.1.101:3389 on my internal network, I’d use the following syntax:

Source port: 127.0.0.2:3389
Destination: 192.168.1.101:3389

Then, after establishing the SSH connection, using the 127.0.0.2 alias in Remote Desktop lets me connect to the machine behind the NAT traversal firewall.

Notepad++ is another program I highly recommend as a replacement for Notepad. If you can’t use TextMate because you’re not on a Mac, Notepad++ is an acceptable substitute. It offers some neat syntax highlighting features, and saves session history even when you exit.

Paint.NET is also a decent image editor, allowing you to verify hex values for colours and perform some minor tweaks to images without incurring the load time and aggravations in Photoshop.

Minor site tweaks and new WordPress plugins I’m using

I’ve made a few changes to the site, specifically applying the latest upgrade to WordPress 2.3 and a few new plugins. Here’s what’s behind the engine now:

The All-In-One SEO Pack: Modifies titles and meta tags for better search engine optimization. The site was doing fairly well before, but it’d be nice to make sure all content here is accessible.

Google XML Sitemaps: Generates a sitemap XML file (that you can use with Google Webmaster Tools) from your existing WordPress structure.

Share This: Adds a fairly inobtrusive link to the bottom of posts allowing submission to social networking and news aggregation sites.

WP-Cache: Caches page contents so that your database server doesn’t get reamed as badly. I’d already been running a previous version, and the only annoying thing is that if you’re editing a theme, you have to turn it off (or delete the cached pages every time) and remember to turn it back on when done.

The theme being used here is a slightly tweaked version of Mandigo Blue, with some sidebar code modifications and added support for Google Adsense/Analytics code.

Eclipse PHP Development Tools (PDT) 1.0 released (now with bug!)

It must be a banner day for Eclipse projects, since the primary IDE I use for both work and personal projects has been released as a 1.0 version at eclipse.org.

The primary download mirror from eclipse.org is fairly slow (I’m pulling about 50-60K/s from my work connection from it, when normally I get much speedier downloads), at least for the full-sized Win32 large package. I’d suggest you use an alternate mirror - the OSU Open Source Lab mirror seems to be pulling in the ZIP file fairly well. It also hasn’t percolated to all mirrors yet, so try a different one if you’re unlucky with the first.

I’m going to check and see if the minor file naming bug in RC1 made its way out to the final release; stay tuned.

Update: Wow, we’ve got a UI and a pain-in-the-rear bug when trying to create filenames with multiple periods:

PHP IDE 1.0 New File bug

I might decide to report this to the Bugzilla site, but last time I did, apparently the description and sample code I provided wasn’t really needed.

Update 2: Apparently this will be fixed in the next integration build, but it’ll still probably have an ‘illigal’ spelling error.

Update 3: The typo should be picked up in the next nightly.

Update 4: The typo and bug has been fixed in the N20070920 build, but still doesn’t allow the creation of filenames with dashes. Might have to report this one.

Tunnelling through PuTTY to MySQL, using SQLyog

Let’s say you have a shared web hosting account on a random cheap provider, and the only way to access the MySQL server is to either connect from an SSH command line or use phpMyAdmin. While both of these options are okay, in some circumstances it’s not exactly wise to have a phpMyAdmin installation publically accessible, as it lends itself to repeated bruteforce attacks and is subject to server configuration changes. I know on my personal server, there have often been times that I’ve inadvertently left a myAdmin installation wide open for anyone to monkey with the database.

I also enjoy using an application called SQLyog, which is a Windows GUI tool for managing MySQL databases. The Community Edition of the application has proven to be more than sufficient for my random projects, and I highly suggest anyone doing database work on Windows take a look at the application.

Using some details from this article, with an updated version of PuTTY, here’s how I go about configuring my local system to connect to a remote MySQL server:

Start PuTTY:
PuTTY 01

Define your connection settings and save them:
PuTTY 02

Navigate to the Connection / SSH / Tunnels view, and provide the following settings:

Source Port: 3306
Destination: localhost:3306
(This assumes you don’t have a MySQL server running on your local machine. If you do, change localhost:3306 to localhost:freeport where freeport is the number of a free port on your workstation.)

PuTTY 03

Click the Add button, then save your connection settings. In the Forwarded Ports list, you should see L3306 localhost:3306.
Open the connection and sign in to the remote system.
PuTTY 04
Start SQLyog and define a connection to localhost:3306.
PuTTY 05
Your connection will be tunnelled through SSH to the remote server transparently. Make sure that you close SQLyog before you close the PuTTY tunnel.
PuTTY 06

Let me know how this works for your development situations - even if you have direct access to the MySQL server, you should still check out SQLyog for database development if you haven’t already tried it.

Web dev quick tip: Style your INPUT elements by type

One thing I recently found out from looking through some example CSS: when doing web development or design, you may want to apply certain attributes to one type of input field but not another. For example,

is coded:

<input type="button" value="A Button" />

The problem therein is that you may want different borders, backgrounds or sizing attributes for your buttons, text fields, radio buttons, check boxes and password fields. Internet Explorer is an especially aggravating candidate for this, since it enforces border styles around the actual radio button itself. Safari behaves differently than other browsers as well, since it likes to enforce OS X-style control attributes.

In your CSS file or <style> tag section, you can force specific elements with attributes to take certain styles, such as this example from a recent project:

input[type="text"], input[type="password"], textarea {
border: 1px solid #aaa;
background-color: #f5f5f5;
}
input[type="text"]:focus, input[type="password"]:focus, textarea:focus {
border: 1px solid #336bc3;
background-color: #ffffff;
}
input[disabled][type="text"] {
border: 1px solid #888;
background-color: #ddd;
}
input[disabled][type="text"]:hover {
border: 1px solid #888;
background-color: #ddd;
}

In this example, text fields and textarea elements will be styled with a specific background color and border, which won’t affect other elements such as buttons.

Another neat trick I’ve learned while working on this project is the ability to put action images inside <button> elements. Instead of the <input type="button" /> tag, using the HTML 4 <button> element allows even more tricks. For example:

<button type="button"><img src="images/rss.gif" alt="" /> RSS</button>

would give something like this:

What is this Facebook applications nonsense?

Over the past few days I’ve seen a plethora of really inane applications on my Facebook news feed. At this point, I primarily use the site for stalking other people, since I get any relevant status updates and private messages sent over to my cell phone.

Seriously, though, “Glitter Text”? “X Me”? These sorts of items encourage banal, MySpace-esque pages cluttered with about fifty extra boxes, causing extra page load time and bandwidth usage. If you don’t believe me about these particular items, check their comment pages out in the application directory. You’ll see the worst CAPS LOCK IS CRUISE CONTROL FOR COOL, nd txtspeak lyk dis offenders this side of NewsCorp’s abortion of a social portal.

(Keep in mind that the following screenshot is from “Honesty Box”, an application that specifically allows anonymous commenting on your profile.)

Honesty Box Comments

That’s by far not the worst of it. The “X Me” application, a utility that lets you supplement the standard “poke” action with custom text, immediately attracted people who thought adding ‘fuck’ would be just an EXCELLENT idea. Never mind the fact that your profile is public, says you’re 12 and go to middle school; “FUCK” would be just a great idea and hilarious.

Christ almighty.

People wonder why pedophiles are having an easier time of it these days.

What’s more, is when I go to block these applications from appearing, I get the message that “This will not prevent you from seeing application if other people have it installed.” What a pathetic cop-out; I don’t want your 96Kbps Fergie MP3’s even beginning to think about loading on my box.

Anyone with me on this?

Developers, developers

There’s been a lot of whinging and amateur-lawyering over the past few days (including a massive dupe on Slashdot), about a controversy surrounding an app called TestDriven.NET. As far as I understand the situation, the primary developer is currently in a whine-fest with some people from Microsoft about licensing for a Visual Studio 2005 Express add-in. Basically, you can’t extend Express without a mad UI hack, and according to Microsoft, said extensions violate Express Edition’s license.

This sort of patent, licensing or “you’re violating the product’s ethos” crap irks me in a different way. At work, I have nearly free reign to develop applications in whatever language I feel like. As long as it suits the business case, I could crank out Java, Python, VB6 - most of the existing stuff I’m updating is a combination of Visual Basic 2005 and VBA automation. I use all of the languages in VS2005, as well as some optional components (managing an SQL Server 2005 installation is somewhat interesting.)

As part of an organization, generally “getting a license” is preferred to snagging some copy of an application off BitTorrent. ;) At IBM, the software requisition process could take a week or two to get rolling, unless your manager put a “please expedite this” note along with it. With the license, you’d have to renew it after a certain period of time - although I was probably subject to this more than most people, since I’d request six months for each term, and end up needing to extend that three times over the course of my time there. Aside from that, I can recall a certain internally licensed program had to be updated with a license file nearly every two weeks.

My current experiences have been the opposite. Most software is available on the public network share, and it’s an average time of one day between my request for a program and its receipt.

The crux of all this is that if I wanted Visual Studio 2005 Team Suite, SQL Server 2005, whatever the newest Exchange Server is, and any other ridiculously expensive application (for an individual, at least) and asked for it tomorrow, I’d likely have it by Friday. So why, instead of using Windows Server 2003, IIS 6.0 with ASP.NET 2.0, and Visual Studio Expression Web Designer, would I pick up a copy of Eclipse 3.2 with the PHP Development Tools extensions and crank away on my latest project with that?

Partially because I know PHP very well and am aware of its idiosyncracies and language features.

Partially because this Microsoft nonsense has left me wondering where the hell I stand using Express editions of their products.

Partially because Eclipse actively encourages UI extensions - in fact, you can’t even really consider Eclipse an IDE first of all, because it’s more of a Java UI framework.

Formal unit testing is not something I’ve done much of in my line of work. I’ve written and executed testcases, and contributed code/XML/documentation to fix deficiencies in said testcases. But when I move into needing formal testing, I don’t want to be beholden to someone’s mad licensing whims.

I’ve said my piece, in any event.

Custom application: hostseditor

Quick editor I whipped up for a friend using C#. It edits your Windows hosts file. If you’re running on Vista it’ll probably require administrative mode.

Hosts Editor (0.0.3)

Source code to be available soon once I’ve cleaned things up a bit. This post was more for my own personal use.