Was trying to add the tweetback plugin to my thesis themed blog recently and found out that the tweets doesn’t appear automatically as comments if you just enable the plugin. Some searching around didn’t help much either. Here is a step by step account on what worked for me.

Download and Install TweetBacks Plugin
You can just search for the term “tweetback” on your WordPress plugin installer or go to Yoast

Activate and Configure TweetBacks
Activate the plugin and do the basic configuration available, including update frequency, list of exclusions, etc.

Edit comments.php
Go to Appearance > Editor in your admin area and edt comments.php.

Find the line of code below

if ($comment->comment_type == 'trackback' || $comment->comment_type == 'pingback')

And replace it with the below code

if ($comment->comment_type == 'trackback' || $comment->comment_type == 'pingback' || $comment->comment_type == 'tweetback')

You’re all set! The tweets will now appear as trackbacks.

 

One issue that web developers trying to port a “designed for IE” application (to support more browsers) is the use of certain IE-specific method calls. One such is the use of .click on anchor tags. This method call will generate an error in browsers other than IE as it is not defined.

Here is one way to solve this issue, without changing the existing code. What we have to do is see if .click is defined and if not, define it with an alternative that works in our target browser.

  window.onload = function() {
    var allLinks = document.getElementsByTagName('a');
    if(allLinks.length>0 && !allLinks[0].click)
      for(var i=0; i < allLinks.length; i++)
        allLinks[i].click = function() {
          if(this["onclick"]) this["onclick"](); else location.href = this.href;
          return false; };
  }

By putting the above code in the head section of your page, we can ensure that .click is defined. You may need to change the this["onclick"]() part based on your target browser.

Here is a sample piece of html for testing the above script. Clicking on the second link should navigate to target URL of the first link.

Target Link

Click Me!
 

Everyone says SP2010 can be installed on Win7 and when you actually try to do it – BAM!
It says setup is unable to proceed as the product requires Windows Server 2008 (x64).

Error Saying Win 7 is not Supported

Error Saying Win 7 is not Supported

Here is what came to my rescue: http://www.codeproject.com/KB/sharepoint/Install_SP2010_on_Win_7.aspx

The above issue can be solved easily by adding one extra line to your setup configuration file, usually found at C:\Program Files (x86)\MSECache\oserver2010\Files\Setup\config.xml

Edit the file and add the below line just before </config>

<Setting Id="AllowWindowsClientInstall" Value="True"/>

Now you can go ahead and run C:\Program Files (x86)\MSECache\oserver2010\splash.hta

 

Twitter has an API implementation that is very simple to understand and use. Here is how it works, if you want to get the friends timeline of a user.

http://twitter.com/statuses/friends_timeline.rss

Here there are 3 keywords.
What are you working on? statuses
What do you need from the above object? friends_timeline
In what format do you need it? RSS

Formats supported are: xml, json, rss, atom
Methods supported: See Twitter API
There are many objects supported, but the above format applies for only a few like Statuses, User, Direct Messages. The rest are also similar.

Now, let us look at a GET to bring in Friend Status.

First off, we would need the URL above

        public string GetFriendsStatus()
        {
            return InvokeAPI("friends_timeline", "statuses", "rss");
        }

        public string GetUrl(string action, string target, string format)
        {
            return String.Format("http://twitter.com/{0}/{1}.{2}", target.ToLower(), action.ToLower(), format.ToLower());
        }

        private string InvokeAPI(string action, string target, string format)
        {
            return AskTwitter(GetUrl(action,target,format), this.User);
        }

The public method GetFriendsStatus calls a generic method InvokeAPI to (surprise!) invoke the API. This function in turn relies on GetUrl to build the URL for this request. AskTwitter is a helper method that does a GET request and returns the result.

        public static string AskTwitter(string Url, NetworkCredential nwUser)
        {
            try
            {
                WebClient client = new WebClient();
                if(User!=null) client.Credentials = nwUser;
                Stream stream = client.OpenRead(Url);
                StreamReader reader = new StreamReader(stream);
                return reader.ReadToEnd();
            }
            catch (WebException ex)
            {
                if (ex.Response is HttpWebResponse)
                {
                    if ((ex.Response as HttpWebResponse).StatusCode == HttpStatusCode.NotFound)
                    {
                        return null;
                    }
                }
                throw ex;
            }
        }

As you can see, AskTwitter takes in 2 parameters. First one is the URL we built earlier. The second parameter is user credential, for requests that require credentials. This can be built easily as below.

            NetworkCredential nwUser = new NetworkCredential("UserName", "Password");

Hope that helped get you started. Am working on a complete set of wrappers that will make using these APIs much more easier. Please leave a comment and let me know if you would be interested in any specific features.

 

We had a requirement to find all images within our solution folder and check which all of them are being referenced. This was mainly to identify the unused images and cleanup the deployment.

Looked like a good place to use Windows PowerShell and this is what I came up with. From the current folder, it looks all subfolders and builds a list of files. Then, a list of images is made similarly. Then a loop runs to find if any of these images are present in file content.

$AllFiles = Get-ChildItem . -Recurse | Where-Object {!($_.PSIsContainer)}
$Images = Get-ChildItem . -Recurse | where{$_.Extension -match "gif|jpg|jpeg|png"}

foreach ($Image in $Images) {
  $count = 0
  foreach ($OneFile in $AllFiles) {
    $found = select-string -pattern $Image -path $OneFile
    if($found -ne $null) { $count++ }
  }
  Write-Host $Image : $count
  Write-Host $Image : $count >> log.txt
}

There are couple of known limitations to this script
1. The script doesn’t look at the actual path of the referenced file, just looks for file name
2. There is no filter for file-type when we get list of files (deliberately left out, please add)

 

Like COUNTIF and SUMIF, it is helpful to have a conditional MAX or MIN. Excel doesn’t have it for now (yeah, “for now”, ‘coz MS has steadily been adding such useful functions with every new release).

Well, the good news is that there is a work-around.

MAXIF
The approach is to have an array formula on the IF column returning a bool, and multiplying it with the column from which MAX is to be found.

Say, we need to find the MAX in column F based on a condition on Column A and K5 having the compare value. We can do this as below.

=MAX((A2:A30=K5)*(F2:F30))
Since it is an array formula, do a Ctrl+Shift+Enter and it’ll become {=MAX((A2:A30=K5)*(F2:F30))}. Do NOT manually add the braces.

MINIF
This is very similar to what we did in the above activity. Just replace MAX with MIN.

=MIN((A2:A30=K5)*(F2:F30))

Again, this is an array formula and need to do Ctrl+Shift+Enter.

Note that empty cells in column F might break this formula.

 

COUNTIFS is a new function available in Excel 2007. This has proved to be very helpful and it is always a problem if you switch back and forth between the two versions. Well, here is an equivalent for COUNTIFS that you can use until your company decides to spend those extra bucks and get everyone on to version 2007 :)

Let’s assume the data is as below.
countifs-example

These will be the equivalents in 2003, using SUMPRODUCT.
COUNTIFS

=SUMPRODUCT((A2:A10=E5)*(B2:B10=F5))

Where E5 has the counter and F5 has the sales-person. The same can be extended for a SUMIFS as below.

SUMIFS

=SUMPRODUCT((A2:A10=E5)*(B2:B10=F5)*(C2:C10))

Next post, we’ll talk about two functions that are not in any version of Excel yet – MAXIF and MINIF.

 

In date manipulation, one common task is to arrive at first and last day of a month. Here are two methods to achieve the same in VB and C#.

DateSerial
This is my favorite method for creating date out of numbers. The beauty is that the function accepts numbers beyond the usual range. i.e. Month = 14 would mean 2nd month of next year. Negative numbers too are accepted.

'This year, this month, first day
FirstDay = DateSerial(Today.Year, Today.Month, 1)
'This year, next month, 0th day is this month's last day
LastDay = DateSerial(Today.Year, Today.Month + 1, 0)
'See: http://msdn.microsoft.com/en-us/library/bbx05d0c.aspx

AddMonths and AddDays
In the C# world, it is not as straight-forward to use the above function. The below work-around helps.

firstDay = new DateTime(Today.Year, Today.Month, 1);
//first day of next month minus one day
lastDay = firstDay.AddMonths(1).AddDays(-1);
//See: http://msdn.microsoft.com/en-us/library/system.datetime.addmonths.aspx

As usual, no rocket-science being discussed here. Just small snippets to make your search life easier :)

 

EMI Calculation is a rather complex math with the denominator being the integration of a series (or something of that sort). It is fairly complex to take a pen and paper and calculate. Well, that is if you do not have access to Excel or a similar spreadsheet. In a spreadsheet, it is a very straight-forward calculation to arrive at an EMI.

In this article am explaining how to calculate EMI and amortization schedule of your loan using MS Excel. Also, you can see by changing the EMI how your tenure changes. I’ve attached a sample sheet here with the calculation.

You can download it here: EMI Explained

This is the EMI formula in Excel
=PMT(D4/12,D6,-D3)
Where
D4 has the per annum interest
D6 has the tenure in months
D3 has the loan amount.

So what if your loan is quarterly diminishing (or compounding, as they say) even though you are paying monthly. In that case the formula changes a bit.
=PMT(D4/4,D6,-D3)/3
Where
D6 has the tenure in quarters (i.e. # of years multiplied by 4)
D3 and D4 remain unchanged.

Note the interest changed from D4/12 to D4/4 and tenure from Years * 12 to Years * 4. Also, there is a division by 3 at the end to make it monthly. Similarly you can extend it to any compounding interval.

With slight changes you can use this to calculate the return of a SIP, assuming an average return over the tenure you plan to invest.

The attached sheet has more than just the EMI formula. It’ll generate the amortization schedule for you, given a tenure so that you can play with the EMI amount to reach at a preferred tenure.

Update: Came to know later that Microsoft has similar template for amortization schedule and loan analysis. You can get it here.

© 2012 code.Snip Suffusion theme by Sayontan Sinha