日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

[转载]Programming MS Office 2000 Web Components第二章第三节

發布時間:2025/3/15 编程问答 40 豆豆
生活随笔 收集整理的這篇文章主要介紹了 [转载]Programming MS Office 2000 Web Components第二章第三节 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
第二章第三節 編程模型要點 現在總結一下我們對電子表格組件的介紹,我將講述(電子表格)控件編程模型的各個要點,以便您了解如何運用這個控件,以及當您需要編寫腳本來實現不同的功能時該如何去做。本節不是編程模型的完全指南――那將會是另外一本書。在這里會介紹常用的屬性和方法,每個屬性和方法會附帶一段簡短的描述。如果需要了解編程模型的更多信息,可以參考Msowcvba.chm文件中的聯機幫助,它位于Office目錄的本地化目錄下(對于英語語系的人來說,一般是Program Files\Microsoft Office\Office\1033目錄)。 如果您已經非常熟悉Excel的編程模型的話,您會發現它和電子表格組件的編程模型非常相似。 在腳本中使用枚舉 COM和OLE自動化允許組件定義枚舉,每個枚舉都是指定的一組常量,作用類似編程語言中的類型聲明。可以將一個枚舉用作屬性或方法的參數,這使得如VB和VC這樣的環境能夠顯示包含一個枚舉中有效常量的語句完成下拉列表 (譯者注:語句完成功能指開發環境會建議開發者如何完成一行代碼,使開發者不需要記住對象的所有成員)。OWC和其它許多ActiveX控件一樣,包含了一系列預定義的枚舉。 不過,在例如web頁面或asp頁面這樣的腳本環境中,不存在類型的概念。因此,在現有的這些腳本語言中不能使用枚舉成員名稱,因為這些語言無法知道一個特定的常量(例如ssHAlignLeft)對應的是什么。這意味著您的VBScript或ECMA腳本代碼會因為不能自描述的神秘數字而混亂不堪(譯者注:指程序中大量的數字常量造成程序很難被讀懂)。 為了盡量解決這個問題,所有的owc組件都有一個名為Constants的頂級屬性,在腳本語言中可以使用這個屬性訪問組件的各種枚舉成員。例如,如果您需要在VBScript代碼中使用ssHAlignLeft常量使一個單元的內容居左顯示,您可以這樣書寫代碼:
MyRange.HAlignment = Spreadsheet1.Constants.ssHAlignLeft
為了使用一個枚舉常量,您可以把它當作Constants對象的一個屬性來引用它,而對象將返回枚舉成員的正確的值。 請注意,如果您計劃在您的函數的多處使用常量對象,那么設置一個等于Constants對象的變量是很有用的,這樣做既避免了不必要的打字,也能夠提高性能。例如,您可以這樣書寫代碼:
Set c = Spreadsheet1.Constants MyRange.HAlignment = c.ssHAlignRight MyRange.VAlignment = c.ssVAlignBottom
Constants對象只在沒有枚舉概念的腳本語言中有意義。如果您是在VB,VBA或C++中編寫代碼,請象平常那樣直接使用枚舉,不用理會Constants對象。 將數據輸入組件 之前我已經講述了多個將數據輸入組件的技巧。我現在將更詳細的描述編程模型中的這些技巧。表2-1列出了與裝載數據相關的屬性和方法,所有這些屬性和方法都通過電子表格對象的接口公布。 表 2-1. 裝載數據相關的屬性和方法。
屬性或方法 描述
Spreadsheet.DataType 字符串類型的屬性,告訴電子表格組件在多個用于裝載數據的屬性被設置時,應該使用哪個屬性來裝載數據,您賦給這個屬性的值就是您應該用來裝載數據的那個屬性的名稱――例如,將這個屬性設置位HTMLURL,會使組件裝載從屬性HTMLURL中的URL處 獲得的數據。
Spreadsheet.HTMLData 字符串類型的屬性,可用來獲得或設置HTML表格格式的電子表格內容。這種格式也包含了額外的屬性和XML標簽,這些屬性和標簽是用來保留重建電子表格模型所必須的信息(例如一個單元公式)的,不過它們不屬于HTML3.2的表格格式。您可以將這個屬性設置為包含了一個HTML表格的一個字符串,或者在需要保存時通過讀取該屬性值來獲得電子表格完整的內容。
Spreadsheet.HTMLURL 字符串類型的屬性,包含了一個URL,可以從這個URL處裝載電子表格的數據。這個URL必須返回一個包含了表格的HTML文檔。使用Excel 2000中來保存為HTML格式的電子表格可以使用這個屬性來裝載,這個屬性也可以是一個從數據庫中實時生成表格的ASP頁面或CGI程序。
Spreadsheet.CSVData 字符串類型的屬性,和屬性HTMLData類似,不過它接受和返回的數據的格式是CSV。當需要從不能發布HTML格式的老系統中裝載數據時這個屬性很有用。
Spreadsheet.CSVURL 字符串類型的屬性,和屬性HTMLURL類似,不過它希望從URL處獲得的數據格式是CSV格式的。就象HTMLURL屬性一樣,這個URL可以是一個從數據庫中實時生成表格的ASP頁面或CGI程序。
Spreadsheet.LoadText 可以將一個被分隔的文本文件載入電子表格的方法。與CSV格式不同的是,這個文本文件可以使用任何字段分隔符。Range對象也可使用LoadText方法和它的兄弟方法ParseText,來將文本載入電子表格的一個指定的區域中。LoadText方法可以指向一個您本地文件系統中的文件或指向一個URL。
使用區域 電子表格組件中最常使用的編程接口是Range對象。很多方法都返回Range對象,當需要對一個區域的各單元進行修改,格式化,排序和設置自動篩選時,都會用到Range對象。表2-2顯示了在你建立電子表格組件的解決方案時應該了解的Range對象的屬性和方法。 表 2-2. Range對象主要的屬性和方法。
屬性或方法 介紹
Spreadsheet.Range 該方法接受一個區域引用(例如A1:B2或A:B),返回一個Range對象。因為一個區域可以只有一個單元,因此您也可以傳入單個單元的引用(例如A1)。該方法也可以接受兩個不同的單元引用,而返回一個含蓋這兩個單元的區域。
Range.Address 該屬性返回一個區域的地址(例如,A1:B2)。
Range.Cells 當我首次看到這個函數時,我非常迷惑,因為它被定義為返回另一個Range對象。不過,您可以將該屬性當作多個單元的一個集合,也就是說您可以使用For Each循環訪問其中的每一個單元。該屬性也可以以二維數組的形式來訪問。例如,MyRange.Cells(1,3).Value會返回區域中行1,列3的值。電子表格和工作表對象也有Cells屬性,因此您可以使用它代替前面提到的Range方法來訪問指定單元。
Range.Column, Range.Row 這些屬性指出區域中首行和首列的編號。當您在一個區域的行和列中進行迭代時,如果需要知道當前位于哪行哪列,這個屬性非常有用。
Range.Columns, Range.Rows 雖然在名稱上和前兩個屬性非常相似,但是這對屬性返回的是Range對象包含的行或者列的集合。Range.Columns.Count和Range.Rows.Count可以告訴你當前區域中行和列的數目。
Range.HTMLData 該屬性和Spreadsheet.HTMLData屬性相似,不過Range對象的這個屬性是只讀的。使用它可以快速獲得一個給定區域中數據的HTML表格的表現形式。
Range.Value 該屬性獲得或設置區域的值。雖然幫助文件說在區域由多個單元構成時Range.Value會返回一個二維變量數組,但實際上當前版本中這是沒有實現的。不過,Range.Value可以接受一個二維數組的變量,來將數據輸入區域中,當你需要使用文字值設置一個或多個單元值,或者是需要獲得一個單元的無格式的值時,可以使用這個屬性。(譯者注:無格式的值指沒有經過格式化的值。)
Range.Formula 該屬性讀寫一個單元的公式字符串。當您需要獲得或設置區域中一個或多個單元的公式時可以使用這個屬性,記住在公式的開始使用等號(=)。
Range.Text Range.Text屬性返回Range.Value屬性的格式化版本。當您需要在消息框或另一個用戶界面元素中顯示格式化的值時這個屬性很有用,該屬性值就是您在添加AutoFilter對象的過濾條件時傳入的值。(譯者注:也就是說,過濾是根據一個單元格的表面值進行過濾的)
格式化 在將數據載入電子表格后,您可能需要通過編程來控制格式化。每個單元都有自己的字體,對齊方式,邊框,顏色和數字格式屬性,所有這些特征都可以使用表2-3中的屬性來設置。 表 2-3. 用于格式化的屬性。
屬性 描述
Range.NumberFormat 字符串類型的屬性,控制單元中數字的格式。您可以引用現有的格式名稱來使用眾多的內置格式(例如Currency)。也可以創建自定義格式(例如,#,###,使1000顯示為1,000)。
Range.Font 返回一個許多組件都能使用的常用的Font對象。Range.Font允許設置Font對象的的多個屬性,例如Name,Size,Bold,Italic,Color和Underline。請注意,如果需要,可以在Font.Color屬性中使用IE的顏色名。
Range.Halignment, Range.Valignment 這兩個屬性控制區域的各單元水平和垂直方向上的文本對齊方式。每個屬性中定義了各自可以使用的對齊方式的枚舉值。
Range.Borders 該屬性返回一個Borders對象,可以通過它來設置每個單元邊框的各個特性,如邊框線的線粗,樣式及顏色。
組件級的外觀和行為 許多屬性和方法會影響整個電子表格組件。表2-4列出了定制的解決方案中最有趣的屬性和方法。 表 2-4. 影響整個電子表格的主要屬性。
Property Description
Spreadsheet.AllowPropertyToolbox 控制是否顯示屬性工具箱(譯者注:指“命令和選項”窗口)。如果該屬性設置為False,屬性工具箱在工具條上的圖標和在右鍵菜單上的菜單項會被禁止。可以隨時使用這個屬性來禁止缺省的格式化用戶界面,并提供您自己的格式化界面。
Spreadsheet.AutoFit 決定控件是否處于”自動適應”模式。請查看前面章節中關于AutoFit的細節來了解這個特性是如何工作的。
Spreadsheet.Dirty 當控件中有任何修改,任何單元被改變后,這個屬性會被置為true以通知您這個改變。通常使用Dirty屬性來判斷是否需要保存內容。請注意這是一個read/write屬性,因此可以通過重置它來使電子表格又變”干凈”。You commonly use the Dirty property to determine whether you need to save the contents in some way.
Spreadsheet.DisplayColHeaders, Spreadsheet.DisplayRowHeaders 控制是否顯示行頭和列頭。這兩個屬性的缺省值是True。通常,您可以在需要使用您自己的代碼來對電子表格界面進行完全控制時將這兩個屬性設置為False。
Spreadsheet.DisplayGridlines 控制是否顯示網格線。缺省是要顯示的,如果在定制解決方案中,要在需要單元分隔線的地方使用邊框,則常關閉這個屬性。
Spreadsheet.DisplayPropertyToolbox 控制是否顯示屬性工具箱。將屬性置為true則顯示,置為false就會隱藏。
Spreadsheet.DisplayTitleBar 控制是否顯示標題欄。缺省是顯示標題欄的。可以使用下面介紹的TitleBar屬性來修改標題欄上的內容和格式。
Spreadsheet.DisplayToolbar 控制是否顯示工具條。缺省是顯示。
Spreadsheet.EnableAutoCalculate 控制電子表格模型如何重算。如果這個屬性被置為false,電子表格模型不會自動重算;必須調用工作表對象的Calculate方法,才能看到改變輸入后的新結果。在計劃修改一系列的輸入,并且希望只有在完成所有的修改后,才重算模型時,這個屬性非常有用。這個屬性缺省為True――模型會在電子表格被修改時自動進行重算。
Spreadsheet.ScreenUpdating 缺省情況下,電子表格在屏幕上的顯示總是反映最新的數據,不過如果計劃執行大量的操作,而不希望電子表格在每一個操作之后都會因為更新而閃爍。可以將這個屬性置為Fasle。將這個屬性重新設回True會觸發一個徹底的重繪動作。
Spreadsheet.Selection 返回當前選擇的對象。可以使用VBA或VBScript中的TypeName函數來判斷這個對象的類型。
Spreadsheet.TitleBar 使您可以訪問電子表格的標題欄,可以改變標題欄的文本和格式。
Spreadsheet.ViewableRange 控制電子表格實際可見的部分。請查看前面關于ViewableRange和AutoFit的介紹,以了解這個屬性是如何工作的。
排序和過濾 表 2-5 列出了在電子表格組件中排序和過濾數據時可以使用的屬性和方法。 Table 2-5. 具有排序和過濾功能的屬性和方法。
屬性或方法 介紹
Range.Sort 根據給定的列和排序方法對區域進行排序。
Worksheet.AutoFilter 該屬性返回一個AutoFilter對象,該對象可用來設置當前過濾器的細節。
AutoFilter.Filters 該屬性返回當前自動過濾區域的過濾器集合。一個過濾器對象應用于自動過濾區域中的一列,過濾器對象的下標和對應區域中列的下標相對應。
AutoFilter.Apply 該方法應用一個新的自動過濾。在設置完過濾條件后,必須調用這個方法來實際應用過濾器。
Criteria.FilterFunction 該屬性控制符合過濾條件的項是包括在過濾器中,還是被排除在過濾器之外。如果是包含,則過濾器僅包含符合過濾條件集合的項,而如果是排除,則過濾器包含除了哪些符合過濾條件集合的項之外的所有項。
Criteria.ShowAll 該屬性決定是否顯示所有的數據。當設為True時,該屬性重置過濾器來顯示所有的數據。當設為False時,假如沒有任何過濾條件,將不顯示任何數據。
Criteria.Add 該方法將一個新的過濾條件添加到過濾器中。
Range.AutoFilter 該方法針針對一個給定的區域打開AutoFilter開關。首先調用這個方法,然后使用Worksheet.AutoFilter屬性(如上所述)訪問過濾器并設置過濾條件。
保?? 護 如果需要保護電子表格的某部分,使您的用戶不能修改單元內容或改變單元格式,您需要使用控制保護功能的各屬性。表2-6列出了常用的具有保護功能的屬性,并給出如何使用每一個屬性的簡短的描述。 請注意,保護設置即通過用戶界面應用到用戶交互中,也應用到代碼中執行的操作中。因此如果您在一個保護選項被啟用的情況下需要刪除一行,您必須在執行刪除操作前將Protection對象的Enabled屬性設置為False,并在完成操作后,將Enabled屬性設回True以便返回到受保護的狀態。 表 2-6. 常用的具有保護功能的各屬性。
屬性 描述
Worksheet.Protection 返回一個Protection對象,可以使用它設置各種保護選項,從而啟用各種全局操作(例如:插入和刪除行)。
Protection.Enabled 控制一般情況下是否啟用保護。在需要使用保護選項或鎖定單元格時,首先設置選項或者鎖定單元格,然后將這個屬性設為True。當需要在代碼中執行操作時,可以將該屬性設為False來暫時禁止保護功能。
Protection.AllowInsertingColumns, Protection.AllowInsertingRows, Protection.AllowDeletingColumns, Protection.AllowDeletingRows 允許或禁止在電子表格中插入、刪除列或行。例如,如果AllowInsertingRows被設為False,則電子表格會禁止所有用來插入一行的命令,包括哪些編程模型中的命令。
Protection.AllowSizingAllColumns, Protection.AllowSizingAllRows 允許或禁止調整列或行的大小。例如,如果AllowSizingAllRows被設為False,電子表格就不會允許用戶調整行的大小,也不會允許您通過代碼來這樣做。
Protection.AllowSorting 允許或禁止在電子表格中排序數據。將AllowSorting設為False會禁止用戶對任何區域中的內容進行排序。
Protection.AllowFiltering 允許或禁止使用自動篩選功能。將這個屬性設為False會禁止用戶啟用自動過濾功能。
撤? 消 表2-7 列出了當您控制電子表格組件的撤消機制時會用到的有關的屬性和方法。 表 2-7. 控制撤消機制相關的屬性和方法。
屬性和方法 描述
Spreadsheet.BeginUndo 一個使您可以將一系列的操作看作一個撤消操作的方法。例如,如果您調用BeginUndo方法,然后執行了三個不同的排序動作,或改變了許多單元,您可以一下撤消所有這些操作。
Spreadsheet.EndUndo 一個標記您的邏輯撤消單元的結束的方法。在調用BeginUndo方法和調用EndUndo方法之間執行的所有操作會被當前一個單一的單元來撤消。
Spreadsheet.EnableUndo 該屬性控制撤消功能是否可用。缺省是可用的。您可能需要暫時禁止這個功能,以便節約內存或在代碼中執行大量的操作。
有用的事件 電子表格的頂級對象公布了大量的事件,比OWC庫中的任何其它控件都要多。表2-8列出了一些關鍵的事件,當圍繞電子表格組件開發定制解決方案時您很可能會需要使用它們。 幾乎所有電子表格控件中的事件都向事件處理函數傳遞一個類型為SpreadsheetEventInfo的單一參數。SpreadsheetEventInfo是一個COM對象,您可以使用它來獲得事件被觸發時應用程序狀態的各種信息,包括什么被選擇了,那個區域被影響了,鼠標的位置在哪里,那個鍵被按下,等等。這反映出DOM中事件信息的處理方式。 使用一個對象來作為事件參數的最重要的原因,是因為需要支持JavaScript中的撤消事件的功能。JavaScript中傳遞給事件的參數總是值傳遞的,除非參數是對象的指針。換句話說,如果OWC小組將事件設計為附加一個ReturnValue參數,腳本可以通過將這個參數設置為True來撤消事件,那么在JavaScript中因為參數的值傳遞方式這不會正常工作。然而,如果腳本將一個對象的ReturnValue屬性設置為True,觸發事件的控件則會正確的發現它。因此,如果您需要撤消一個事件(大部分以”Before”開頭的事件是可以被撤消的),請將SpreadsheetEventInfo對象的ReturnValue屬性設為False。 表 2-8. 有用的事件
事件 描述
Spreadsheet.Change 只要電子表格中的一個或多個單元被改變,該事件就會被觸發。可以使用SpreadsheetEventInfo對象的Range屬性來判斷被改變的區域。
StartEdit, EndEdit, CancelEdit 當一個單元將要被編輯,正要結束編輯,或正要撤消編輯時觸發。您可以在EndEdit事件處理函數中執行數據校驗,并通過將SpreadsheetEventInfo對象的ReturnValue屬性設為True來拒絕輸入的新值。使用SpreadsheetEventInfo對象的EditData屬性可獲得單元的新值。為了編輯的需要,可以在StartEdit事件中使用另一個元素來代替顯示的值。例如使用一個TrueType字體來顯示一個特殊的符號,而不是使用文字的描述。
BeforeCommand, Command 在正要執行一個命令之前和剛執行完一個命令后觸發――例如排序;過濾;插入或刪除行或列;顯示幫助;和剪切,拷貝或粘貼這樣的動作――被執行時。在Msowcvba.chm文件中或在您的對象瀏覽器中查看SheetCommandEnum常量的列表,可以知道在這些事件中可以捕捉的所有命令。同樣的,如果需要撤銷一個事件的缺省行為,您可以將BeforeCommand事件中的ReturnValue屬性設置為False。例如,您可能希望當用戶點擊電子表格工具條上的幫助按鈕時顯示您自己的幫助頁面。
獲得版本信息 有時您需要查看正在使用的控件的版本,以便能夠利用新特性的優勢,或者使用變通的代碼來解決一個較早版本中的問題。大部分的軟件程序都會在主要的版本之間發布SR版本,因此您常常需要檢查您的代碼正在交互的版本是否就是您所期望的版本。 為了幫助您完成這些工作,我們為owc庫中的每一個控件添加了表2-9中的屬性。您可以使用這些屬性來判斷您的代碼正在操作的控件的版本,并執行適當的動作。 表 2-9. 所有Office Web 組件都具有的版本信息屬性。
屬性 描述
MajorVersion 一個long型的整數值,指示組件的主版本號。對于Office 2000版本來說,這個數字是9。
MinorVersion 一個字符型的值,指示組件的次版本號。對于Office2000版本來說,這個數字是0,如果在下一個主版本發布之前,發布了任何小版本,這個數字會增加。請注意,為了處理出現例如”a”這樣的版本號的情況,MinorVersion是一個字符串值。最好對這個值進行相等的比較操作,而不要進行大于或小于的比較操作。
BuildNumber 一個字符型的值,指示組件的編譯數目。編譯數目會隨著組件DLL的每一次編譯而增加。Office 2000版本的這個數值在寫作這本書時還不可用,這也是一個字符型的數值,因此,它可以處理在發布一個次版本時版本號中被加入了一個字母的情況。
Version 一個字符型的數值,返回整個版本數值。當需要顯示版本號時可以使用這個屬性,但是在需要判斷一個版本是否就是您需要的那個版本時請使用其它的屬性。
附錄(英文原文): Key Elements of the Programming Model To conclude our introduction to the Spreadsheet component, I'll cover the key elements of the control's programming model so that you'll know how to get the control working and where to go when you want to write script for different functionality. This section is not a full reference to the programming model—that would be a book unto itself. Instead, the properties and methods you'll commonly use are presented here, along with a brief description of each. For more information on any part of the programming model, refer to the online help in the Msowcvba.chm file, which you can find in the local folder under the Office folder (generally Program Files\Microsoft Office\Office\1033 for English-speaking people). If you are at all familiar with the Excel programming model, you will notice that the Spreadsheet component's programming model is quite similar. Working with Enumerations in Script COM and OLE Automation enable components to define enumerations, each of which is a named set of constant values that acts like a type declaration in a programming language. A property or method argument can be typed as an enumeration, which causes environments such as Visual Basic and Microsoft Visual C++ to display the statement completion drop-down list containing the valid constants for that enumeration. The Office Web Components, like many other ActiveX controls, have a predefined set of enumerations. However, in scripting environments such as a web page or an ASP page, no notion of types exists. Therefore, no ability to use enumeration member names in script languages exists since these languages have no way of knowing what a particular constant, such as ssHAlignLeft, evaluates to. This means your VBScript or ECMA Script code can get littered with magical numbers that are not self-describing. To alleviate this problem, all the Office Web Components have a top-level property called Constants that can be used in scripting languages to access their various enumeration members. For example, if you want to use ssHAlignLeft in VBScript code to align a cell's contents to the left, you can write code like this:
MyRange.HAlignment = Spreadsheet1.Constants.ssHAlignLeft
Working with Ranges The most commonly used programming interface in the Spreadsheet component is that of the Range object. Range objects are returned from many methods and are used whenever you want to modify the contents, formatting, sort order, or filter settings of a range of cells. Table 2-2 shows the properties and methods of the Range object that you should know about when building solutions with the Spreadsheet component. Table 2-2. Principal Range object properties and methods.
Property or Method Description
Spreadsheet.Range This method returns a Range object given a range reference (such as A1:B2 or A:B). Because a range can be just one cell, you can also pass a single cell reference (such as A1). The Range method can also take two different cell references and return a bounding range.
Range.Address This property returns the address of the range (for example, A1:B2).
Range.Cells I was highly confused by this property when I first saw it because it's typed to return another Range object. However, you can use this property as a collection of cells, meaning that you can access the individual cells using a For Each loop. The property can also be accessed as a two-dimensional array. For example, MyRange.Cells(1,3).Value will return the value from row 1, column 3 in the range. There is also a Cells property for the Spreadsheet and Worksheet objects, so you can use it instead of the Range method (described earlier) to access specific cells.
Range.Column, Range.Row These properties indicate the number of the first column and first row in the range. They are useful when you're iterating over a range of columns or rows and want to know what column or row you're at.
Range.Columns, Range.Rows Although strikingly similar in name to the previous two properties, this duo returns a collection of columns or rows contained in the Range object. Range.Columns.Count and Range.Rows.Count tell you the number of columns and rows in the current range.
Range.HTMLData This property is similar to Spreadsheet.HTMLData, except that it is read-only for the Range object. Use it to quickly get an HTML table representation of the data in a given range.
Range.Value This property gets or sets a variant value for the range. Although the help file states that Range.Value returns a two-dimensional array of variants if the range constitutes more than one cell, it actually isn't implemented in this version. However, Range.Value can accept a two-dimensional array of variants for putting data into the range. Use this property when you want to set the cell or cells to a literal value or when you want to get a cell's current unformatted value.
Range.Formula This property is used to read or write the formula string for a cell. Use it when you want to get or set the formula for a cell or cells in a range, and remember to use the equals sign (=) at the beginning of the formula.
Range.Text The Range.Text property returns the formatted version of the Range.Value property. It is useful when you need to present the formatted value in a message box or another user interface element, and it's the value you pass when adding AutoFilter criteria.
Formatting Once you have loaded data into the spreadsheet, you might want to apply formatting programmatically. Each cell can have its own font, alignment, border, color, and number formatting, and all these aspects are set using the properties shown in Table 2-3. Table 2-3. Formatting properties.
Property Description
Range.NumberFormat A string-based property that controls the formatting used for a cell's numeric value. A number of built-in formats that you can use by name exist (such as Currency). You can also construct your own format definitions (for example, #,###, which makes 1000 appear as 1,000).
Range.Font A property that returns the common Font object used by many of the components. Range.Font lets you set various properties of the Font object such as Name, Size, Bold, Italic, Color, and Underline. Note that you can use the Internet Explorer color names with the Font.Color property if you want.
Range.Halignment, Range.Valignment Two properties that control the horizontal and vertical text alignment within the range's cells. Enumerations that contain the possible alignment values are defined for each of these properties.
Range.Borders A property returning the Borders object that lets you set the various aspects of each cell border, such as line weight, line style, and line color.
Component-Level Appearance and Behavior A number of properties and methods affect the entire Spreadsheet component. Table 2-4 shows the most interesting ones for custom solutions. Table 2-4. Principal properties that affect the spreadsheet as a whole.
Property Description
Spreadsheet.AllowPropertyToolbox Controls whether the Property Toolbox can be shown. If AllowPropertyToolbox is set to False, the Property Toolbox toolbar icon and context menu command are disabled. You use this property any time you want to disable the default formatting user interface and supply your own.
Spreadsheet.AutoFit Determines whether the control is in AutoFit mode. See the section earlier on AutoFit for more details on how this feature works.
Spreadsheet.Dirty Tells you if anything has changed in the control. If any cell has been modified, this property returns True. You commonly use the Dirty property to determine whether you need to save the contents in some way. Note that this is a read/write property, so you can also reset it to make the spreadsheet "clean" again.
Spreadsheet.DisplayColHeaders, Spreadsheet.DisplayRowHeaders Control whether the column and row headers are displayed. These two properties are True by default. Normally, you set them to False if you want to have total control over the spreadsheet surface from your code.
Spreadsheet.DisplayGridlines Controls whether gridlines are displayed. By default they are, and it's common to turn them off for custom solutions that use borders in specific places where you want cell separator lines.
Spreadsheet.DisplayPropertyToolbox Controls whether the Property Toolbox is displayed. Set this to True to display it or False to hide it.
Spreadsheet.DisplayTitleBar Controls whether the title bar is displayed. The title bar is displayed by default. Use the TitleBar property described below to modify the contents and formatting of the title bar.
Spreadsheet.DisplayToolbar Controls whether the toolbar is displayed. The toolbar is displayed by default.
Spreadsheet.EnableAutoCalculate Controls how the spreadsheet model recalculates. If this property is set to False, the spreadsheet model will not automatically recalculate; you must call the Calculate method of the Worksheet object to see new results for changed inputs. This property can be useful if you plan to make many changes to a set of inputs and want to recalculate the model only when you're done with all the changes. By default, this property is True—models will automatically recalculate when changed.
Spreadsheet.ScreenUpdating By default, the screen display of the spreadsheet always reflects the most current data, but you can set this property to False if you plan to perform a number of operations and don't want the spreadsheet to flicker after each one. Setting this property back to True causes a full repaint.
Spreadsheet.Selection Returns the currently selected object. You can use the TypeName function in VBA or VBScript to determine what type of object it is.
Spreadsheet.TitleBar Gives you access to the Spreadsheet control's title bar, which you can change the text and formatting of.
Spreadsheet.ViewableRange Controls what part of the spreadsheet is actually visible. See the earlier discussions of ViewableRange and AutoFit for more details on how this works.
Sorting and Filtering Table 2-5 lists the properties and methods you will use when sorting and filtering data in the Spreadsheet component. Table 2-5. Properties and methods for sorting and filtering.
Property or Method Description
Range.Sort This method sorts the range given a column and sort direction.
Worksheet.AutoFilter This property returns the AutoFilter object that can be used to set up the details of a current filter.
AutoFilter.Filters This property returns the Filters collection for the current AutoFilter range. One Filter object applies to each column in the AutoFilter range, and the index of the Filter object matches the column index in the range.
AutoFilter.Apply This method applies a new AutoFilter. After you've set up the criteria, you must call this method to actually apply the filter.
Criteria.FilterFunction This property controls whether the criteria is included in the filter or excluded from it. Include filters include exactly the items in the criteria set, while exclude filters exclude the items in the criteria set but include everything else.
Criteria.ShowAll This property determines whether all data will be shown. When set to True, the property resets a filter to show all data. When set to False, assuming there are no filter criteria, it shows no data.
Criteria.Add This method is used to add new criteria to a filter.
Range.AutoFilter This method is used to turn AutoFilter on for a given range. Call this method first, and then use the Worksheet.AutoFilter property (described above) to access the filters and set up the criteria.
Protection If you want to protect part of the spreadsheet so that your users cannot modify cell contents or change cell formatting, you need to work with the properties that control protection. Table 2-6 lists the common protection properties and gives a brief description of how each is used. Note that the protection settings apply to user interactions through the user interface and to operations performed in code. If you want to delete rows while a protection option is enabled, you must set the Enabled property of the Protection object to False before performing the operation, and then set it back to True to return to the protected state. Table 2-6. Common protection properties.
Property Description
Worksheet.Protection Returns the Protection object for which you set the various protection options that enable global actions such as inserting or deleting rows.
Protection.Enabled Controls whether protection in general is enabled. To use the protection options or lock cells, first set the option or lock the cells and then set this property to True. You can set this property to False to temporarily disable protection while you perform operations in code.
Protection.AllowInsertingColumns, Protection.AllowInsertingRows, Protection.AllowDeletingColumns, Protection.AllowDeletingRows Enable or disable the ability to insert or delete columns or rows in the spreadsheet. For example, if AllowInsertingRows is set to False, the spreadsheet will disable all commands that can be used to insert a row, including those in the programming model.
Protection.AllowSizingAllColumns, Protection.AllowSizingAllRows Enable or disable the ability to resize columns or rows. For example, if AllowSizingAllRows is set to False, the spreadsheet won't allow the user to resize the rows, nor will it let you do so through code.
Protection.AllowSorting Allows or prohibits the sorting of data in the spreadsheet. Set AllowSorting to False to prohibit users from sorting the contents of any range.
Protection.AllowFiltering Allows or prohibits the use of the AutoFilter feature. Set this property to False to prohibit users from enabling the AutoFilter feature.
Undo Table 2-7 lists the relevant properties and methods you will use when controlling the Undo mechanism of the Spreadsheet component. Table 2-7. Properties and methods for controlling undo.
Property or Method Description
Spreadsheet.BeginUndo A method that enables you to treat a number of operations as one undo operation. For example, if you call BeginUndo and then perform three different sorts or change many cells, you can undo all these operations at once.
Spreadsheet.EndUndo A method that marks the end of your logical undo unit. All operations performed between the BeginUndo call and the EndUndo call will be undone as a single unit.
Spreadsheet.EnableUndo A property that controls whether the undo feature is available. By default, it is. You might want to temporarily disable this feature to save memory or perform a number of operations in code.
Useful Events A number of events are exposed from the top-level Spreadsheet object, more than from any other control in the OWC library. Table 2-8 lists several of the key events you likely will want to use when developing custom solutions around the Spreadsheet component. Nearly all the events in the Spreadsheet control pass a single parameter of type SpreadsheetEventInfo to the event handler. SpreadsheetEventInfo is a COM object that you can use to retrieve all kinds of information about the state of the application when the event was fired, including what was selected, what range was affected, where the mouse was, what keys were pressed, and so on. This mirrors the treatment of event information in the DOM. The biggest reason to use an object as the parameter is to support cancelable events in JavaScript. Parameters passed to an event in JavaScript are always passed by value unless they are object pointers. In other words, if the OWC team had designed the events with a ReturnValue parameter that the script set to True to cancel the event, it wouldn't work in JavaScript because of the parameter being passed by value. However, if the script sets an object's ReturnValue property to True, the control raising the event will see it. So if you want to cancel an event (most of the events whose names begin with "Before" can be canceled), set the ReturnValue property of the SpreadsheetEventInfo object to False. Table 2-8. Useful events.
Event Description
Spreadsheet.Change Fires any time a change is made to a cell or cells in the spreadsheet. Use the Range property of the SpreadsheetEventInfo object to determine the range affected.
StartEdit, EndEdit, CancelEdit Raised whenever a cell is about to be edited, was just edited, or just had its edit canceled. You can perform data validation in the EndEdit event and set the ReturnValue property of the SpreadsheetEventInfo object to True to deny the new value. Use the EditData property of the SpreadsheetEventInfo object to get the new value for the cell. Use the StartEdit event to replace a displayed value with another element for editing purposes, such as using a TrueType font for displaying a special symbol instead of a text description.
BeforeCommand, Command Raised just before and after a command—an action such as sorting; filtering; inserting or deleting rows or columns; showing help; and cutting, copying, or pasting—is processed. See the list of SheetCommandEnum constants in the Msowcvba.chm file or in your object browser for all the possible commands you can catch using these events. Again, set the ReturnValue property to False in the BeforeCommand event if you want to cancel the default behavior for an event. For example, you might want to show your own help page when the user clicks the Help button on the spreadsheet ?toolbar.
Retrieving Version Information Sometimes you need to find out the version of the control you're working with so that you can either take advantage of new features or use workaround code to solve problems in an older version. Most software programs have service releases between their major version releases, so you often need to verify that the version your code is talking to is indeed the version you expect. To help you do so, we added the properties listed in Table 2-9 to every control in the Office Web Components library. You can use them to determine the version of the control you're coding against and take the appropriate action. Table 2-9. Version information properties for all Office Web Components.
Property Description
MajorVersion A long integer value that indicates the major version number of the component. For the Office 2000 release, this number is 9.
MinorVersion A string-based value that indicates the minor version number of the component. For the Office 2000 release, this number is 0 and will be incremented if any minor releases occur before the next major version release. Note that MinorVersion is a string value in case an "a" release occurs. It's best to perform an equality comparison on this value rather than a greater-than or less-than comparison.
BuildNumber A string-based value that indicates the build number of the component. The build number is incremented with every build of the component DLL, and the value for the Office 2000 release wasn't yet available at the time of this writing. Again, this is a string value, so it can handle cases in which a letter is added to a version number in the event of a minor release.
Version A string-based value that returns the entire version number. Use the Version property when displaying the version, but use the other properties for determining whether the version is the one you want.

轉載于:https://www.cnblogs.com/88223100/archive/2008/11/27/1342424.html

總結

以上是生活随笔為你收集整理的[转载]Programming MS Office 2000 Web Components第二章第三节的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。