Page 1 of 1 1
Topic Options
#187121 - 2008-04-20 11:18 PM Paging Glenn. Question about xlRangeFormat UDF
Mart Moderator Offline
KiX Supporter
*****

Registered: 2002-03-27
Posts: 4673
Loc: The Netherlands
Glenn,

Could you shine some light on the usage of your xlRangeFormat UDF? I dl-ed the Excel library from your site but I'm having some trouble with one of the UDF’s.

I'm creating an Excel sheet that has one column that contains phone numbers. All phone numbers start with a 0 for us but excel removes it because basically 0111 is the same as 111. True if it was a number to do some math with but not true in this case. I need the leading 0.
My solution was to use your UDF to set the format to text so Excel leaves the leading 0 alone but all I get is an error on line 34 of the UDF telling me that the required 3rd parameter is missing. The example in the UDF header shows an example from an other Excel UDF so that is of no use right now.

Could you let me in on the secrets of xlRangeFormat? Is it possible to set several cells to text format by using "A1:Z100" as a range? If so, how?

 Code:
....
$rc = xlRangeFormat($xlPtr, 'A2', "Text", 'Sheet1')
....



Oh and BTW I really love the Excel library
Started exploring it today and already got some projects that could use (parts of) it to eliminate some steps and speed things up.

[edit]
Changed the title.
[/edit]


Edited by Mart (2008-04-20 11:25 PM)
_________________________
Mart

- Chuck Norris once sold ebay to ebay on ebay.

Top
#187144 - 2008-04-21 06:59 PM Re: Paging Glenn. Question about xlRangeFormat UDF [Re: Mart]
kholm Offline
Korg Regular
*****

Registered: 2000-06-19
Posts: 714
Loc: Randers, Denmark
I believe the function You should use is: xlRangeFormatNum()

To format a total column, range should be eg. 'A:A'

Example to format column A as Text:

$rc = xlRangeFormatNum($xlPtr, "A:A", "Text",,'Sheet1')

-Erik

Top
#187158 - 2008-04-22 09:55 AM Re: Paging Glenn. Question about xlRangeFormat UDF [Re: kholm]
Mart Moderator Offline
KiX Supporter
*****

Registered: 2002-03-27
Posts: 4673
Loc: The Netherlands
Thanks Erik. I'll give it a go at home tonight.
_________________________
Mart

- Chuck Norris once sold ebay to ebay on ebay.

Top
#187173 - 2008-04-22 08:07 PM Re: Paging Glenn. Question about xlRangeFormat UDF [Re: kholm]
Mart Moderator Offline
KiX Supporter
*****

Registered: 2002-03-27
Posts: 4673
Loc: The Netherlands
Works great.
Two things made me want to cry and feel a little stupid

1 - I misunderstood the UDF name and thought it was a UDF to set the format of a cell and just had a wrong header. xlRangeFormatFont would be more obvious. Reading the top header of the library also wouldn't hurt I guess

2 - One should first initiate Excel and create a workbook before trying to set the formatting on one or more cells. My bad. Been looking at it for way to long and got kinda codeblind and missed this obvious error.

All is working just fine now.
I have it reading some default customer info from and ini file and reading an ad feed from a txt file supplied by the customer.

 Code:
Break on

;Call all functions
$udf = Dir(@SCRIPTDIR + "\functions\*.udf")
While $udf <> "" AND @ERROR = 0
	Call @SCRIPTDIR + "\functions\" + $udf
 	$udf = Dir()
Loop

;Remove invalid characters from  the time and date.
$date = Join(Split(@DATE, "/"), "")
$time = Join(Split(@TIME, ":"), "")

;Read default customer values from ini file.
$name = ReadProfileString(@SCRIPTDIR + "\test.ini", "Custdata", "Name")
$phone = ReadProfileString(@SCRIPTDIR + "\test.ini", "Custdata", "phone")
$website = ReadProfileString(@SCRIPTDIR + "\test.ini", "Custdata", "website")
$email = ReadProfileString(@SCRIPTDIR + "\test.ini", "Custdata", "email")

;Set Excel filename
$xlFileName = @SCRIPTDIR + "\" + $name + "Feed-" + $date + "-" + $time + ".xlsx"

;Initialize excel instance
$xlPtr = xlInit()

;Create workbook.
$rc = xlBookCreate($xlPtr, 1,)

;Set format for row A to preserve the leading 0.
$rc = xlRangeFormatNum($xlPtr, "A:A", "Text", ,"Sheet1")

;Write header row.
$rc = xlRangeValue($xlPtr, "A1", "Phone",)
$rc = xlRangeValue($xlPtr, "B1", "Website",)
$rc = xlRangeValue($xlPtr, "C1", "E-mail",)
$rc = xlRangeValue($xlPtr, "D1", "Value",)
$rc = xlRangeValue($xlPtr, "E1", "Text",)
$rc = xlRangeValue($xlPtr, "F1", "Category",)

;Read feed into array.
$file = ReadFile(@SCRIPTDIR + "\test.txt")

;Set line index for Excel.
;Start at line 2 because lines 1 contains column names.
$lineindex = 2

;Do stuff for each line in the feed.
For Each $line in $file
	;Split line.
	$line = Split($line, "#")
	;Remove leading and trailing sapces.
	For $i = 0 to Ubound($line)
		$line[$i] = Trim($line[$i])
	Next
	;Write data to excel if first ellement is not empty.
	If $line[0] <> ""
		$aData = xlRangeValue($xlPtr, "A" + $lineindex, $phone,)
		$aData = xlRangeValue($xlPtr, "B" + $lineindex, $website,)
		$aData = xlRangeValue($xlPtr, "C" + $lineindex, $email,)
		$aData = xlRangeValue($xlPtr, "D" + $lineindex, $line[0],)
		$aData = xlRangeValue($xlPtr, "E" + $lineindex, $line[1],)
		$aData = xlRangeValue($xlPtr, "F" + $lineindex, $line[2],)
		$lineindex = $lineindex + 1
	EndIf
Next

;Change sheet name
$rc = xlSheetName($xlPtr, $name)

;Save excel file
$rc = xlFile($xlPtr, 2, $xlFileName)

;Quit excel
$rc = xlQuit($xlPtr)
_________________________
Mart

- Chuck Norris once sold ebay to ebay on ebay.

Top
#187181 - 2008-04-23 05:16 AM Re: Paging Glenn. Question about xlRangeFormat UDF [Re: Mart]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4402
Loc: New Jersey
Glad I could be of assistance! ;\)

Actually, just got back from a weekend celebration and had a few minutes to check in this evening after a crazy day back at work.

I'll take a look at the library - I integrated several of Erik's UDFs, and merged a few together, so I may not have updated the headers correctly.

Let me know if you find any other issues - I've been using it in some projects, but haven't had any other serious feedback yet.

Glenn
_________________________
Actually I am a Rocket Scientist! \:D

Top
#187186 - 2008-04-23 09:39 AM Re: Paging Glenn. Question about xlRangeFormat UDF [Re: Glenn Barnas]
Mart Moderator Offline
KiX Supporter
*****

Registered: 2002-03-27
Posts: 4673
Loc: The Netherlands
For now I have no issues whatsoever besides the name of the UDF mentioned above being a bit unclear as to what it actually does but when one reads the header of the library all is explained.
_________________________
Mart

- Chuck Norris once sold ebay to ebay on ebay.

Top
Page 1 of 1 1


Moderator:  Glenn Barnas, NTDOC, Arend_, Jochen, Radimus, Allen, ShaneEP, Ruud van Velsen, Mart 
Hop to:
Shout Box

Who's Online
0 registered and 2220 anonymous users online.
Newest Members
Viginette, ManuvdWielNL, Sir_Barrington, batdk82, StuTheCoder
17888 Registered Users

Generated in 0.065 seconds in which 0.034 seconds were spent on a total of 13 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org