Skip to content

Commit

Permalink
Fix Issue #28 :
Browse files Browse the repository at this point in the history
- add a function vtkDefaultExcelExtension in vtkExcelUtilities module
- add a test for the new function
- divide vtkExcelUtilities in two version for 2003 and 2007
430 Tests OK in 177 seconds. No failure.
  • Loading branch information
Jean-Pierre Imbert committed Sep 6, 2014
1 parent 8d5d601 commit dfb005d
Show file tree
Hide file tree
Showing 5 changed files with 188 additions and 5 deletions.
6 changes: 3 additions & 3 deletions Project/VBAToolKit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -271,9 +271,9 @@
</module>
<module mID="m34">
<name>vtkExcelUtilities</name>
<modulePath confId="c1">Source\ConfProd\vtkExcelUtilities.bas</modulePath>
<modulePath confId="c2">Source\ConfProd\vtkExcelUtilities.bas</modulePath>
<modulePath confId="c3">Source\ConfProd\vtkExcelUtilities.bas</modulePath>
<modulePath confId="c1">Source\ConfProd\vtkExcelUtilities2007.bas</modulePath>
<modulePath confId="c2">Source\ConfProd\vtkExcelUtilities2007.bas</modulePath>
<modulePath confId="c3">Source\ConfProd\vtkExcelUtilities2003.bas</modulePath>
</module>
<module mID="m35">
<name>vtkProjectCreationUtilities</name>
Expand Down
160 changes: 160 additions & 0 deletions Source/ConfProd/vtkExcelUtilities2003.bas
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
Attribute VB_Name = "vtkExcelUtilities"
'---------------------------------------------------------------------------------------
' Module : vtkExcelUtilities
' Author : Jean-Pierre Imbert
' Date : 21/08/2013
' Purpose : Utilities for Excel file management
'
' Copyright 2013 Skwal-Soft (http://skwalsoft.com)
'
' Licensed under the Apache License, Version 2.0 (the "License");
' you may not use this file except in compliance with the License.
' You may obtain a copy of the License at
'
' http://www.apache.org/licenses/LICENSE-2.0
'
' Unless required by applicable law or agreed to in writing, software
' distributed under the License is distributed on an "AS IS" BASIS,
' WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
' See the License for the specific language governing permissions and
' limitations under the License.
'---------------------------------------------------------------------------------------

Option Explicit
'---------------------------------------------------------------------------------------
' Procedure : vtkCreateExcelWorkbook
' Author : Jean-Pierre Imbert
' Date : 25/05/2013
' Purpose : Utility function for Excel file creation
'---------------------------------------------------------------------------------------
'
Public Function vtkCreateExcelWorkbook() As Workbook
Dim Wb As Workbook
Set Wb = Workbooks.Add(xlWBATWorksheet)
Set vtkCreateExcelWorkbook = Wb
End Function

'---------------------------------------------------------------------------------------
' Procedure : vtkCreateExcelWorkbookForTestWithProjectName
' Author : Jean-Pierre Imbert
' Date : 25/05/2013
' Purpose : Utility function for Excel file creation for Test and VBA project name initialization
'---------------------------------------------------------------------------------------
'
Public Function vtkCreateExcelWorkbookForTestWithProjectName(projectName As String) As Workbook
Dim Wb As Workbook
Set Wb = vtkCreateExcelWorkbookWithPathAndName(vtkPathToTestFolder, vtkProjectForName(projectName).workbookDEVName)
Wb.VBProject.name = projectName
Set vtkCreateExcelWorkbookForTestWithProjectName = Wb
End Function

'---------------------------------------------------------------------------------------
' Procedure : vtkCreateExcelWorkbookWithPathAndName
' Author : Jean-Pierre Imbert
' Date : 08/06/2013
' Purpose : Create a New Excel File and save it on the given path with the given name
'---------------------------------------------------------------------------------------
'
Public Function vtkCreateExcelWorkbookWithPathAndName(path As String, name As String) As Workbook
Dim Wb As Workbook
Set Wb = vtkCreateExcelWorkbook
Wb.SaveAs fileName:=path & "\" & name, FileFormat:=xlOpenXMLWorkbookMacroEnabled
Set vtkCreateExcelWorkbookWithPathAndName = Wb
End Function

'---------------------------------------------------------------------------------------
' Procedure : vtkCloseAndKillWorkbook
' Author : Jean-Pierre Imbert
' Date : 08/06/2013
' Purpose : Close the given workbook then kill the Excel File
'---------------------------------------------------------------------------------------
'
Public Sub vtkCloseAndKillWorkbook(Wb As Workbook)
Dim fullPath As String
fullPath = Wb.FullName
Wb.Close saveChanges:=False
Kill PathName:=fullPath
End Sub

'---------------------------------------------------------------------------------------
' Procedure : VtkWorkbookIsOpen
' Author : Abdelfattah Lahbib
' Date : 24/04/2013
' Purpose : return true if the workbook is already opened
'---------------------------------------------------------------------------------------
'
Public Function VtkWorkbookIsOpen(workbookName As String) As Boolean
On Error Resume Next
Workbooks(workbookName).Activate 'if we have a problem to activate workbook = the workbook is closed , if we can activate it without problem = the workbook is open
VtkWorkbookIsOpen = (Err = 0)
End Function

'---------------------------------------------------------------------------------------
' Procedure : vtkDefaultFileFormat
' Author : Jean-Pierre Imbert
' Date : 09/08/2013
' Purpose : return the default FileFormat of an Excel file given its full path
' - xlOpenXMLWorkbookMacroEnabled for .xlsm
' - xlOpenXMLAddIn for .xlam
' - 0 if the extension is unknown
'---------------------------------------------------------------------------------------
'
Public Function vtkDefaultFileFormat(filePath As String) As XlFileFormat
Select Case vtkGetFileExtension(filePath)
Case "xla"
vtkDefaultFileFormat = xlAddIn
Case "xls"
vtkDefaultFileFormat = xlWorkbookNormal
Case Else
vtkDefaultFileFormat = 0
End Select
End Function

'---------------------------------------------------------------------------------------
' Function : vtkDefaultExcelExtension as String
' Author : Jean-Pierre Imbert
' Date : 06/09/2014
' Purpose : return the default extension for an Excel file
' - .xls for Excel until 2003 (11.0)
' - .xlsm for Excel since 2007 (12.0)
'---------------------------------------------------------------------------------------
'
Public Function vtkDefaultExcelExtension() As String
If Val(Application.Version) >= 12 Then
vtkDefaultExcelExtension = ".xlsm"
Else
vtkDefaultExcelExtension = ".xls"
End If
End Function

'---------------------------------------------------------------------------------------
' Function : vtkDefaultIsAddIn as Boolean
' Author : Jean-Pierre Imbert
' Date : 11/11/2013
' Purpose : return True if the default File Format is an Excel Add-In
' - currenttly is add-in if extension is .xla or .xlam
'---------------------------------------------------------------------------------------
'
Public Function vtkDefaultIsAddIn(filePath As String) As Boolean
vtkDefaultIsAddIn = (vtkGetFileExtension(filePath) = "xlam") Or (vtkGetFileExtension(filePath) = "xla")
End Function

'---------------------------------------------------------------------------------------
' Function : vtkReferencesInWorkbook as Collection of vtkReferences
' Author : Jean-Pierre Imbert
' Date : 23/06/2014
' Purpose : return a collection of references of the workbook given as a parameter
' - the ID property of each vtkReference instance of this collection is not initialized
' - the collection is indexed by the name of the references
' NOTE : this function is not tested
'---------------------------------------------------------------------------------------
'
Public Function vtkReferencesInWorkbook(Wb As Workbook) As Collection
Dim c As New Collection, ref As vtkReference, r As VBIDE.Reference
For Each r In Wb.VBProject.references
Set ref = New vtkReference
ref.initWithVBAReference r
c.Add Item:=ref, Key:=ref.name
Next
Set vtkReferencesInWorkbook = c
End Function
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,23 @@ Public Function vtkDefaultFileFormat(filePath As String) As XlFileFormat
End Select
End Function

'---------------------------------------------------------------------------------------
' Function : vtkDefaultExcelExtension as String
' Author : Jean-Pierre Imbert
' Date : 06/09/2014
' Purpose : return the default extension for an Excel file
' - .xls for Excel until 2003 (11.0)
' - .xlsm for Excel since 2007 (12.0)
'---------------------------------------------------------------------------------------
'
Public Function vtkDefaultExcelExtension() As String
If Val(Application.Version) >= 12 Then
vtkDefaultExcelExtension = ".xlsm"
Else
vtkDefaultExcelExtension = ".xls"
End If
End Function

'---------------------------------------------------------------------------------------
' Function : vtkDefaultIsAddIn as Boolean
' Author : Jean-Pierre Imbert
Expand Down
4 changes: 2 additions & 2 deletions Source/ConfProd/vtkProject.cls
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ End Property
'
Public Property Get workbookDEVName() As String
If initialized Then
workbookDEVName = projectDEVName & ".xlsm"
workbookDEVName = projectDEVName & vtkDefaultExcelExtension
Else
workbookDEVName = ""
End If
Expand All @@ -149,7 +149,7 @@ End Property
'
Public Property Get workbookName() As String
If initialized Then
workbookName = projectName & ".xlsm"
workbookName = projectName & vtkDefaultExcelExtension
Else
workbookName = ""
End If
Expand Down
6 changes: 6 additions & 0 deletions Source/ConfTest/vtkExcelUtilitiesTester.cls
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,20 @@ Public Sub Test_vtkDefaultIsAddIn()
mAssert.Should Not vtkDefaultIsAddIn("TestFile.xls"), "xls extension is not Add-In"
End Sub

Public Sub Test_vtkDefaultExcelExtension()
mAssert.Equals ".xlsm", vtkDefaultExcelExtension, "Default Excel 2007 extension"
End Sub

Private Function ITest_Suite() As TestSuite
Set ITest_Suite = New TestSuite
ITest_Suite.AddTest ITest_Manager.ClassName, "Test_vtkDefaultIsAddIn"
ITest_Suite.AddTest ITest_Manager.ClassName, "Test_vtkDefaultExcelExtension"
End Function

Private Sub ITestCase_RunTest()
Select Case mManager.methodName
Case "Test_vtkDefaultIsAddIn": Test_vtkDefaultIsAddIn
Case "Test_vtkDefaultExcelExtension": Test_vtkDefaultExcelExtension
Case Else: mAssert.Should False, "Invalid test name: " & mManager.methodName
End Select
End Sub
Expand Down

0 comments on commit dfb005d

Please sign in to comment.