当前位置: 首页 > 图文教程 > 网络编程 > Javascript > Javascript实例教程(21) OLE Automation(6)

Javascript
form中限制文本字节数js代码
use jscript with List Proxy Server Information
use jscript List Installed Software
List Installed Software Features
List Information About the Binary Files Used by an Application
List the Codec Files on a Computer
List the UTC Time on a Computer
List Installed Hot Fixes
excel操作之Add Data to a Spreadsheet Cell
Add Formatted Data to a Spreadsheet
Apply an AutoFormat to an Excel Spreadsheet
JavaScript语法着色引擎(demo及打包文件下载)
类之Prototype.js学习
一款JavaScript压缩工具:X2JSCompactor
iis6+javascript Add an Extension File
jscript之Open an Excel Spreadsheet
jscript之Read an Excel Spreadsheet
jscript之List Excel Color Values
去除图像或链接黑眼圈的两种方法总结
Add a Formatted Table to a Word Document

Javascript实例教程(21) OLE Automation(6)


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-08-10   浏览: 350 ::
收藏到网摘: n/a

使用JavaScript中的OLE Automation

6. 怎样提高脚本程序的性能

自从我们处理重的对象模型(比如Word.Application)和应用程序实例,注意系统的资源示相当重要的。一旦我们完成对应用程序实例的处理,我们必须去掉它,以从内存重清除对象的实例。在Jscript中有唯一的方法来实现,它就是应用程序对象的Quit()方法,以下是一个例子:

var wdApp = new ActiveXObject("Word.Application");

wdApp.Quit(); // tidy up

在Visual Basic中,不象Jscript和VBScript,设置对象变量为空是不能清除内存的。而且,如果没有其它对这个对象进行引用的话,这样的赋值操作将导致应用程序的关闭。不幸的是,在JSCript中我们必须使用Quit()方法在我们已经使用对象的时候来将它从内存中清除。而设置变量为零长度的字符串或者为空只是一个好的练习,它并不能起到清除内存的作用。

在教程中放置点至少代码了一个过程调用不得不在背景中被执行。最好得解决方法就是局部化高速缓冲对象引用。总得说来,这个技巧可以应用到对象以及Automation对象。下面看看脚本片段:


var exApp = new ActiveXObject("Excel.Application");

exApp.Workbooks(1).Worksheets(1).Cells(1, 1).Value = "First Cell";

exApp.Workbooks(1).Worksheets(1).Cells(1, 2).Value = "Second Cell";

exApp.Workbooks(1).Worksheets(1).Cells(1, 3).Value = "Third Cell";

exApp.Workbooks(1).Worksheets(1).Cells(1, 4).Value = "Fourth Cell";

exApp.Workbooks(1).Worksheets(1).Cells(1, 5).Value = "Fifth Cell";


下面的代码说明了以更有效的方法来实现:

var exApp = new ActiveXObject("Excel.Application");

var exWbook = exApp.Workbooks(1).Worksheets(1);

exWbook.Cells(1, 1).Value = "First Cell";

exWbook.Cells(1, 2).Value = "Second Cell";

exWbook.Cells(1, 3).Value = "Third Cell";

exWbook.Cells(1, 4).Value = "Fourth Cell";

exWbook.Cells(1, 5).Value = "Fifth Cell";