首页 > 开发 > 综合 > 正文

Programming MS Office 2000 Web Components第二章第二节

2024-07-21 02:27:37
字体:
来源:转载
供稿:网友
第二章第二节 电子表格组件的高级功能

我们已经讨论了电子表格组件的大部分基本功能,现在让我们转向一些高级功能。大部分的这些高级功能excel2000都不包含,因为这些是组件专门需要的特殊功能。而那些excel2000中存在的功能,在电子表格组件中也被增强,使得可以提供一些新的功能。

属性绑定和实时数据
“属性绑定”是电子表格组件中最新奇的新功能之一,它是指控件能够将同一个web页面上其它对象的属性和方法用作单元值或公式参数的能力。电子表格控件使用标准的com机制来实现绑定到属性,并在数据源对象告知属性值已经改变时,自动接收新值,并重新计算相关联的单元。

例如,如果您开发了一个组件,它公开了一些属性和方法以返回一个给定股票代号的最后销售价格,您就可以使用电子表格控件来查看这个信息,并在价格更新时观察到它的变化。如果电子表格的其它部分――例如当前用户的组合信息(译者注:金融专业的术语。)――引用了这个最后销售价格值,那么电子表格组件也会在值改变时重新计算这些单元。

可以象下面这样在单元中输入一个函数来建立属性绑定:
=document.stockticker.quote("msft").lastsale


只要公式以“=document.”开始,电子表格组件就认为紧接着的是一个属性绑定。它会认为表达式的下一部分将是页面上另一个元素的id,而表达式的其余部分将会该元素的一个属性,或是由一个方法返回的另一元素的一个属性。您可以在方法调用中使用单元引用作为参数,而电子表格控件必然会将真实的值传递给方法。

单元绑定到的对象既可以是另一个com对象,也可以是页面上的任何html元素,例如一个文本框或一个下拉列表。这样您就可以直接在重算模型中包含页面上其它的数据,而不必写脚本来将html元素的值输入到电子表格单元中。

属性绑定机制经常在提供实时数据的环境中被提到,因为它包含了实现提供动态数据的两个必须的特性。
l 当得知属性被修改时,电子表格组件立刻更新单元,而不会有一个固定的轮询间隔。
l “即使用户在编辑其它单元,或执行命令时”,电子表格组件依然会继续监听新值并更新单元。人们对excel的dde链接机制的一个普遍抱怨就是它不能完成这个功能,因此我们确信应该在电子表格控件的属性绑定特性中避免同样的错误。

属性绑定可以指向电子表格组件自身吗?
大胆的读者可能已经在思考将电子表格组件中的单元绑定到组件本身,或绑定到另一个电子表格组件上的可能性。(thinking wildly)从表面上看,似乎可以通过这个机制,轻松的支持关联不同的电子表格上的单元。
但是,唉,这是不可能的。电子表格控件自身禁止了这种支持,因为可能造成的重入引用和循环引用是相当恐怖的。电子表格控件只知道它被绑定到另一个对象上(不是自身或另一个电子表格控件)――因此它无法确保引用没有产生会挂起重算链的一个依赖循环。

为了实现将当前电子表格组件中的单元关联到另一个电子表格组件的单元上,您必须在另一个组件中通过代码来响应change事件,并将新值写入到相关联的单元中。
当然,请记住电子表格组件处理属性变化通知的能力完全依赖于它重算当前模型的时间。对于中小型的模型来说,通常不存在问题,因为重算只会花费一秒或者更少的时间――比起大多数人所能容忍的数据更新时间要快的多。然而,如果模型相当巨大,电子表格控件也只能尽快重算来提供新数值,这可能就比新值到达的速度慢多了。

在第十章将讲述关于属性绑定的更多细节,在该章中,您将看到如何在visual basic中建立一个股票行情控制系统,为股票组合表格提供实时的报价。


函数插件
和在excel中一样,开发者可以使用函数插件,将新功能添加到电子表格组件中。与符合excel私有的xll模型的插件不同,电子表格组件的函数插件是以com对象的方式被创建的。这种对象所公布的任何方法都以内部函数的形式被添加(译者注:什么是potential function?),这样您就可以在公式中象使用那些excel内部函数一样使用它。
例如,如果您开发了一个com对象,包含了一个叫做sumtopn的方法,该方法根据传入的一列数值返回前n位数值的和,那么您可以在电子表格组件中通过下列代码来使用这个函数,就像在window_onload事件中一样:
spreadsheet1.addin myobject


myobject变量应该指向自定义函数对象的一个实例。为了保证您的对象可用,在页面上使用<object>标签,并将对象的id传给电子表格控件的addin方法,如下所示:
<object classid="clsid:0002e510-0000-0000-c000-000000000046"
id=spreadsheet1>
</object>
<object classid="clsid:clsidofyourobject"
codebase=pathtocabfileofyourobject id=myobject>
<script language=vbscript>
spreadsheet1.addin myobject.object
</script>


<object>标签中的codebase属性告诉ie如果客户端机器上没有class id所引用的对象,应该从哪里去安装这个对象。如果需要了解更多关于codebase属性的信息,可以查看msdn库中的ie和dhtml主题。

只有在internet explorer或其它使用不同的界面来包装对象的容器中需要使用object属性。在我们刚才讨论的html文件中,myobject实际上指向了一个称作object的com对象类型(由<object>标签声明),而不是标签创建的实际com对象。object属性返回一个指向实际的com对象的指针。
在visual basic中,您还是需要调用addin方法,但是需要传递一个指向您创建的类的实例的变量。例如:
dim myaddin as new functionlib
spreadsheet1.addin myaddin



在c++中,技巧是一样的,不过您应该使用cocreateinstance函数并向addin方法传递指向您的对象的idispatch接口的引用。

电子表格组件实际上使用这种插件机制来装载不常使用的函数。owc主要的dll文件msowc.dll并没有实现电子表格控件的所有函数,那些不常使用的函数实际上是由msowcf.dll来实现的(“f”代表扩展函数库);当第一次使用它们时电子表格组件自动将他们添加到插件列表中。扩展函数通过包含一系列方法的com对象实现,其中,每一个方法对应一个公布的函数。

您可能会怀疑是否函数插件与之前所描述的属性绑定机制有所不同。答案是肯定的。属性绑定监听值改变时数据源的通知信息,而函数插件仅在函数的输入变化(或影响输入的单元变化)时被调用。这基本上是推模式和拉模式的区别:属性绑定类似推模式;当它认为必要时会将新值推入电子表格控件中。而另一方面,函数插件没有到电子表格组件的通讯通道;电子表格组件决定什么时候需要调用插件函数来计算新值。

有时推和拉的界限是模糊的,尤其是当您意识到ie将页面上的脚本函数以对象的方式暴露出来,这种能力所造成的奇怪而相当有趣的副效果时。您页面上的所有各种<script>块都以被名为script的dom对象公布出来,在<script>块中定义的每一个函数或子方法都被当作script对象的一个方法。这就意味这您可以将您页面上的脚本函数用作您电子表格中的函数,不过您来完成这个功能的机制更像是属性绑定,而不是插件函数。

例如,假设您在页面上有这样一个<script>块:
<script language="vbscript">
function vbdateadd(interval, number, date)
on error resume next
vbdateadd = dateadd(interval, number, date)
end function
</script>


电子表格组件不包括复杂的日期操作函数。不过,vbscript提供了灵活的dateadd函数,允许您在一个给定的日期上加(或减,或使用一个负数作为一个内部的参数)任何数目的时间间隔。为了使电子表格组件能够使用这个函数。前面的<script>块定义了一个名为vbdateadd的函数,返回vbscript函数dataadd的结果。为了在您的电子表格中使用vbdateadd函数,在您需要在显示结果的单元中输入下列的公式:
=document.script.vbdateadd(b1, b2, b3)


这个公式将单元b1,b2,b3的当前值分别用作间隔,数目和日期的参数值。这个公式和其它公式一样,被放置在依赖链中,当那些输入的单元改变时,电子表格组件会调用这个函数,传入新的输入值并显示新的结果。

使用脚本函数有它的优点和不足。脚本是以解释方式来执行的,这意味着他们将比编译的代码要慢一些。脚本也受限于脚本语言的功能和与客户端机器的交互能力,以及网络的限制(因为安全限制。)不过,编译的对象需要下载和在客户端机器上进行安装,在某些机构中可能不允许这样,还有,如果对象编写的不完善,测试不充分,可能会给客户端机器带来潜在的负面影响。web页面中的脚本定义上是”安全”的,并且因为他们是实时解释执行,因此他们不需要下载和安装额外的文件。

当然,只有在容器是ie时才会谈到脚本函数。例如,如果您在vb窗体中使用电子表格组件,您仍然可以使用函数插件,不过这里没有页面上的脚本块的概念。请注意addin方法传入一个com对象的引用,因此如果您在您的应用程序中使用vb,您可以使用工程中的任何公共类作为一个函数插件的对象。只需创建该类的一个实例,然后将它的引用传给电子表格控件的addin方法。

可视区域和自动调整
电子表格应用程序经常只需显示电子表格界面的一部分,而不是显示所有的行和列。例如,时间表应用程序应该显示足够的行和列,以便用户在输入他(她)的工作时间时不必看到数据周围大量的空白行、列。电子表格控件通过viewablerange属性来完成这个任务,您也可以在设计阶段通过属性工具箱来设置它。

“可视区域”定义了电子表格要显示多少行、列。缺省是显示所有的行、列,不过您既可以在脚本中改变它,也可以在设计阶段通过属性工具箱中改变它,可将它改变为任何有效的区域引用。例如,将区域设置为a1:d6,使电子表格只显示四行六列。而其它部分则显示为一个空白的灰色区域;用户既不能在可视区域之外选择,也不能移动到可视区域之外。可视区域之外的单元仍然存在,并可以在脚本代码中被引用,但是用户看不到这些单元,也不能和它们交互。如果需要隐藏对照表(译者注:也就是我们常说的代码表,例如:数据库多个表中都有单位id列,那么单位001,002,……具体代表什么单位,就在单位代码表,既单位对照表中定义。)或不希望用户看到的中间计算值,这是一个极好的方法。

可以通过代码动态调整可视区域,因此您可以调整可视区域来响应其它用户的事件。还要注意可视区域具有和普通区域一样的自动调整特性――如果开发者或用户在可视区域中插入了一行或一列,可视区域就会增加一行或一列。例外,可视区域属性是string类型,而不是range对象类型,因此如果需要获得整个可视区域的区域对象,您需要这样写代码:
set rngviewable = spreadsheet1.range(spreadsheet1.viewablerange)


设置可视区域不会自动调整容器中的电子表格控件的尺寸。不过,只要您定义了一个可视区域,就可将autofit属性设置为true,这会使电子表格自动调整大小,以便不需滚动条就能显示整个可视区域。如果可视区域比控件的当前尺寸小,控件将缩短;如果比它大,它将扩大。
另外两个属性,maxheight和maxwidth,管理电子表格控件增大的限度,可以使控件不会变的过大。这两个属性定义了电子表格高度和宽度的极限。如果可视区域超过了允许的最大高度和宽度,电子表格将显示内部的滚动条,使得用户可以浏览整个区域。
当控件在ie中运行时,可以将maxheight和maxwidth属性设置为百分比。例如,如果maxwidth设置为80%,电子表格将允许自身扩大到容器元素宽度80%的大小。例如,如果电子表格被放在<body>元素中,电子表格将允许扩大到页面宽度的80%。如果它被放置在一个table单元中,电子表格可以扩大到table单元宽度的80%。这种根据百分比来进行缩放的能力也应用在ie中标准的width和height属性中。这种能力在动态html布局的环境中非常有用,并且可以使电子表格能够在页面上正确的布局,而不会受窗口大小和显示器分辨率的影响。
将电子表格组件用作数据源
excel社团中最普遍的需求之一就是希望excel能够成为xls文件中的数据的ole db提供者。当我们开发电子表格组件时,我们意识到我们必须提供一种方法,使得图表组件能够从电子表格组件中获得一系列的数据,以便使用图表来表示它们。图表组件也必须能够感知这些数据的变化,以便它能够更新图表。令人高兴的是,这些需求正是ole db数据绑定所能提供的,因此我们决定将电子表格控件设计成一个真正的ole db数据源。我会马上从技术的角度详细讨论它的意义。不过,首先我将举一个例子以便阐明这一点。
在ie中,您可以将众多的html元素数据绑定到任何是有效数据源的控件上。ie5能够将元素绑定到数据源中某个特定的数据成员上,以适应数据源包含一个或多个数据成员的情况。例如,如果在页面上有一个名为tdccomposers的数据源控件,您可以使用下面的html片断将一个html表格绑定到数据源上。
<table datasrc=#tdccomposers>
<thead>
<tr style="font-weight:bold">
<td>first</td><td>last</td>
<td>birth</td><td>death</td><td>origin</td>
</tr>
</thead>
<tbody>
<tr>
<td><div datafld="compsr_first"></div></td>
<td><div datafld="compsr_last"></div></td>
<td><div datafld="compsr_birth"></div></td>
<td><div datafld="compsr_death"></div></td>
<td><div datafld="origin"></div></td>
</tr>
</tbody>
</table>


同样,您可以将一个html表格绑定到电子表格组件中一个区域的内容上。下面的html片断是从随书光盘的spreadsheetds.htm文件中摘抄的:
<table datasrc=#spreadsheet1.a2:d7 border=1>
<thead>
<tr>
<th>salesperson</th>
<th>fy98 sales</th>
<th>projected growth</th>
<th>est. fy99 sales</th>
</tr>
</thead>
<tbody>
<tr>
<td><div datafld="a"></div></td>
<td><div datafld="b"></div></td>
<td><div datafld="c"></div></td>
<td><div datafld="d"></div></td>
</tr>
</tbody>
</table>


电子表格控件实现了idatasource接口,这是ie和vb6及后续版本所定义和支持的标准数据源接口。这些容器将任何实现了这个接口的控件看作页面或窗体上其它数据绑定控件的一个有效的数据源,一个数据源控件可以暴露任意数量的数据成员,每个数据成员通过一个字符串来标记,并返回一个ole db行集合。电子表格控件几乎暴露了无数的数据成员,因为任何有效的区域引用都是一个有效的数据成员。例如,前面的html片断要求一个名为a2:d7的数据成员,并得到了一个两行七列的行集合。ie5中指定数据成员的神奇的语法是<数据源控件名称>.<数据成员名称>。id前的hash符号(#)显示数据源是当前页面上的一个控件。对于电子表格组件,任何有效的区域引用都能够作为数据成员名称。
当暴露整个区域的数据时,电子表格组件也实现了简单ole db提供者接口(有时被称为osp)。相对于实现irowset和其它ole db接口而言,这个接口是被用来简化ole db中暴露数据的方式的,简单ole db提供者工具包将为那些需要与irowset接口交互的数据绑定控件,把osp映射为一个完整的irowset接口。实际上,ie在绑定控件请求一个irowset接口,而数据源返回的是一个osp接口时会自动使用这些映射关系。
电子表格组件执行简单ole db提供者接口的方式是read/write,当数据源区域变化时,它会发出适当的通知,以便绑定控件得知需要使用新值来刷新它们的内容。(译者注:因为对简单ole db提供者不了解,这两段翻译的有些似懂非懂,有没有熟悉这方面知识的朋友帮忙校正一下这两段?)








附录:英文原文
advanced functionality of the spreadsheet component
now that we've discussed the more basic features of the spreadsheet component, let's move on to some of the advanced ones. most of these do not exist in excel 2000 since they enable specific functionality that is desirable in a component. those that do exist in excel 2000 have been enhanced to enable some new capabilities.
property binding and real-time data
one of the most curious new features in the spreadsheet component is property binding, which refers to the control's ability to use properties and methods of other objects on the same web page as cell values or formula arguments. the spreadsheet control uses a standard com mechanism for binding to properties, and when the source object notifies the control that the property's value has changed, the control automatically retrieves the new value and recalculates any dependent cells.
for example, if you develop a component that exposes properties and methods returning the last sale price for a given stock symbol, you can use the spreadsheet control to view this information and see it change when the value updates. if other parts of the spreadsheet—such as the current user's portfolio information—refer to that last sale value, the spreadsheet control also will recalculate those cells when the value changes.
to set up property binding, enter a function like this into a cell:
=document.stockticker.quote("msft").lastsale


whenever the first part of the formula contains =document., the spreadsheet component knows that a property binding follows. the spreadsheet control will expect the next part of the expression to be the id of another element on the page, and the rest of the expression will resolve to a property of that element or of another element returned by a method. you can use a cell reference as an argument in a method call, and the spreadsheet control will make sure to pass the real value to the method.
the object the cell is bound to can either be another com object or any html element on the page, such as an edit box or a drop-down list. this allows you to include other data on the page in the recalculation model without having to write script to push the html element's value into a spreadsheet cell.
the property binding mechanism is often discussed in the context of real-time data feeds because it includes two necessary features for working with live data feeds:
when notified that the property has changed, the spreadsheet component updates the cell immediately, rather than on a fixed polling interval.
the spreadsheet component continues to listen for new values and updates cells even while the user is editing other cells or invoking commands. a common complaint about excel's dde links mechanism is that it doesn't do this, so we made sure to avoid the same mistake in the spreadsheet control's property binding feature.
can a property binding refer to the spreadsheet component itself?
adventurous readers are probably thinking wildly about the possibilities of binding cells in the spreadsheet component back to the component itself or perhaps to another spreadsheet component. on the surface, it might seem that you can support the dependence of cells in one spreadsheet upon cells in another spreadsheet simply by using this mechanism.
but alas, this is not possible. the spreadsheet control itself prohibits such support because the reentrancy and circular reference possibilities are absolutely frightening. the spreadsheet control only knows it is bound to another object—not to itself or another spreadsheet control—so it cannot check that a reference doesn't create a circular dependency that would hang the recalculation chain.
to have cells dependent on cells in another spreadsheet component, you must write code in the other component that responds to the change event and pushes new values into the dependent cells.
of course, keep in mind that the spreadsheet component's ability to process property change notifications is entirely dependent on how long it takes to recalculate the current model. for small and medium-sized models, this usually isn't a concern since recalculation takes one second or less—far faster than most people want to see new data flash before their eyes. however, if the model is quite large, the spreadsheet control can only process new values as fast as it can recalculate, which might be slower than the rate at which new values arrive.
i'll cover property binding in greater detail in chapter 10, where you'll see how to build a stock ticker control in visual basic that feeds real-time quotes to a stock portfolio spreadsheet.
function add-ins
as in excel, developers can use function add-ins to incorporate new functions into the spreadsheet component. unlike the add-ins that follow excel's proprietary xll model, function add-ins for the spreadsheet component are created as com objects. any method exposed by such an object is added as a potential function that you can use in formulas, just as you would do using the intrinsic excel functions.
for example, if you develop a com object with a method called sumtopn that takes a range of values and returns the sum of the top n numbers, you can make that function available in the spreadsheet component by executing the following code, most likely in the window_onload event:
spreadsheet1.addin myobject


the myobject variable should point to an instance of the custom function object. to ensure that your object is available, use an <object> tag on the page and pass the value of the id attribute to the spreadsheet control's addin method, like so:
<object classid="clsid:0002e510-0000-0000-c000-000000000046"
id=spreadsheet1>
</object>
<object classid="clsid:clsidofyourobject"
codebase=pathtocabfileofyourobject id=myobject>
<script language=vbscript>
spreadsheet1.addin myobject.object
</script>


the codebase attribute in the <object> tag tells internet explorer where to install the object from if the object referenced by the class id isn't on the client's machine. to learn more about the codebase attribute, see the internet explorer and dhtml topics in the microsoft developer network (msdn) libraries.
using the object property is necessary only in internet explorer or other containers that wrap objects with a different interface. in the html file we just examined, myobject actually refers to a com object type known as object, which represents the <object> tag, not the actual com object that the tag created. the object property returns the pointer to the real com object.
in visual basic, you would still call the addin method but would pass a variable that refers to an instance of a class you created. for example:
dim myaddin as new functionlib
spreadsheet1.addin myaddin


in c++, the technique is exactly the same, but you would of course use the cocreateinstance function and pass a reference to the idispatch interface of your object to the addin method.
the spreadsheet component actually uses this add-in mechanism for loading the functions used less often. not all the spreadsheet control's functions are implemented in the primary office web components dll file, named msowc.dll. the ones used less often are actually implemented in msowcf.dll (the "f" stands for extended function library); the spreadsheet component automatically adds them to the add-in list the first time you use them. the extended functions are implemented as com objects with a series of methods, one for each function exposed.
you might be wondering if function add-ins are any different than the property binding mechanism described earlier. the answer is yes. property bindings listen for source notification that a value has changed, whereas function add-ins are called only when an input to the function (or a cell affecting the input) changes. this is essentially the difference between a push model and a pull model: property binding is like a push model; it can push new values into the spreadsheet control whenever it deems this necessary. function add-ins, on the other hand, have no communication channel back to the spreadsheet component; the spreadsheet component determines when it needs to call the function add-in to calculate a new value.
the line between push and pull can get fuzzy sometimes, especially when you consider the strange and rather interesting side effect caused by internet explorer's ability to expose script functions on the page as an object. all the various <script> blocks on your page are exposed as a dom object called script, and each function or subroutine defined in those <script> blocks is exposed as a method of that script object. this means you can use script functions on your page as functions in your spreadsheet, but the mechanism you use to do this is more akin to property binding than to using function add-ins.
for example, suppose you have a <script> block on the page like this:
<script language="vbscript">
function vbdateadd(interval, number, date)
on error resume next
vbdateadd = dateadd(interval, number, date)
end function
</script>


the spreadsheet component doesn't have terrific date manipulation functions. however, vbscript offers the flexible dateadd function that lets you add (or subtract, by using a negative number for the interval argument) any number of intervals to a given date. to enable the spreadsheet component to use this function, the previous <script> block defines a function called vbdateadd that returns the results of the vbscript function dateadd. to use vbdateadd in your spreadsheet, enter the following formula into a cell in which you want the result placed:
=document.script.vbdateadd(b1, b2, b3)


this will use the current values in cells b1, b2, and b3 for the interval, number, and date arguments, respectively. the formula is put into the dependency chain just as any other formula, and any time one of those input cells changes, the spreadsheet component will call this function, passing the new input values and displaying the new result.
using script functions has its advantages and disadvantages. scripts are executed in an interpreted manner, meaning they will usually be slower than compiled code. scripts also are limited to the capabilities of the scripting language and are limited in their interaction with the client computer and the network because of security restrictions. however, compiled objects require downloading and installation on the client machine, which might not be allowed in certain organizations and can have a potentially negative impact on the client machine if the object wasn't implemented and tested well. by definition, scripts in web pages are "safe," and since they are interpreted on the fly, they don't require additional files to be downloaded or installed.
of course, script functions are only relevant when the container is internet explorer. for example, if you are using the spreadsheet component in a visual basic form, you can still use function add-ins, but there is no concept of script blocks in a page. note that the addin method takes a reference to a com object, so if you are using visual basic for your application, you can use any public class in the same project as a function add-in object. just create an instance of it, and pass a reference to the spreadsheet control's addin method.
viewable range and autofit
spreadsheet applications often display just a portion of the spreadsheet surface rather than show all the columns and rows. for example, a timesheet application will show enough columns and rows so that the user can enter his or her work times without having to see numerous blank columns and rows surrounding the data. the spreadsheet control lets you do this through its viewablerange property, which you can also set through the property toolbox at design time.
the viewable range defines how many columns and rows the spreadsheet displays. the default value is to show all columns and rows, but you can change this—either in script or in the property toolbox at design time—to any valid range reference. for example, setting the range to a1:d6 makes the spreadsheet show only four columns and six rows. the rest of the spreadsheet appears as a blank gray area; the user cannot select or move anywhere outside the viewable range. the cells outside the viewable range still exist and can be referenced in script code, but the user cannot see or interact with them. this is an excellent way to hide lookup tables or intermediate calculated values that you don't want your users to see.
the viewable range can be adjusted dynamically through code, so you can adjust the viewable range in reaction to other user events. also note that the viewable range has the same auto-adjustment characteristics that normal ranges do—if the developer or user inserts a column or row inside the viewable range, the viewable range will extend by one column or row. also, remember that the viewablerange property is a string rather than a range object, so if you want to retrieve a range object for the entire viewable range, you need to write code like this:
set rngviewable = spreadsheet1.range(spreadsheet1.viewablerange)


setting the viewable range does not automatically alter the size of the spreadsheet control within its container. however, once you have defined a viewable range, you can set the autofit property to true, which will cause the spreadsheet to resize itself so that it can show the entire viewable range without scroll bars. if the viewable range is smaller than the current size, it will shrink; if it's larger, it will grow.
two other properties govern how large the spreadsheet control can grow so that it does not become ridiculously huge. the maxheight and maxwidth properties determine the height and width thresholds for the spreadsheet. if the viewable range is larger than the maximum height and width allow for, the spreadsheet will show the internal scroll bars so that the user can navigate over the entire range.
the maxheight and maxwidth properties can be set to percentages when running in internet explorer. for example, if maxwidth is set to 80 percent, the spreadsheet will allow itself to grow to 80 percent of the size of the containing element. if the spreadsheet is inside the <body> element, for example, the spreadsheet can grow to 80 percent of the document width. if it is inside a table cell, the spreadsheet can grow to 80 percent of the table cell's width. this percent sizing capability also applies to the normal width and height properties of internet explorer. such a capability can be extremely useful in the world of dynamic html layout and can be used to make sure the spreadsheet looks correct on the page regardless of window size or monitor resolution.
the spreadsheet component as a data source
one of the most common requests heard in the excel group is for excel to be an ole db provider for data contained in an xls file. when we built the spreadsheet component, we knew that we had to provide a way for the chart component to retrieve ranges of data from the spreadsheet component to chart them. the chart component also had to know when those values changed so that it could update the chart. happily, these requirements match those for ole db data binding, so we decided to make the spreadsheet control a real ole db data source. i'll discuss exactly what this means from a technical perspective in a moment. but first, an example will help clarify this point.
in internet explorer, you can data-bind a number of html elements to any control that is a valid data source. internet explorer 5 has the ability to bind elements to a particular data member within a data source in cases where the data source has one or more data members. for example, if you have a data source control on the page named tdccomposers, you can bind an html table to it using the following html fragment:
<table datasrc=#tdccomposers>
<thead>
<tr style="font-weight:bold">
<td>first</td><td>last</td>
<td>birth</td><td>death</td><td>origin</td>
</tr>
</thead>
<tbody>
<tr>
<td><div datafld="compsr_first"></div></td>
<td><div datafld="compsr_last"></div></td>
<td><div datafld="compsr_birth"></div></td>
<td><div datafld="compsr_death"></div></td>
<td><div datafld="origin"></div></td>
</tr>
</tbody>
</table>


in the same manner, you can bind an html table to the contents of a range in the spreadsheet component. the following html fragment is taken from the file spreadsheetds.htm on the companion cd:
<table datasrc=#spreadsheet1.a2:d7 border=1>
<thead>
<tr>
<th>salesperson</th>
<th>fy98 sales</th>
<th>projected growth</th>
<th>est. fy99 sales</th>
</tr>
</thead>
<tbody>
<tr>
<td><div datafld="a"></div></td>
<td><div datafld="b"></div></td>
<td><div datafld="c"></div></td>
<td><div datafld="d"></div></td>
</tr>
</tbody>
</table>


the spreadsheet control implements the idatasource interface, which is the standard data source interface defined and supported by internet explorer and visual basic version 6 and later. these containers consider any control implementing this interface to be a valid source of data to other data-bound controls on the page or form. a data source control can expose any number of data members, each of which is identified by a string and returns an ole db rowset. the spreadsheet control exposes a nearly limitless number of data members because any valid range reference is a valid data member. for example, the previous html fragment asks for a data member named a2:d7 and gets a rowset of two columns and seven rows. the magic syntax in internet explorer 5 for specifying the data member is <id of data source control>.<data member name>. the id is preceded by a hash symbol (#) to indicate that the source is a control on the same page. for the spreadsheet component, any valid range reference can be passed for the data member name.
the spreadsheet component also implements the ole db simple provider interface, sometimes known as osp, when exposing ranges of data. this interface was defined to make exposing data in ole db a tad easier than implementing irowset and the other interfaces of ole db, and the ole db simple provider toolkit provider will map osp into a full irowset interface for data-bound controls that want to work with the irowset interface. in fact, internet explorer will automatically use these mappers when the bound control requests an irowset interface but the source returns an osp interface.
the spreadsheet component's implementation of the ole db simple provider interface is read/write, and it will raise the appropriate notifications when data in the source range changes so that bound controls know to refresh their contents with new values.


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表