JKP Application Development Services.

                    Microsoft Office Application Development

Excel Headlines

Up • News • Excel Headlines • Products • Recommendations • Services • Contact • Excel Articles • Excel Books • Downloads • Links • All Comments • Search

•  •

Search Zoeken

WWW
This site

Go to Ga Naar
Home
Up

Donate Donaties
Did you find something helpful on my site? Consider a donation!
Heeft u iets gevonden waar u wat aan had? Overweeg dan een donatie!

 

Excel Headlines

My very own list of news on Excel, gathered from various sources!

This site

New article: Fixing Links To UDF's in Addins
Excel has a host of built-in functions to help you do your computations. But sometimes you need to write your own functions using VBA. These functions are called "User Defined Functions" (UDF). UDF's typically are placed in addins. As long as the addin is installed, the UDF's work as expected. You get into trouble when the location of the addin changes, for example because you have distributed the Addin to your co-workers and they have installed it to their local user addin folder (which is different for each user!). As soon as a file which uses the UDF is opened on a system that uses a different addin path, all cells that use your UDF show #NAME! errors. Also, the complete path to the original location of the add-in is shown in each formula: ='C:\data\FixLink2UDF.xla'!UDFDemo(A1,A2). This article describes a couple of ways to avoid/resolve this situation.
Mon, 02 Jun 2008 18:15:00 GMT
New article: Showing An Array On A Form; Autosizing ColumnWidths Of A ListBox
For a project I needed a quick way to display the content of an array to the user. I didn't want to use a worksheet, but opted for a userform. The data I wanted to show was contained in an array. So I figured I'd put a listbox on a userform and make sure the column widths of the listbox resize with the data I want shown. That proved far from easy...
Wed, 14 May 2008 18:15:00 GMT

Daily Dose Of Excel

By Dick Kusleika (et al)
Excel Links
First, from the double trademark infringement department, there’s Excel®RainMain®, who says: Whether it’s your homework, day job, or just a new idea, let the Excel Rain Man make your life easier! Submit a Request, inquiries are free! (see How it Works for further details) Pay for homework service? For a fee, I’ll read your lit homework [...]
Wed, 02 Jul 2008 16:09:30 +0000
To Disable or Not To Disable, That Is the Question
Joel Spolsky says don’t: Instead, leave the menu item enabled. If there’s some reason you can’t complete the action, the menu item can display a message telling the user why. I thought it was pretty common knowledge that users don’t read message boxes. Anyway, I’m squarely in the disable-menu-items-that-aren’t-applicable camp, and I never hide items. Take the [...]
Wed, 02 Jul 2008 04:01:01 +0000

The Ken Puls Blog

By Ken Puls
Windows Mobile Smartphone thoughts
I still love my phone, but there are a couple of things I’d like to see… We’re activating the feature which requires our users to put in a password to unlock the phone.  In addition, we’ll be activating automatic wipe of the phone if the pin is botched five times.  This should be interesting, as I [...]
Thu, 03 Jul 2008 06:15:12 +0000
Office Mobile
With my new Windows Mobile phone came Office Mobile, including Excel Mobile version 6.1.  (I always thought it was Pocket Office and Pocket Excel, but my phone says otherwise.) Being an Excel guy, I was kind of excited to see this because… er… it’s Excel.  What other reason do you really need?    Now, to [...]
Sat, 28 Jun 2008 07:04:22 +0000

.NET & Excel

By Dennis Wallentin
Some links to useful sources
A new and time consuming project keeps me busy so at present I don’t have the time to post here on a regular basis. Anyway, the following links points to several recently published articles by MSFT.  Finally, the version 1.0 of Open XML Format SDK is released. It provides us with new tools to interact with the XML [...]
Wed, 25 Jun 2008 17:41:25 +0000
Check which Excel version is installed
Whenever I need some enviroment information I always tries to read the wanted values from Windows registry.  In VB.NET this is as rather straightful process as we have access to the namespace “Microsoft.Win32′ which includes the class to work with the registry. Recently I had a need to find out if Excel is installed and if [...]
Sun, 27 Apr 2008 14:31:38 +0000

PTS Blog

By Jon Peltier
Dynamic Chart Source Data
A dynamic chart can be constructed using dynamic ranges that change in size as data is added or removed. This technique is very powerful: you can define flexible ranges limited only by your ability to write an appropriate Refers To formula. One imitation of this approach is that it can only account for dynamic numbers of [...]
Thu, 03 Jul 2008 04:38:06 +0000
Contest Deadline Approaching
In Contest - Excel Models for Science and Engineering I announced a contest for scientific and engineering uses of Excel. People can enter up to three candidate Excel models (up to six if they blog about the contest) by posting a link to the model in a comment to that post or to this one. [...]
Wed, 02 Jul 2008 16:32:25 +0000

Contextures Blog

By Debrah Dalgleish
Convert Measurements in Excel
Last weekend I set up a little spreadsheet in Excel to compare the cost of a trip in a rented RV versus a small car. The only gas consumption numbers I could find for the RV were in miles per gallon. Since we use the metric system here in Canada, I needed to convert everything [...]
Fri, 04 Jul 2008 04:05:14 +0000
Get to the Point in Twitter
I recently signed up for Twitter, to see what all the fuss is about. A few anti-Twitter bloggers have recently posted positive articles, saying it’s a good way to learn new things and connect with different people. So I took the plunge. To start, I found those bloggers, and a few internet marketing types, and followed [...]
Thu, 03 Jul 2008 04:05:52 +0000

Professional Office Developers Association's Blog

By various Authors
Managing Custom Environment Variables
I am in the process of developing a new MS Word form for our company.  This is a work authorization form that is intended to be pretty generic, allowing the user to select the type of work to be performed and any attachments that should accompany the form.  There are various fields that require completion, [...]
Tue, 03 Jun 2008 15:23:46 +0000
MS Access on the Fly
MS Access databases are great for quick data synthesis applications.  The ability to create, populate, and then extract data can really simplify situations where consolidation of information is the eventual goal. A little while back, we put in place a tool that makes life easier for our project management office function. The following describes the [...]
Tue, 22 Apr 2008 15:16:05 +0000

Putting You & I Back Into Office 2007 UI

By Patrick Schmid
RibbonCustomizer bug fixes
I just posted a new RibbonCustomizer release with lots of bug fixes related to custom image handling. Unfortunately, I had to change the image handling code substantially. This new release cannot read images created with the previous one. It should simply not load them and you can add them manually again. If you encounter problems with [...]
Mon, 29 Oct 2007 00:56:51 +0000
RibbonCustomizer - Use custom images
The newest beta version of RibbonCustomizer lets you now use your own custom images as icons on the Ribbon. The only limitation is that you cannot assign custom images to Microsoft commands. But you can assign your own images to all your macro buttons. Important warning: Files generated with the newest beta release cannot be read [...]
Wed, 03 Oct 2007 02:56:02 +0000

Microsoft Excel on MSDN

By various Authors
Building an Asset Tracking Application in Excel Services – Part 3b of 5

Today's author, Dan Parish, continues his discussion on building an Excel Services solution.

In Part 3a I walked through how to create the SharePoint List that stores the data users enter. In this post, I will walk through how to create the Excel Services User Defined Function (UDF) that will pull this information from the SharePoint List into Excel Services.

Overview

Out of the box, Excel Services does not support pulling in data stored in SharePoint Lists. However, you can add this functionality using one of Excel Services' extensibility methods: User Defined Functions.

User Defined Functions (UDFs) are custom worksheet functions that can be used to extend the capabilities of Excel Services. Once implemented they work just like any other function, except that they run your own code. For more information on UDFs, see the blog posts Excel 2007 investments in UDFs Part 1, Part 2, and Part 3.

While you can build UDFs that work on both the client and server, for this post we are just going to create one that works on the server and that fetches the information from the SharePoint List and returns it into the Excel workbook.

The code for the UDF and the workbook is included at the end of this post.

Writing the code

Before you can write any code, the first thing you'll need to do is to create a Visual C# Class Library in Visual Studio. Once you've done that, you'll need to add references to Microsoft.Office.Excel.Server.Udf.dll, and Microsoft.SharePoint.dll (both of which are located your Office SharePoint Server's ISAPI folder).

When writing a UDF, the first step is to mark the class as a UDF class, like so:

[UdfClass]

public class Class1

{

This ensures that Excel Services knows that there are UDF functions within this class. If you remember back to Part 2 of this series, the function that we want to create is called GetMachines, and it takes as a parameter a string that specifies whether we want data for laptops or for desktops. It returns an array of values which we then input into our spreadsheet using an array formula. The function definition then looks like:

[UdfMethod(IsVolatile = true, ReturnsPersonalInformation = true)]
public object[,] GetMachines(string MachineType)
{
}

You'll notice that I've marked it as a UDFMethod which tells Excel Services that this is the function that will be usable in your worksheet. Setting it as volatile ensures that it calculates all the time and won't have its values cached by Excel Services, while saying that it returns personal information is required to impersonate the user that is loading the workbook, which we do in order to access the SharePoint List as them, and not as the Excel Calculation Services' process account.

To impersonate the user, we'll use a simple function called impersonateUser(). All it does is try to get the Windows identity of the user accessing the workbook, and if it can, impersonate them. The function is:

// Causes the UDF to impersonate the user that loaded it through the EWA
private WindowsImpersonationContext impersonateUser()
{

WindowsIdentity wi = Thread.CurrentPrincipal.Identity as WindowsIdentity;
if (wi == null)
throw new InvalidOperationException("Can't get Windows identity.");
return wi.Impersonate();

}

Using that function, the first thing we need to do in our GetMachines function is impersonate the user like so:

// Impersonate the current user using the EWA and not the ECS
using (WindowsImpersonationContext wiContext = impersonateUser())
{

Now that we've created the class and method and impersonated the user, we need to go ahead and get the requested data from the SharePoint List:

try
{

// Declare constants
string serverName = http://server/;
string siteName = "site";
string listName = "list";
string viewName = ""

if (MachineType.Equals("Laptops"))

viewName = "Laptops";

else

viewName = "Desktops";

// Get objects corresponding to the SharePoint Site, List, and List View
SPSite site = new SPSite(serverName);
SPList list = site.AllWebs[siteName].Lists[listName];
SPView view = list.Views[viewName];

// Get a collection of the values in the specified List View
view.Update();
SPListItemCollection values = list.GetItems(new SPQuery(view));

// Create an object array to return to the grid
object[,] toExcelGrid = new object[values.Count, view.ViewFields.Count];        

In this example I am hard coding the server name, site name, and SharePoint List name. These could just as easily be passed in as arguments to the function, but this approach eases the usage for the end user. Also note that I use the MachineType parameter to determine the view that we will be getting list items from. We created the views when creating the SharePoint List in Part 3a, and the views only contain items for the type of machine specified by the MachineType parameter (laptops or desktops).

Now we just need to iterate through the list items, put them into the toExcelGrid object, and then return all that data to the Excel workbook:

// Iterate through each of the SPListItems in the view
int a = 0;
foreach (SPListItem currVal in values)
{

// Add every field value of current item to the array that will be returned
for (int i = 0; i < view.ViewFields.Count; i++)

{

// Get the current field
SPField field = currVal.Fields.GetFieldByInternalName(view.ViewFields[i]);

// Add the item to the array
toExcelGrid[a, i] = field.GetFieldValueAsText(currVal[field.Id]);

}

a++;

}

return toExcelGrid;

}

Finally, we need to add our catch event to return an error message if something goes wrong:

catch (System.Exception ex)
{

object[,] error = new object[1, 1];
error[0, 0] = ex.Message;
return error;

}

That's it!

For more details on UDFs, and a step by step walkthrough of how to create one, see the Excel Services User-Defined Functions article on MSDN.

Deploying your UDF

In order to ease management of your UDF files, start by creating a special folder for them on your server, something like C:\UDFs. Next, copy the Debug DLL that you just created into that folder. Now all we need to do is tell Excel Services about it and enable it.

To get to Excel Services' administration pages, click Start > Administrative Tools > SharePoint 3.0 Central Administration. Your default web browser will launch showing Central Admin. Click on the name of your shared service on the left (SharedServices1 by default), and then in the Excel Services Settings section, click on User-defined function assemblies.

This will take you into the UDF management page. Click Add User-Defined Function Assembly to add the one we just created. Enter the file local path to the UDF (C:\UDFs\GetMachines.dll), and pick File Path as the assembly location. Click Ok, reset your server (using iisreset), and your UDF is ready to go!

Testing your UDF

If you've created the SharePoint List and updated the static variables in the UDF correctly, then all you should need to do is upload the workbook we built in Part 2 and you should start see the values from the SharePoint List in your Excel workbook when it is rendered using Excel Services.


Tue, 01 Jul 2008 05:34:00 GMT

    Subscribe in a readerpowered by longhead.com

Use the contact page to issue questions or comments about this website.
Copyright © 2003-2008 JKP Application Development Services