当前位置: 主页 > Office办公 > Word专区 > Word教程 > Word2007教程 > Excel2007新知:轻车绕便道,题外不言他 #3 Excel在XLL方面所做

Excel2007新知:轻车绕便道,题外不言他 #3 Excel在XLL方面所做

  • 2022-06-28
  • 来源/作者: Wps Office教程网/ 菜鸟图库
  • 157 次浏览

Quick detour #3: Updates we have made to XLLs

轻车绕便道,题外不言他 #3:Excel 12在XLL方面所做的更新

In the past few months, I have written a couple of articles (big grid, multi-threaded calculation) that prompted comments and questions along the lines of “what about XLLs?” Since the email questions have actually been picking up in frequency (I guess a lot of readers are also XLL authors), I thought I would write a quick article that outlines the changes we are making in this area to support new functionality.

在过去几个月里,围绕“XLL将会怎样”的主题我有写过两篇文章(《更大的表格》、《多线程计算》),给出了一些相关的注解及问题。由于我收到的邮件中关于这方面的提问至今仍有相当的频度(我估计这其中的许多读者也是XLL加载宏的作者),我想我还是得再写上一篇简短的文章,来给大家描绘一下我们在新函数的支持方面所作的变更。

Stepping back for a minute, let me briefly explain XLLs. An XLL is a DLL that is written so that Excel can open it directly. XLLs can be used for a number of things, but (in my experience at least) the most common use of XLLs is creating user-defined worksheet functions (UDFs) to supplement Excel’s intrinsic functions. Developers who write XLLs do so for a number of reasons, the paramount being performance.

先耽搁您几分钟时间,让我来简要地介绍一下XLL为何物。

XLL文件实际上就是一个能够让Excel直接打开的DLL(动态链接库)文件。虽说XLL文件可以用来做很多事情,不过,至少在我的经验中是这样,其最通常的应用是创建用户自定义工作表函数,以扩充Excel的内置函数。(译者注:就这一点而言,用VBA也可以做,那为什么还要编译成XLL文件呢?)开发人员这样做的原因有很多,其中一个最重要的原因就是为了提高执行效率。

We have made three changes to XLLs in Excel 12, primarily to give developers access to new functionality. With Excel 12, XLL authors will have support for

·The bigger grid

·More function arguments

·Multi-threaded calculation

在Excel 12中,我们对XLL的相关应用作了三个方面的变更,主要目的是为了方便开发人员访问新的函数。透过Excel 12,XLL的开发者们将获得下列支持:

·更大的表格

·更多的函数参数

·多线程计算

Big grid and more arguments

更大的表格及更多的参数

When building an XLL today, developers can use a special Excel data type called an “XLOPER”. An XLOPER is essentially a structure that allows developers to pass data types like references, arrays, and error values to and from Excel (technically, XLOPERs can contain 12 possible data types – if you are curious, here is some documentation on MSDN). Currently, several of the data types (i.e. the one that can be used to communicate a reference to and from Excel) are not big enough to support the dimensions of the bigger grid size in Excel 12.

今天,当我们在构建XLL文件的时候,开发人员可以使用一种称之为“XLOPER”的专用数据类型(译者注:XLOPER是OPER 结构的增强版本。在Microsoft Excel 4.0及更高版本中,可以使用这种数据类型来编写调用Microsoft Excel函数的DLL和代码资源,使用XLOPER 结构,DLL函数除了可以传递数据外,还可以传递对工作表的引用并实现流控制)。这是一种基础数据结构,它允许开发人员在应用程序和Excel之间传递或返回诸如引用、数组及错误信息等数据(从技术上来讲,XLOPER能够包含12种可能的数据类型,如果你还想知道更多的话,请点击这里参阅MSDN上的相关资讯)。目前,这其中有一部分数据类型(比如其中一个用来与工作表引用区域交互通信的数据类型)尚不够大,不足以支持Excel 12增大的表格维度(译者注:关于Excel 12工作表的大小及其它方面限制的变更,请参阅Kevin翻译的《大片上映…》)。

To address this, Excel 12 will implement a new XLOPER – which will be called something like XLOPER12 – which will have a larger reference (xltypeRef) data type and a larger array (xltypeMulti) data type which will support the entire big grid. In addition, XLOPER12 will support Unicode data and therefore strings larger than 255 characters. One happy by-product of the support for more characters is that XLLs written using XLOPER12 will be able to support 255 arguments, the same limit supported by Excel 12.

有鉴于此,Excel 12将执行一套全新的XLOPER方案 – 我们暂且称之为XLOPER12。新方案将拥有一个更大的引用(xltypeRef)数据类型及一个更大的数组(xltypeMulti) 数据类型,以支持整个增大的工作表。另外,XLOPER12也将支持Unicode数据并因此能够支持长度超过255个字符的字符串数据,这项改进还带来另一个让人高兴的副产品,那就是使用XLOPER12开发的XLL能够支持多达255个参数,这与Excel 12能够支持的上限是一致的。

The Excel12 function

Excel12 函数

In order to use XLOPER12, developers will need to use a new C API function – Excel12 – instead of the existing Excel4 function. Excel12 is essentially the same function updated to handle the new XLOPER12, so if developers want to take advantage of any of the changes described above, they will need to update their code to call the new API. Note that existing code will continue to run as it always has, so developers will not face no backwards compatibility problems.

要使用XLOPER12的数据结构,开发人员必须使用一个新的C API函数-Excel12-来代替现有的Excel4。Excel12函数本质上讲与Excel4是一样的,升级的目的是为了操作XLOPER12,因此,开发人员如果想要获得上述任何变更所带来的优势,就必须更新其代码以调用新的API函数。当然,现有代码能够一如继往地运行,开发人员不需要面对向后兼容的问题。

Updating XLL’s to take advantage of multi-threaded calculation

更新XLL以利用多线程的计算

As discussed in a previous post, Excel 12 will support multi-threaded calculation. Since performance is very important for the bulk of XLL authors, we wanted to give developers who write UDFs using XLLs a way to allow their XLLs to participate in multi-threaded calculation (meaning Excel would calc multiple UDFs at once thereby improving calculation times).

就像上一个帖子中讨论的那样,Excel12将会支持多线程计算。由于对大多数XLL开发者而言,产品性能是至关重要的,因此,我们应该给那些使用XLL编写用户自定义函数(UDFs)的开发人员们一个参与多线程计算的途径,这就意味着Excel将能够同时计算多个用户自定义函数(UDFs),亦即提高了计算次数。

To do so, XLL authors need to do two things. First, (the harder part) they need to make sure their XLL UDF (the code they wrote) is threadsafe (i.e. it must not make any non-thread-safe callbacks into Excel). Second, they need to make a minor update to their XLL to tell Excel 12 it is threadsafe. This is done in pretty much the same way that an XLL author would tell Excel that their function is volatile – by adding a special character to the “type text” entry in an XLL’s function table (the type text entry defines the data types for the functions’ return value and arguments).

要达成这一目标,XLL的开发人员需要做两件事情:第一件事,也是最困难的一件,开发人员必须确保他们的代码是线程安全的,也就是说,代码不得造成任何导致Excel发生非线程安全的回调行为。第二件事,开发人员必须对他们的XLL代码作一些小的更新,以便告诉Excel 12这些代码是线程安全的。这一点,与XLL的开发人员通过为XLL函数表中的 “类型说明”入口点(用于定义函数返回值及参数的数据类型)添加某些特殊的字符,以此告诉Excel程序这些是易失性函数的作法,是非常相似的。

Specifically, to indicate that an XLL function is safe for multi-threaded calculation, an XLL author needs to add a "$" to the type text entry in an XLL’s function table … so the type text entry for a UDF that had one argument would look like this “ RR$”. If this character is present, Excel will allow the UDF to participate in muti-threaded calc, just like Excel’s intrinsic functions; if the flag is absent, the reverse is true – all calls to that UDF will be run on a single thread. Note, the Excel4 API will also respond appropriately to the “$” character, so if an XLL author wants to take advantage of multi-threading, but doesn't care about the big grid, they do not have to use the Excel12 API.

更确切地说,要明示一个XLL函数对于多线程计算是安全的话,XLL的开发人员必须为XLL函数表中的类型说明入口点添加一个“$”符号…如此,带有一个参数的用户自定义函数的类型说明入口点看起来就象这样“ RR$”。如果这个字符有提交,Excel将允许用户自定义函数参与多线程计算,就象是Excel的内置函数一样;如果这个标志缺失的话,那么情况就相反-所有对用户自定义函数的调用都将只能进行单线程计算。值得关注的是,Excel4 API函数同样会对这个“$”符号作出适当的响应,因此,XLL的开发人员如果不想顾及Excel 12增大的表格,而又想利用多线程计算的话,他们不一定非得使用Excel12 API。