Writing Excel Spreadsheets Using PHP

July 24th, 2008

When using the Spreadsheet_Excel_Writer library from the PEAR repository, I came across an issue I didn’t see handled in the docs (as of this writing, I am using Spreadsheet_Excel_Writer 0.9.1 beta)

My application creates spreadsheets that contain order information. Part of each row is a list of up to 20 ISBNs and the quantities desired of each. The issue came in how to handle ISBNs that had a leading zero. When I first looked through the PEAR docs for the library, a Worksheet method, writeString, looked to be the solution. However, the end result was that while the leading zero was maintained, the cell’s format was still numeric. This resulted in the application receiving the generated xls to then drop the zero, resulting in an invalid ISBN.

Looking over the internals of the Worksheet::writeString method didn’t reveal an undocumented feature that would ensure a cell was read as text, regardless of its contents. I next looked at the Format::setNumFormat method as I knew it contained ways to format the number as currency, timestamp, fractions, etc. You could then pass this Format object as the optional fourth parameter to the Worksheet::write method.

Contained in the Format::setNumFormat docs was a link to the OpenOffice.org documentation of the Excel File Format (found here, pdf). Interested in how exactly the file was structured, I read on. What I learned that was directly applicable is that each cell contains a pointer to a format definition, or XF record, and it was this XF record where formatting was stored. From the doc, section 4.6:

All cell formatting attributes are stored in XF records…The cell records themselves contain an index into the XF record list. This way of string cell formatting saves memory and decreases the file size.

So if two cells use the same formatting, like the ISBN columns would, each cell would contain a pointer to the XF record that would tell Excel the cell was text. Seciton 4.6.1 lists the 6 groups of formatting attributes, the first of which is number format, which is then an index to a FORMAT record. Okay, we’re on to something here. Further in the pdf, in section 5.49, we see the definition of the FORMAT record. Lo and behold, the table of formats from the setNumFormat page is listed in the pdf, but we see that the PEAR listing is incomplete. Scanning the complete table in the pdf, we see index 49, type Text, format string ‘@’. Bingo.

Our code for formatting numeric data as text in a string goes a little something like this (modified from the PEAR example code):

$workbook = new Spreadsheet_Excel_Writer();
$worksheet =& $workbook->addWorksheet();
 
// We'll show dates with a three letter month and four digit year format
$text_format =& $workbook->addFormat();
$text_format->setNumFormat('@');
 
$worksheet->write(0, 0, "Without formatting");
$worksheet->write(0, 1, '0123'); // cell contains 123
 
$worksheet->write(1, 0, "With formatting");
$worksheet->write(1, 1, '0123', $text_format); // cell contains 0123

To verify, generate the xls and open it. Right click the cells to modify the format of the cell, and see that the first cell is formatted as a general number, and the second cell is formatted as text.

The meta-moral is to read the docs and follow references to get at the source material. Had I not opened the pdf, it may have been a few more time units finding the information on Google. Plus, I learned a lot more about an important file format. I can sleep easy knowing I’m that much more knowledgeable.

Don’t buy that politician

June 17th, 2008

From a mailing list:

Just like you wouldn’t buy a new car with a serious defect, nor should you vote for a politician with a serious defect and then work to fix it later. You also shouldn’t buy a car with a serious defect because the only other car on the lot is an even bigger piece of junk.

What should you do?

Keep your money and walk, and tell the dealership that you aren’t buying until they carry something that’s worth buying. That is how we get quality cars, and that’s how we’ll get quality leaders.

Think voting for a candidate is throwing your vote away because they can’t win? Vote for them anyway. Let your voice be heard.

Private Philanthropy

June 13th, 2008

So many government-sponsored social welfare programs are touted as helping the poorer and more disadvantaged in society. While noble in intent, history shows that these programs are at best inefficient. With so much bureaucracy inherent in government doings, these programs are at a disadvantage from the start. So why do they persist?

I think mostly because politicians refuse to take a stand on issues. Who wants to vote against welfare? You are immediately pegged as “elitist”, uncaring, out of touch with most of Americans. The fact is, the government cannot effectively deal with this social issues, and should not for that very reason. Some would even argue that most of the social welfare programs are un-Constitutional. Indeed, these programs are paid for by forcibly taking money from hard-working citizens and redistributing it according to what the government deems appropriate.

A problem with this approach is that a disproportionate amount of the funds go to a small segment of society. Why is my labor paying for a family in another state to live on welfare for years at a time? Why am I forced to make this payment? Who doesn’t resent being told to do something, especially if they don’t agree with it? And yet, we must comply, otherwise the IRS thugs will take your money by force. How is it moral to force someone to pay for something they don’t support, and if they refuse, to take drastic actions to ensure they pay? Indeed, it is an encroachment on personal liberty.

How much more fulfilling is it to help out of the goodness of your heart? My volunteer experiences have all been positive and encourage me to continue my efforts. What if it was compulsory? I would probably not expend the effort I do when it’s my choice. When it comes to what to do with my money, I’m even more bothered by forced support. That is why reading stories about private businesses and people reaching out into their communities, where they can see a direct effect, are so important to know about and support. That should be government’s roll in welfare. Make it easy for people to support their local communities. Incentive it. Don’t force it.

I firmly believe we have a responsibility to help our fellow man. I do not believe we should be forced by any authority other than our own to do so.

Thoughts for November

May 24th, 2008

As the primary season winds down for our two main political parties, it comes time to question within ourselves what direction we wish our country to take. With that in mind, ruminate on this quote:

A government big enough to give you everything you want is a government big enough to take from you everything you have.

The quote, often attributed to Thomas Jefferson, seems to actually have originated from Gerald Ford. Regardless, the message of the quote is clear: big government does not act in the individual’s interests, and given a chance, will take whatever it can from you.

The perception that we Americans seem to have of our government as fundamentally good is dangerous. When we view the body of history, we see that the more of life government controls, the less liberty the individual is able to express. Both political parties trumpet a decrease in personal liberty, though they would never admit it outright. Democrats hide it under themes of “saving the children” and “eliminating poverty”. Republicans hide it under themes of “family values” and “patriotism”. How are the parties doing?

“Saving the children” has led to a fear, especially among men, that helping a child in need, even if it’s your own, will be misconstrued as a sexually-motivated, dangerous activity. There also seems to be an article or editorial every month or so lamenting the lack of un-monitored exploration by children. Indeed, I had free reign of my childhood, traveling the town and surrounding wilderness alone and with siblings. Today, children are fenced in, constantly monitored, given highly structured and “safe” activities, “diagnosed” with ADD or ADHD or whatever they’re calling it today, given pills to reign in their natural exuberance. I call bullshit! This sheltered lifestyle, in my opinion, leads to what some Internet commenters call “sheeple”, people unable to question authority and assuming those in power know best.

While property values have dropped across the country and foreclosures increase, the low-income borrowers have been hit the hardest. Improper lending practices (such as no-money-down, ARMs, especially to first-time buyers) have packaged up debt and exported it worldwide. Because lines of credit have been relatively scarce in low income areas, with the decrease in standards in the lending industry, low income families gravitated to the credit with little more that rosy visions of owning a home of their own. While a noble goal in and of itself, it seems most homebuyers allowed this dream to overshadow the stark realities that came with a mortgage, particularly the adjustable rate variety. So as this housing collapse has come to light and the ramifications are just beginning to be felt, where are the Democrats? Oh yeah, allowing the “Federal” Reserve to bailout the very financial institutions that got the housing market in this mess. What about the little guy? Not a dime. With a majority in both the House and Senate, the Dems have failed to bring help to ailing homeowners.

You know what could help the economy? $3+ trillion dollars spent overseas maintaining our empire. Oh wait. With a majority in Congress, their hands on the purse of the government, the Democrats have continually caved to Bush’s demands for more funding of the occupation in Iraq. So while Obama and Hillary debate about the war, they and their Democratic cohorts continue to feed the beast. Condemning Bush’s foreign policy is easy and takes no guts; opposing funding and actually bringing the troops you claim to support home is hard and takes intestinal fortitude, something history has shown lacking in the Democratic party. Why is that? Oh yeah, 100+ members of Congress directly profit, through their investments, from the military activities in Iraq, Afghanistan, and other countries. Why actually take a meaningful stand and cease to vote for funding when it hurts their individual bottom line?

Don’t kid yourself…the top echelon of Democratic and Republican leadership all hang out at the same country clubs, are members of the same corporate boards, eat at the same high end restaurants. They are cut from the same cloth; they just think they can keep power by espousing different ways of stealing your liberty.

So how does “family values” steal liberty? Probably two of the hottest issues, abortion and gay rights, are both attacked under the guise of “family values”. Abortion, in my mind at least, is the trickier of the two. At what point does a fetus gain the right to individual liberty versus the right of the mother to determine what happens to her body? It is a contentious point and not one that is solved, as far as I can tell. Each must draw their own line, and I think at this point government cannot weigh in here with law. Leave it to the mother, her support structure, and the doctor, to determine what is right.
Gay rights is, to me, a no brainer. The ability to marry should not be restricted by law. Indeed, marriage between heterosexuals has lacked the moral fortitude that “family values” pushers claim. While the examples are numerous to the point of depressing, let’s take a high profile case: John McCain. After his first wife had an accident and was no longer desirable to him, McCain “upgraded” to a newer, younger, richer, and more influential model, Cindy. Sure, he acknowledges his misstep but does that give him the right to tell Ellen DeGeneres she can’t marry Portia de Rossi because he believes in the sanctity of marriage between a man and woman? The failings of the religious right in upholding their “values” far outweighs any perceived damage from a homosexual lifestyle. Plus, as near as I can tell, the only rationale for opposing gay rights is Biblically based. While the Bible certainly has great stories and examples of noble living, it should not, cannot, be the sole basis for law in a “free” society. I have not seen arguments that are not rooted in Biblical passages.

Under the guise of Patriotism, President Bush brought us the Patriot Act, torture, Abu Ghraib, warrentless wiretapping, PROTECT America act, and more. He illegally invaded and occupied Iraq and has, more recently, been beating the drums of conflict with Iran. He speaks to Middle Eastern countries about increasing their levels of democracy and personal freedom, and yet enacts domestic policies that curtail free speech and political dissension. In our current state, we do not represent the country envisioned by the Founders.

And exhale!

So as we journey down the road to another President, take a real look at what each candidate offers. Obama offers hope. Hope for what? That the Democrats and Republicans can work together to more efficiently to steal our personal liberties? Hilary seems all but out, unless her ace up her sleeve is a hit on Obama, which she recently alluded to in a speech about why she was still in the race. Her voting record and words show she won’t get us out of Iraq anytime soon. “Responsible withdrawal” is code for “my generals will ‘find’ intel that requires prolonging the stay for national security”. McCain wants more war (Bomb bomb bomb, bomb bomb bomb Iran, anyone?). All three want more government control of more aspects of our lives. Try to think of something you do in your day that is not affected directly by government on some level. It’s hard. And heaven forbid you rise in dissent…

Announcements

April 28th, 2008

Thought I’d go ahead and announce, mainly to myself, that I will be working through SICP. The rub…doing it in Javascript. Seems as though most other languages are covered (I know Erlang is taken) and since I am doing an increasingly large amount of Javascript, coupled with the eventual prevalence of server-side Javascript, I figured it best to start getting intimate. What I like about this task is that since SICP has been so widely covered on the web, I have many resources to aid in better understanding the material (and it is some thick material). Anyway, I’ve begun chapter one and will post the chapters, as well as excerpts I find interesting, in no pre-defined timeframe.

Oh yeah, and I’m engaged.

More Wget

April 10th, 2008

It’s hard to understate the usefulness and robust feature set that most of the GNU tools have in their arsenal. Today, I’ll make mention of one such tool, wget, and a novell use of the command.

As I go through my work, I find that sites we agree to take over have little structure. They generally were slapped together a long time ago, with little thought to organization, made with Dreamweaver or, Stallman forbid, FrontPage. I’m not judging; as long as something looks okay in the browser, a company can proclaim, “We’re on the intarwebs!” However, tracking down all of their pages to be converted into a CMS, for instance, can be time consuming. Not wanting to waste a client’s money by searching through the source for links and images, then manually reconstructing the layout of the files, I fell on my trusty GNU tool wget. (I also did not have FTP access, but I knew there were dead pages that I didn’t want to resurrect. Using wget in this case helped me retrieve only the pages that were still linked to from the main page).

Here’s a variation of the incantation of wget I used:

wget -r -A '*.htm*, *.jpg, *.png, *.gif' -l 3 http://www.example-site.com

What’s it all mean?
-r: wget should retrieve recursively
-A: takes a comma-separated list of patterns to match files to accept (use -R to reject). In this case, we want all htm, html, and most picture format files.
-l: denotes how far down the rabbit hole to venture. I started with 1, so only links from the first page were parsed and followed. I then tried 2, following links that were a level below the parent and compared the resulting structure. Trying 3, I found no difference between 3’s results and 2’s results, meaning all links had been followed and accounted for.

The result:
A directory called www.example-site.com that contains the files in their layout on the server. Now I knew which pages needed converting and which images to add to the new site.

A side note: A handy way to see the layout of your newly downloaded directory is to use the tree command.

tree www.example-site.com/

will display something like this:

www.example-site.com/
|– about.html
|– calendar.html
|– committees.html
|– contact.html
|– otherdir
| `– index.html
|– images
| |– header.gif
| |– logo.gif
| `– spacer.gif
|– index.html
|– join.html
|– news.html
|– partnerships.html
`– scoopholiday.html

Walk the Walk

April 2nd, 2008

I’ve had the pleasure of working on the technology that powers iliveinspired.com and know that the founders, Rob and Chris, are working their butts off to make this a great service. Armed with not much more than inspiration and determination, they are taking their service to the world by foot. Their first foray into marching marketing was to sign the Dalai Lama on as a content provider. Not only did the Dalai Lama receive them (read about their trip, starting here), but he embraced them and their message and agreed to work with the service.

Now these two are on a mission to sign Oprah and are generating some press about it. Pick up the story from their blog here, and then read about some of the press they’ve gotten here, here, here, and here. This is a great service, there are lots of different themes to choose from, and it’s a cheap service that can provide a lot of value. Why else would they offer the first 45 days free of charge?

What’s keeping you from living inspired today? Visit I Live Inspired and start receiving daily inspiration on your phone.

Iraq and Iran Pose Threat to False Strength of the US Dollar

March 9th, 2008

It is hard to trust politicians, isn’t it? I know I believed Colin Powell’s speech to the UN detailing Iraq’s WMD capabilities, with the mobile units and all that. That trust has gotten us involved in an unconstitutional war, one that has cost us and the Iraqi people dearly in lives, property, and money. Still, there is one politician who can be trusted, mostly because what he’s been preaching in the House has come to pass in our experience. It should be no stretch to know I’m talking about Ron Paul.

What I have come to learn is that Ron Paul is dead-on when he talks about the negative impact our foreign and monetary policies are having on our standard of living. While modern Republicans and Democrats think they represent different ideals, they are really two heads of the same beast. Both encourage a welfare state in our country; the Democrats just support a more visible version.

In an article written by Ron Paul in February of 2006, Paul lays out the last 100 years of US monetary policy, and shows, in crystal clear HD detail, how the Federal Reserve and the Federal government have given us a very false sense of prosperity. In essence, because of the government and the people of this nation as a whole, cannot live within their means, we have had to control the value of our dollar by force, either covertly overthrowing governments that threaten our dollar supremacy, or by outright war. The problem is that, militarily speaking, we are unmatched in the world in military might.

Why that’s a problem is that, rather than producing more goods and exporting them, generating wealth, we are the schoolyard bullies who, when someone questions the value of our dollar, we push them around until we get what we want. So it was with Iraq and Saddam. As the world’s reserve currency, the US dollar was the only currency used in pricing oil. In November of 2000, Saddam decided he would sell oil in Euros, challenging US dollar dominance. Before this time, there was no talk of Saddam and his threat to “national security”. Using 9/11 as a false springboard to rally support, the US military went in, and soon after Hussein’s capture, Iraq was selling oil in dollars, not Euros.

A similar situation occurred in 2001 in Venezuela when an Venezuelan ambassador mentioned the possibility of selling oil in Euros. Within that year, there was a coup attempt supported by the CIA. Fast forward to today and Iran’s plans to form an oil bourse that would sell its oil in a handful of non-dollar currencies, including the Euro. Following this announcement and subsequent execution, war drums began pounding in Washington, as the President fabricated Iran’s nuclear armament program to scare Americans into another engagement in the Middle East. Rumors abound, with one saying Bush wants us in Iran by the summer.

What is clear from reading the article is that our flawed and failed monetary policies are crumbling around us, and rather than face facts and make appropriate changes, we are resorting to violence against those that challenge our fairytale. Like punching a kid that tells you there’s no Santa, forcefully stopping countries from conducting business, particularly oil exports, in competing currencies only delays the inevitable collapse, and worse still, alienates us from the rest of the world who perceive America as a bully living in a dream world. Are we surprised at how little support across the globe we have anymore? Sure, countries roll out the red carpet for us when we visit because they fear our military, but when it comes to supporting our inane wars, their support proves scarce.

Dr. Paul’s article is much longer (probably a 15-20 minute read) and runs through a lot more of the history and implications, but it is very much worth the time to read it and contemplate the consequences of voting in McCain, Clinton, or Obama, all of whom support war to varying degrees (maybe not Iraq with Obama, but he still supports operations in Afghanistan and possibly Pakistan). Keep in mind, too, that the article was written in 2006, and how much of its foretelling has come true.

Remember, a vote for Ron Paul is a vote for true peace, not just withdrawal from Iraq.

Join the REVOLUTION!

McCain Not Fit To Lead

March 5th, 2008

It is easy to convince yourself that John McCain is able to be president. He has served in the Senate for years and is a veteran of our Armed Forces. Sounds pretty much like a textbook president. Except he’s not fit. The presidency requires more than a militaristic view of the country and world. The economy can’t be run with an iron fist; it must flow and have a life of its own, with the government protecting the economy’s ability to thrive, not force-feeding it fertilizer in the hopes it would grow faster. McCain, by his own admission, doesn’t “get” economics (for more on McCain and his economics squad, read this article from the Weekly Standard and you’ll see the people he cites as advisers have economic views that are antagonistic with the others).

The problem with not “getting it” is that national security and the economy are married issues. You can’t get one and not the other. In an interview between Nathan Gardels and Nobel laureate Joe Stiglitz (Nobel Prize winner in Economics in 2001), Mr. Stiglitz responds to this lack of knowledge on McCain’s part:

Gardels: The economic costs have now come back to undermine the whole post-9/11 security effort. When John McCain says he’s not interested in and doesn’t understand the economic aspect of things, and only knows about how to keep America safe, what does that say about his leadership capability?

Stiglitz: If he doesn’t understand the economy, he doesn’t understand security. If we had infinite resources, we might be able to have perfect security. But America, like every other country, has resource constraints. That means you need to be smart — that is, economic — about the money we spend. If you weaken the American economy, you won’t be able to find the resources you need for security. The two cannot be separated.

Sadly, this critique can be made of the two front-runners in the democratic race as well. What we need is a candidate who gets the economy and foreign policy. Hmm, Michael Scheuer knows all about bin Laden and foreign policy and he is on record as saying that only Ron Paul gets it. Dr. Paul is also the only candidate that can go toe-to-toe with Ben “I’m Alan Greenspan in a bigger man’s body” Bernanke, head of the Federal Reserve and make him squirm. So Dr. Paul gets the economy.

A candidate that gets foreign policy and the economy?!? Incredible!

And yet, because of the nanny state our country has devolved into, people seem to not question the media’s anointed Republican candidate. A man riddled in controversy (Keating 5, improper relationship with female lobbyist), lacking in moral integrity (dumping wife after she has an accident and marrying trophy wife a couple months later), embraces more war-mongering (stay in Iraq 100 years, bomb bomb bomb, bomb bomb Iran), and an ardent supporter of Bush Co.; no, John McCain is not fit to run this country.

Join the REVOLUTION!

Michael Scheuer and Ron Paul

February 22nd, 2008

There may not be a better authority on Osama Bin Laden than Michael Scheuer. Intimately involved with the tracking down of bin Laden from at least 1996 to 2004, Scheuer has also written several books dealing with the handling of the Middle East by our government over the last 40 years or so. So when this man speaks on the Middle East and bin Laden, he speaks with authority that is unrivaled by most anyone else.

With that in mind, have a listen to an interview he gave recently (video was posted February 19th, 2008). Two things, among the many, that I found enlightening: first, Scheuer makes it clear that bin Laden and al Queda do not hate us for our freedoms, liberties, etc, as Bush, Guiliani, et al, have tried to ram into the conversation. It is about our involvement in the Middle East that bin Laden fights against. Second, of all the presidential candidates, Scheuer believes Ron Paul “gets it”.

Having a girlfriend invested in the green movement, I’d be remiss to comment on another bit of Scheuer’s interview and I’ll paraphrase my understanding of his remarks. When you look at the Middle East, what is there that is of interest to the US? Oil, obviously. Hmm… umm… gee… well… Not much else! So if our government were to get serious about investing in green energy and not using it to garner support while not producing results, we’d lose our interest in the Middle East, at least from a policy perspective. So using less oil = stopping the support of terrorism!

Here’s my beef for the current crop of Bush clones: they preach Christianity, family values, and all that. Fine, all well and good. However, it seems not a week goes by that a Bush supporter in the Congress doesn’t go down in flames with a scandal. But most important of Jesus’ teachings was the Golden Rule: Do unto others as you would have them do unto you. Take a moment and think back to how mad, upset, fearful, irate, how incensed with anger you were on September 11th, 2001. Planes were high-jacked and rammed into the World Trade Center. Now imagine that level of violence occurring everyday. Now double it, and double that. You’re starting to get an approximation of what the average Iraqi goes through each day. Imagine having to cope with all of those emotions everyday. Imagine, upon waking, not knowing whether you’d live or die in some suicide blast or gun fight. A terrible world for anyone. And yet our government is subjecting a country to that everyday with their occupation.

I implore you, regardless of political affiliation, to investigate Michael Scheuer’s writings and interviews and see an expert talk about the situation. Then compare that to the view given by your favorite candidate.

More important than any other issue today is our foreign policy, including the economy. Our next president must understand what’s at stake with respect to the rest of the world, not with respect to a small part of it (namely the European Union countries).

The slim silver lining here is two-fold. Wean our economy and way of life off oil and we lose interest in the Middle East, and, support Ron Paul and bring a man into office that is knowledgeable in both foreign policy and the economy.

Join the REVOLUTION