diff --git a/_0Main.vb b/_0Main.vb index cf3b5dd..d518850 100644 --- a/_0Main.vb +++ b/_0Main.vb @@ -6,18 +6,45 @@ Option Explicit ' subMain: The main program Sub subMain BasicLibraries.loadLibrary "XrayTool" - Dim dStart As Date - dStart = Now - 'MsgBox InStr (1, "abca", "ad") - 'Xray ThisComponent.getSheets.getByIndex (0).getCellByPosition (0, 0) + subRunCorrelation + 'subRunPairedTTest + 'subRunIndependentTTest + 'subRunAnova + 'subRunChi2GoodnessOfFit 'subTestCorrelation - subTestChi2GoodnessOfFit + 'subTestPairedTTest + 'subTestIndependentTTest + 'subTestANOVA + 'subTestChi2GoodnessOfFit - MsgBox "Done. " & Format (Now - dStart, "mm:ss") & " elapsed." End Sub -' fnQueryFormat: Returns the index of the number format, and creates the number format if required. +' fnCheckRangeName: Checks the range name and returns the range when +' found, or null when not found. +Function fnCheckRangeName (oDoc As Object, sRangeName As String) As Object + On Error Goto ErrorHandler + Dim oController As Object, oSheet As Object + Dim nPos As Integer, sSheetName As String, oRange As Object + + oController = oDoc.getCurrentController + nPos = InStr (sRangeName, ".") + If nPos = 0 Then + oSheet = oController.getActiveSheet + Else + sSheetName = Left (sRangeName, nPos - 1) + If Left (sSheetName, 1) = "$" Then + sSheetName = Right (sSheetName, Len (sSheetName) - 1) + End If + oSheet = oDoc.getSheets.getByName (sSheetName) + End If + fnCheckRangeName = oSheet.getCellRangeByName (sRangeName) + + ErrorHandler: +End Function + +' fnQueryFormat: Returns the index of the number format, and creates +' the number format if required. Function fnQueryFormat (oDoc As Object, sFormat As String) As Integer Dim oFormats As Object, nIndex As Integer Dim aLocale As New com.sun.star.lang.Locale @@ -58,17 +85,202 @@ End Function ' fnFindStatsTestDocument: Finds the statistics test document. Function fnFindStatsTestDocument As Object - Dim oEnum As Object, oDoc As Object + Dim oEnum As Object, oDoc As Object, sFile As String + sFile = "/statstest.ods" oEnum = StarDesktop.getComponents.createEnumeration Do While oEnum.hasMoreElements oDoc = oEnum.nextElement If oDoc.supportsService ("com.sun.star.document.OfficeDocument") Then - If Right (oDoc.getLocation, Len ("/statstest.ods")) = "/statstest.ods" Then + If Right (oDoc.getLocation, Len (sFile)) = sFile Then fnFindStatsTestDocument = oDoc Exit Function End If End If Loop - fnFindStatsTestDocument = Null +End Function + +' fnAskDataRange: Asks the user for the data range, or null when +' the user cancelled +Function fnAskDataRange (oDoc As Object) As Object + Dim oRange As Object, sPrompt As String, sCellsData As String + + oRange = fnFindActiveDataRange (oDoc) + If IsNull (oRange) Then + sCellsData = "" + Else + sCellsData = oRange.getPropertyValue ("AbsoluteName") + End If + sPrompt = "Cells with the data:" + + ' Loop until we get good answer + Do While sPrompt <> "" + sCellsData = InputBox (sPrompt, "Step 1/2: Select the data range", sCellsData) + + ' Cancelled + If sCellsData = "" Then + Exit Function + End If + + oRange = fnCheckRangeName (oDoc, sCellsData) + If IsNull (oRange) Then + sPrompt = "The range """ & sCellsData & """ does not exist." + Else + If oRange.getRows.getCount < 2 Or oRange.getColumns.getCount < 2 Then + sPrompt = "The range """ & sCellsData & """ is too small (at least 2×2)." + Else + sPrompt = "" + oDoc.getCurrentController.select (oRange) + fnAskDataRange = oRange + Exit Function + End If + End If + Loop +End Function + +' fnAskDataRange2: Asks the user for the data range, or null when +' the user cancelled +Function fnAskDataRange2 (oDoc As Object) As Object + Dim oRange As Object + Dim oDialogModel As Object, oDialog As Object, nResult As Integer + Dim oTextModel As Object, oEditModel As Object + Dim oButtonModel As Object + Dim sPrompt As String, sCellsData As String + + oRange = fnFindActiveDataRange (oDoc) + If IsNull (oRange) Then + sCellsData = "" + Else + sCellsData = oRange.getPropertyValue ("AbsoluteName") + End If + sPrompt = "Cells with the data:" + + ' Loop until we finds good data + Do While sPrompt <> "" + ' Creates a dialog + oDialogModel = CreateUnoService ( _ + "com.sun.star.awt.UnoControlDialogModel") + oDialogModel.setPropertyValue ("PositionX", 200) + oDialogModel.setPropertyValue ("PositionY", 200) + oDialogModel.setPropertyValue ("Height", 65) + oDialogModel.setPropertyValue ("Width", 95) + oDialogModel.setPropertyValue ("Title", "Step 1/2: Select the data range") + + ' Adds the prompt. + oTextModel = oDialogModel.createInstance ( _ + "com.sun.star.awt.UnoControlFixedTextModel") + oTextModel.setPropertyValue ("PositionX", 5) + oTextModel.setPropertyValue ("PositionY", 5) + oTextModel.setPropertyValue ("Height", 15) + oTextModel.setPropertyValue ("Width", 85) + oTextModel.setPropertyValue ("Label", sPrompt) + oTextModel.setPropertyValue ("MultiLine", True) + oTextModel.setPropertyValue ("TabIndex", 1) + oDialogModel.insertByName ("txtPrompt", oTextModel) + + ' Adds the text input. + oEditModel = oDialogModel.createInstance ( _ + "com.sun.star.awt.UnoControlEditModel") + oEditModel.setPropertyValue ("PositionX", 5) + oEditModel.setPropertyValue ("PositionY", 25) + oEditModel.setPropertyValue ("Height", 15) + oEditModel.setPropertyValue ("Width", 85) + oEditModel.setPropertyValue ("Text", sCellsData) + oDialogModel.insertByName ("edtCellsData", oEditModel) + + ' Adds the buttons. + oButtonModel = oDialogModel.createInstance ( _ + "com.sun.star.awt.UnoControlButtonModel") + oButtonModel.setPropertyValue ("PositionX", 5) + oButtonModel.setPropertyValue ("PositionY", 45) + oButtonModel.setPropertyValue ("Height", 15) + oButtonModel.setPropertyValue ("Width", 40) + oButtonModel.setPropertyValue ("PushButtonType", _ + com.sun.star.awt.PushButtonType.CANCEL) + oDialogModel.insertByName ("btnClose", oButtonModel) + + oButtonModel = oDialogModel.createInstance ( _ + "com.sun.star.awt.UnoControlButtonModel") + oButtonModel.setPropertyValue ("PositionX", 50) + oButtonModel.setPropertyValue ("PositionY", 45) + oButtonModel.setPropertyValue ("Height", 15) + oButtonModel.setPropertyValue ("Width", 40) + oButtonModel.setPropertyValue ("PushButtonType", _ + com.sun.star.awt.PushButtonType.OK) + oDialogModel.insertByName ("btnOK", oButtonModel) + + ' Adds the dialog model to the control and runs it. + oDialog = CreateUnoService ("com.sun.star.awt.UnoControlDialog") + oDialog.setModel (oDialogModel) + oDialog.setVisible (True) + nResult = oDialog.execute + oDialog.dispose + + ' Cancelled + If nResult = 0 Then + Exit Function + End If + + sCellsData = oEditModel.getPropertyValue ("Text") + If sCellsData = "" Then + sPrompt = "Cells with the data:" + Else + oRange = fnCheckRangeName (oDoc, sCellsData) + If IsNull (oRange) Then + sPrompt = "The range """ & sCellsData & """ does not exist." + Else + If oRange.getRows.getCount < 2 Or oRange.getColumns.getCount < 2 Then + sPrompt = "The range """ & sCellsData & """ is too small (at least 2×2)." + Else + sPrompt = "" + oDoc.getCurrentController.select (oRange) + fnAskDataRange = oRange + Exit Function + End If + End If + End If + Loop +End Function + +' fnFindActiveDataRange: Finds the selected data range. +Function fnFindActiveDataRange (oDoc) + Dim oSelection As Object, nI As Integer + Dim oRanges As Object, oRange As Object + Dim aCellAddress As New com.sun.star.table.CellAddress + Dim aRangeAddress As New com.sun.star.table.CellRangeAddress + + oSelection = oDoc.getCurrentSelection + + ' Some data ranges are already selected. + If Not oSelection.supportsService ("com.sun.star.sheet.SheetCell") Then + ' Takes the first selection in multiple selections + If oSelection.supportsService ("com.sun.star.sheet.SheetCellRanges") Then + fnFindActiveDataRange = oSelection.getByIndex (0) + ' The only selection + Else + fnFindActiveDataRange = oSelection + End If + Exit Function + End If + + ' Finds the data range containing the single active cell + aCellAddress = oSelection.getCellAddress + oRanges = oSelection.getSpreadsheet.queryContentCells ( _ + com.sun.star.sheet.CellFlags.VALUE _ + + com.sun.star.sheet.CellFlags.DATETIME _ + + com.sun.star.sheet.CellFlags.STRING _ + + com.sun.star.sheet.CellFlags.FORMULA) + For nI = 0 To oRanges.getCount - 1 + oRange = oRanges.getByIndex (nI) + aRangeAddress = oRange.getRangeAddress + If aRangeAddress.StartRow <= aCellAddress.Row _ + And aRangeAddress.EndRow >= aCellAddress.Row _ + And aRangeAddress.StartColumn <= aCellAddress.Column _ + And aRangeAddress.EndColumn >= aCellAddress.Column Then + oDoc.getCurrentController.select (oRange) + fnFindActiveDataRange = oRange + Exit Function + End If + Next nI + ' Not in a data cell range End Function diff --git a/_1CorRel.vb b/_1CorRel.vb index 6e01ef6..a630cac 100644 --- a/_1CorRel.vb +++ b/_1CorRel.vb @@ -3,13 +3,167 @@ Option Explicit +' subRunCorrelation: Runs the Pearson’s correlation coefficient. +Sub subRunCorrelation As Object + Dim oRange As Object + Dim mLabels () As String, nI As Integer, mSelected (0) As Integer + Dim oDialogModel As Object, oDialog As Object, nResult As Integer + Dim oTextModel As Object, oListModel1 As Object, oListModel2 As Object + Dim oButtonModel As Object + Dim nColumn As Integer, oRange1 As Object, oRange2 As Object + Dim oSheets As Object, sSheetName As String, sExisted As String + Dim oSheet As Object + + ' Asks the user for the data range + oRange = fnAskDataRange (ThisComponent) + If IsNull (oRange) Then + Exit Sub + End If + ReDim mLabels (oRange.getColumns.getCount - 1) As String + For nI = 0 To oRange.getColumns.getCount - 1 + mLabels (nI) = oRange.getCellByPosition (nI, 0).getString + Next nI + + ' Creates a dialog + oDialogModel = CreateUnoService ( _ + "com.sun.star.awt.UnoControlDialogModel") + oDialogModel.setPropertyValue ("PositionX", 200) + oDialogModel.setPropertyValue ("PositionY", 200) + oDialogModel.setPropertyValue ("Height", 80) + oDialogModel.setPropertyValue ("Width", 95) + oDialogModel.setPropertyValue ("Title", "Step 2/2: Specify the data") + + ' Adds the prompt. + oTextModel = oDialogModel.createInstance ( _ + "com.sun.star.awt.UnoControlFixedTextModel") + oTextModel.setPropertyValue ("PositionX", 5) + oTextModel.setPropertyValue ("PositionY", 5) + oTextModel.setPropertyValue ("Height", 10) + oTextModel.setPropertyValue ("Width", 85) + oTextModel.setPropertyValue ("Label", "First score column:") + oTextModel.setPropertyValue ("MultiLine", True) + oTextModel.setPropertyValue ("TabIndex", 1) + oDialogModel.insertByName ("txtPromptGroup", oTextModel) + + ' Adds the drop down list + oListModel1 = oDialogModel.createInstance ( _ + "com.sun.star.awt.UnoControlListBoxModel") + oListModel1.setPropertyValue ("PositionX", 5) + oListModel1.setPropertyValue ("PositionY", 15) + oListModel1.setPropertyValue ("Height", 10) + oListModel1.setPropertyValue ("Width", 85) + oListModel1.setPropertyValue ("Dropdown", True) + oListModel1.setPropertyValue ("StringItemList", mLabels) + mSelected (0) = 0 + oListModel1.setPropertyValue ("SelectedItems", mSelected) + oDialogModel.insertByName ("lstGroup", oListModel1) + + ' Adds the prompt. + oTextModel = oDialogModel.createInstance ( _ + "com.sun.star.awt.UnoControlFixedTextModel") + oTextModel.setPropertyValue ("PositionX", 5) + oTextModel.setPropertyValue ("PositionY", 30) + oTextModel.setPropertyValue ("Height", 10) + oTextModel.setPropertyValue ("Width", 85) + oTextModel.setPropertyValue ("Label", "Second score column:") + oTextModel.setPropertyValue ("MultiLine", True) + oTextModel.setPropertyValue ("TabIndex", 1) + oDialogModel.insertByName ("txtPromptScore", oTextModel) + + ' Adds the drop down list + oListModel2 = oDialogModel.createInstance ( _ + "com.sun.star.awt.UnoControlListBoxModel") + oListModel2.setPropertyValue ("PositionX", 5) + oListModel2.setPropertyValue ("PositionY", 40) + oListModel2.setPropertyValue ("Height", 10) + oListModel2.setPropertyValue ("Width", 85) + oListModel2.setPropertyValue ("Dropdown", True) + oListModel2.setPropertyValue ("StringItemList", mLabels) + mSelected (0) = 1 + oListModel2.setPropertyValue ("SelectedItems", mSelected) + oDialogModel.insertByName ("lstScore", oListModel2) + + ' Adds the buttons. + oButtonModel = oDialogModel.createInstance ( _ + "com.sun.star.awt.UnoControlButtonModel") + oButtonModel.setPropertyValue ("PositionX", 5) + oButtonModel.setPropertyValue ("PositionY", 60) + oButtonModel.setPropertyValue ("Height", 15) + oButtonModel.setPropertyValue ("Width", 40) + oButtonModel.setPropertyValue ("PushButtonType", _ + com.sun.star.awt.PushButtonType.CANCEL) + oDialogModel.insertByName ("btnClose", oButtonModel) + + oButtonModel = oDialogModel.createInstance ( _ + "com.sun.star.awt.UnoControlButtonModel") + oButtonModel.setPropertyValue ("PositionX", 50) + oButtonModel.setPropertyValue ("PositionY", 60) + oButtonModel.setPropertyValue ("Height", 15) + oButtonModel.setPropertyValue ("Width", 40) + oButtonModel.setPropertyValue ("PushButtonType", _ + com.sun.star.awt.PushButtonType.OK) + oButtonModel.setPropertyValue ("DefaultButton", True) + oDialogModel.insertByName ("btnOK", oButtonModel) + + ' Adds the dialog model to the control and runs it. + oDialog = CreateUnoService ("com.sun.star.awt.UnoControlDialog") + oDialog.setModel (oDialogModel) + oDialog.setVisible (True) + nResult = oDialog.execute + oDialog.dispose + + ' Cancelled + If nResult = 0 Then + Exit Sub + End If + + nColumn = oListModel1.getPropertyValue ("SelectedItems") (0) + oRange1 = oRange.getCellRangeByPosition ( _ + nColumn, 0, nColumn, oRange.getRows.getCount - 1) + nColumn = oListModel2.getPropertyValue ("SelectedItems") (0) + oRange2 = oRange.getCellRangeByPosition ( _ + nColumn, 0, nColumn, oRange.getRows.getCount - 1) + + ' Checks the existing report + oSheets = ThisComponent.getSheets + sSheetName = oRange1.getSpreadsheet.getName + sExisted = "" + If oSheets.hasByName (sSheetName & "_correl") Then + sExisted = sExisted & ", """ & sSheetName & "_correl""" + End If + If sExisted <> "" Then + sExisted = Right (sExisted, Len (sExisted) - 2) + If InStr (sExisted, ",") > 0 Then + sExisted = "Spreadsheets " & sExisted & " exist. Overwrite?" + Else + sExisted = "Spreadsheet " & sExisted & " exists. Overwrite?" + End If + nResult = MsgBox(sExisted, MB_YESNO + MB_DEFBUTTON2 + MB_ICONQUESTION) + If nResult = IDNO Then + Exit Sub + End If + + ' Drops the existing report + If oSheets.hasByName (sSheetName & "_correl") Then + oSheets.removeByname (sSheetName & "_correl") + End If + End If + + ' Reports the paired T-test. + subReportCorrelation (ThisComponent, oRange1, oRange2) + + ' Makes the report sheet active. + oSheet = oSheets.getByName (sSheetName & "_correl") + ThisComponent.getCurrentController.setActiveSheet (oSheet) +End Sub + ' subTestCorrelation: Tests the Pearson’s correlation coefficient report Sub subTestCorrelation Dim oDoc As Object, oSheets As Object, sSheetName As String - Dim oSheet As Object, oRange As Object + Dim oSheet As Object, oXRange As Object, oYRange As Object oDoc = fnFindStatsTestDocument - If oDoc = Null Then + If IsNull (oDoc) Then MsgBox "Cannot find statstest.ods in the opened documents." Exit Sub End If @@ -24,12 +178,13 @@ Sub subTestCorrelation oSheets.removeByName (sSheetName & "_correl") End If oSheet = oSheets.getByName (sSheetName) - oRange = oSheet.getCellRangeByName ("B3:C13") - subReportCorrelation (oDoc, oRange) + oXRange = oSheet.getCellRangeByName ("B3:B13") + oYRange = oSheet.getCellRangeByName ("C3:C13") + subReportCorrelation (oDoc, oXRange, oYRange) End Sub ' subReportCorrelation: Reports the Pearson’s correlation coefficient -Sub subReportCorrelation (oDoc As Object, oDataRange As Object) +Sub subReportCorrelation (oDoc As Object, oDataXRange As Object, oDataYRange As Object) Dim oSheets As Object, sSheetName As String Dim mNames () As String, nI As Integer, nSheetIndex As Integer Dim oSheet As Object, oColumns As Object, nRow As Integer @@ -44,7 +199,7 @@ Sub subReportCorrelation (oDoc As Object, oDataRange As Object) Dim sCellN As String, sCellR As String oSheets = oDoc.getSheets - sSheetName = oDataRange.getSpreadsheet.getName + sSheetName = oDataXRange.getSpreadsheet.getName mNames = oSheets.getElementNames For nI = 0 To UBound (mNames) If mNames (nI) = sSheetName Then @@ -54,11 +209,11 @@ Sub subReportCorrelation (oDoc As Object, oDataRange As Object) oSheets.insertNewByName (sSheetName & "_correl", nSheetIndex + 1) oSheet = oSheets.getByName (sSheetName & "_correl") - nN = oDataRange.getRows.getCount - 1 - sCellXLabel = fnGetRangeName (oDataRange.getCellByPosition (0, 0)) - sCellsXData = fnGetRangeName (oDataRange.getCellRangeByPosition (0, 1, 0, nN)) - sCellYLabel = fnGetRangeName (oDataRange.getCellByPosition (1, 0)) - sCellsYData = fnGetRangeName (oDataRange.getCellRangeByPosition (1, 1, 1, nN)) + nN = oDataXRange.getRows.getCount - 1 + sCellXLabel = fnGetRangeName (oDataXRange.getCellByPosition (0, 0)) + sCellsXData = fnGetRangeName (oDataXRange.getCellRangeByPosition (0, 1, 0, nN)) + sCellYLabel = fnGetRangeName (oDataYRange.getCellByPosition (0, 0)) + sCellsYData = fnGetRangeName (oDataYRange.getCellRangeByPosition (0, 1, 0, nN)) ' Obtains the format parameters for the report. nFormatN = fnQueryFormat (oDoc, "#,##0") diff --git a/_2PTTest.vb b/_2PTTest.vb index 4e528c3..5219f24 100644 --- a/_2PTTest.vb +++ b/_2PTTest.vb @@ -1,14 +1,169 @@ ' _2PTTest: The macros to for generating the report of paired T-Test ' by imacat , 2016-08-11 + Option Explicit +' subRunPairedTTest: Runs the paired T-test. +Sub subRunPairedTTest As Object + Dim oRange As Object + Dim mLabels () As String, nI As Integer, mSelected (0) As Integer + Dim oDialogModel As Object, oDialog As Object, nResult As Integer + Dim oTextModel As Object, oListModel1 As Object, oListModel2 As Object + Dim oButtonModel As Object + Dim nColumn As Integer, oRange1 As Object, oRange2 As Object + Dim oSheets As Object, sSheetName As String, sExisted As String + Dim oSheet As Object + + ' Asks the user for the data range + oRange = fnAskDataRange (ThisComponent) + If IsNull (oRange) Then + Exit Sub + End If + ReDim mLabels (oRange.getColumns.getCount - 1) As String + For nI = 0 To oRange.getColumns.getCount - 1 + mLabels (nI) = oRange.getCellByPosition (nI, 0).getString + Next nI + + ' Creates a dialog + oDialogModel = CreateUnoService ( _ + "com.sun.star.awt.UnoControlDialogModel") + oDialogModel.setPropertyValue ("PositionX", 200) + oDialogModel.setPropertyValue ("PositionY", 200) + oDialogModel.setPropertyValue ("Height", 80) + oDialogModel.setPropertyValue ("Width", 95) + oDialogModel.setPropertyValue ("Title", "Step 2/2: Specify the data") + + ' Adds the prompt. + oTextModel = oDialogModel.createInstance ( _ + "com.sun.star.awt.UnoControlFixedTextModel") + oTextModel.setPropertyValue ("PositionX", 5) + oTextModel.setPropertyValue ("PositionY", 5) + oTextModel.setPropertyValue ("Height", 10) + oTextModel.setPropertyValue ("Width", 85) + oTextModel.setPropertyValue ("Label", "First score column:") + oTextModel.setPropertyValue ("MultiLine", True) + oTextModel.setPropertyValue ("TabIndex", 1) + oDialogModel.insertByName ("txtPromptGroup", oTextModel) + + ' Adds the drop down list + oListModel1 = oDialogModel.createInstance ( _ + "com.sun.star.awt.UnoControlListBoxModel") + oListModel1.setPropertyValue ("PositionX", 5) + oListModel1.setPropertyValue ("PositionY", 15) + oListModel1.setPropertyValue ("Height", 10) + oListModel1.setPropertyValue ("Width", 85) + oListModel1.setPropertyValue ("Dropdown", True) + oListModel1.setPropertyValue ("StringItemList", mLabels) + mSelected (0) = 0 + oListModel1.setPropertyValue ("SelectedItems", mSelected) + oDialogModel.insertByName ("lstGroup", oListModel1) + + ' Adds the prompt. + oTextModel = oDialogModel.createInstance ( _ + "com.sun.star.awt.UnoControlFixedTextModel") + oTextModel.setPropertyValue ("PositionX", 5) + oTextModel.setPropertyValue ("PositionY", 30) + oTextModel.setPropertyValue ("Height", 10) + oTextModel.setPropertyValue ("Width", 85) + oTextModel.setPropertyValue ("Label", "Second score column:") + oTextModel.setPropertyValue ("MultiLine", True) + oTextModel.setPropertyValue ("TabIndex", 1) + oDialogModel.insertByName ("txtPromptScore", oTextModel) + + ' Adds the drop down list + oListModel2 = oDialogModel.createInstance ( _ + "com.sun.star.awt.UnoControlListBoxModel") + oListModel2.setPropertyValue ("PositionX", 5) + oListModel2.setPropertyValue ("PositionY", 40) + oListModel2.setPropertyValue ("Height", 10) + oListModel2.setPropertyValue ("Width", 85) + oListModel2.setPropertyValue ("Dropdown", True) + oListModel2.setPropertyValue ("StringItemList", mLabels) + mSelected (0) = 1 + oListModel2.setPropertyValue ("SelectedItems", mSelected) + oDialogModel.insertByName ("lstScore", oListModel2) + + ' Adds the buttons. + oButtonModel = oDialogModel.createInstance ( _ + "com.sun.star.awt.UnoControlButtonModel") + oButtonModel.setPropertyValue ("PositionX", 5) + oButtonModel.setPropertyValue ("PositionY", 60) + oButtonModel.setPropertyValue ("Height", 15) + oButtonModel.setPropertyValue ("Width", 40) + oButtonModel.setPropertyValue ("PushButtonType", _ + com.sun.star.awt.PushButtonType.CANCEL) + oDialogModel.insertByName ("btnClose", oButtonModel) + + oButtonModel = oDialogModel.createInstance ( _ + "com.sun.star.awt.UnoControlButtonModel") + oButtonModel.setPropertyValue ("PositionX", 50) + oButtonModel.setPropertyValue ("PositionY", 60) + oButtonModel.setPropertyValue ("Height", 15) + oButtonModel.setPropertyValue ("Width", 40) + oButtonModel.setPropertyValue ("PushButtonType", _ + com.sun.star.awt.PushButtonType.OK) + oButtonModel.setPropertyValue ("DefaultButton", True) + oDialogModel.insertByName ("btnOK", oButtonModel) + + ' Adds the dialog model to the control and runs it. + oDialog = CreateUnoService ("com.sun.star.awt.UnoControlDialog") + oDialog.setModel (oDialogModel) + oDialog.setVisible (True) + nResult = oDialog.execute + oDialog.dispose + + ' Cancelled + If nResult = 0 Then + Exit Sub + End If + + nColumn = oListModel1.getPropertyValue ("SelectedItems") (0) + oRange1 = oRange.getCellRangeByPosition ( _ + nColumn, 0, nColumn, oRange.getRows.getCount - 1) + nColumn = oListModel2.getPropertyValue ("SelectedItems") (0) + oRange2 = oRange.getCellRangeByPosition ( _ + nColumn, 0, nColumn, oRange.getRows.getCount - 1) + + ' Checks the existing report + oSheets = ThisComponent.getSheets + sSheetName = oRange1.getSpreadsheet.getName + sExisted = "" + If oSheets.hasByName (sSheetName & "_ttest") Then + sExisted = sExisted & ", """ & sSheetName & "_ttest""" + End If + If sExisted <> "" Then + sExisted = Right (sExisted, Len (sExisted) - 2) + If InStr (sExisted, ",") > 0 Then + sExisted = "Spreadsheets " & sExisted & " exist. Overwrite?" + Else + sExisted = "Spreadsheet " & sExisted & " exists. Overwrite?" + End If + nResult = MsgBox(sExisted, MB_YESNO + MB_DEFBUTTON2 + MB_ICONQUESTION) + If nResult = IDNO Then + Exit Sub + End If + + ' Drops the existing report + If oSheets.hasByName (sSheetName & "_ttest") Then + oSheets.removeByname (sSheetName & "_ttest") + End If + End If + + ' Reports the paired T-test. + subReportPairedTTest (ThisComponent, oRange1, oRange2) + + ' Makes the report sheet active. + oSheet = oSheets.getByName (sSheetName & "_ttest") + ThisComponent.getCurrentController.setActiveSheet (oSheet) +End Sub + ' subTestPairedTTest: Tests the paired T-test report Sub subTestPairedTTest Dim oDoc As Object, oSheets As Object, sSheetName As String - Dim oSheet As Object, oRange As Object + Dim oSheet As Object, oXRange As Object, oYRange As Object oDoc = fnFindStatsTestDocument - If oDoc = Null Then + If IsNull (oDoc) Then MsgBox "Cannot find statstest.ods in the opened documents." Exit Sub End If @@ -23,12 +178,13 @@ Sub subTestPairedTTest oSheets.removeByName (sSheetName & "_ttest") End If oSheet = oSheets.getByName (sSheetName) - oRange = oSheet.getCellRangeByName ("B3:C15") - subReportPairedTTest (oDoc, oRange) + oXRange = oSheet.getCellRangeByName ("B3:B15") + oYRange = oSheet.getCellRangeByName ("C3:C15") + subReportPairedTTest (oDoc, oXRange, oYRange) End Sub ' subReportPairedTTest: Reports the paired T-test -Sub subReportPairedTTest (oDoc As Object, oDataRange As Object) +Sub subReportPairedTTest (oDoc As Object, oDataXRange As Object, oDataYRange As Object) Dim oSheets As Object, sSheetName As String Dim mNames () As String, nI As Integer, nSheetIndex As Integer Dim oSheet As Object, oColumns As Object, nRow As Integer @@ -45,7 +201,7 @@ Sub subReportPairedTTest (oDoc As Object, oDataRange As Object) Dim sCellN As String, sCellXYS As String, sCellR As String oSheets = oDoc.getSheets - sSheetName = oDataRange.getSpreadsheet.getName + sSheetName = oDataXRange.getSpreadsheet.getName mNames = oSheets.getElementNames For nI = 0 To UBound (mNames) If mNames (nI) = sSheetName Then @@ -55,11 +211,11 @@ Sub subReportPairedTTest (oDoc As Object, oDataRange As Object) oSheets.insertNewByName (sSheetName & "_ttest", nSheetIndex + 1) oSheet = oSheets.getByName (sSheetName & "_ttest") - nN = oDataRange.getRows.getCount - 1 - sCellXLabel = fnGetRangeName (oDataRange.getCellByPosition (0, 0)) - sCellsXData = fnGetRangeName (oDataRange.getCellRangeByPosition (0, 1, 0, nN)) - sCellYLabel = fnGetRangeName (oDataRange.getCellByPosition (1, 0)) - sCellsYData = fnGetRangeName (oDataRange.getCellRangeByPosition (1, 1, 1, nN)) + nN = oDataXRange.getRows.getCount - 1 + sCellXLabel = fnGetRangeName (oDataXRange.getCellByPosition (0, 0)) + sCellsXData = fnGetRangeName (oDataXRange.getCellRangeByPosition (0, 1, 0, nN)) + sCellYLabel = fnGetRangeName (oDataYRange.getCellByPosition (0, 0)) + sCellsYData = fnGetRangeName (oDataYRange.getCellRangeByPosition (0, 1, 0, nN)) ' Obtains the format parameters for the report. nFormatN = fnQueryFormat (oDoc, "#,##0") diff --git a/_3ITTest.vb b/_3ITTest.vb index 327572a..ab8c6af 100644 --- a/_3ITTest.vb +++ b/_3ITTest.vb @@ -1,14 +1,175 @@ ' _3ITTest: The macros to for generating the report of independent T-Test ' by imacat , 2016-08-24 + Option Explicit -' subTestIndependentTTest: Tests the independent T-test report +' subRunIndependentTTest: Runs the independent T-test. +Sub subRunIndependentTTest As Object + Dim oRange As Object + Dim mLabels () As String, nI As Integer, mSelected (0) As Integer + Dim oDialogModel As Object, oDialog As Object, nResult As Integer + Dim oTextModel As Object, oListModel1 As Object, oListModel2 As Object + Dim oButtonModel As Object + Dim nColumn As Integer, oRange1 As Object, oRange2 As Object + Dim oSheets As Object, sSheetName As String, sExisted As String + Dim oSheet As Object + + ' Asks the user for the data range + oRange = fnAskDataRange (ThisComponent) + If IsNull (oRange) Then + Exit Sub + End If + ReDim mLabels (oRange.getColumns.getCount - 1) As String + For nI = 0 To oRange.getColumns.getCount - 1 + mLabels (nI) = oRange.getCellByPosition (nI, 0).getString + Next nI + + ' Creates a dialog + oDialogModel = CreateUnoService ( _ + "com.sun.star.awt.UnoControlDialogModel") + oDialogModel.setPropertyValue ("PositionX", 200) + oDialogModel.setPropertyValue ("PositionY", 200) + oDialogModel.setPropertyValue ("Height", 80) + oDialogModel.setPropertyValue ("Width", 95) + oDialogModel.setPropertyValue ("Title", "Step 2/2: Specify the data") + + ' Adds the prompt. + oTextModel = oDialogModel.createInstance ( _ + "com.sun.star.awt.UnoControlFixedTextModel") + oTextModel.setPropertyValue ("PositionX", 5) + oTextModel.setPropertyValue ("PositionY", 5) + oTextModel.setPropertyValue ("Height", 10) + oTextModel.setPropertyValue ("Width", 85) + oTextModel.setPropertyValue ("Label", "Group column:") + oTextModel.setPropertyValue ("MultiLine", True) + oTextModel.setPropertyValue ("TabIndex", 1) + oDialogModel.insertByName ("txtPromptGroup", oTextModel) + + ' Adds the drop down list + oListModel1 = oDialogModel.createInstance ( _ + "com.sun.star.awt.UnoControlListBoxModel") + oListModel1.setPropertyValue ("PositionX", 5) + oListModel1.setPropertyValue ("PositionY", 15) + oListModel1.setPropertyValue ("Height", 10) + oListModel1.setPropertyValue ("Width", 85) + oListModel1.setPropertyValue ("Dropdown", True) + oListModel1.setPropertyValue ("StringItemList", mLabels) + mSelected (0) = 0 + oListModel1.setPropertyValue ("SelectedItems", mSelected) + oDialogModel.insertByName ("lstGroup", oListModel1) + + ' Adds the prompt. + oTextModel = oDialogModel.createInstance ( _ + "com.sun.star.awt.UnoControlFixedTextModel") + oTextModel.setPropertyValue ("PositionX", 5) + oTextModel.setPropertyValue ("PositionY", 30) + oTextModel.setPropertyValue ("Height", 10) + oTextModel.setPropertyValue ("Width", 85) + oTextModel.setPropertyValue ("Label", "Score column:") + oTextModel.setPropertyValue ("MultiLine", True) + oTextModel.setPropertyValue ("TabIndex", 1) + oDialogModel.insertByName ("txtPromptScore", oTextModel) + + ' Adds the drop down list + oListModel2 = oDialogModel.createInstance ( _ + "com.sun.star.awt.UnoControlListBoxModel") + oListModel2.setPropertyValue ("PositionX", 5) + oListModel2.setPropertyValue ("PositionY", 40) + oListModel2.setPropertyValue ("Height", 10) + oListModel2.setPropertyValue ("Width", 85) + oListModel2.setPropertyValue ("Dropdown", True) + oListModel2.setPropertyValue ("StringItemList", mLabels) + mSelected (0) = 1 + oListModel2.setPropertyValue ("SelectedItems", mSelected) + oDialogModel.insertByName ("lstScore", oListModel2) + + ' Adds the buttons. + oButtonModel = oDialogModel.createInstance ( _ + "com.sun.star.awt.UnoControlButtonModel") + oButtonModel.setPropertyValue ("PositionX", 5) + oButtonModel.setPropertyValue ("PositionY", 60) + oButtonModel.setPropertyValue ("Height", 15) + oButtonModel.setPropertyValue ("Width", 40) + oButtonModel.setPropertyValue ("PushButtonType", _ + com.sun.star.awt.PushButtonType.CANCEL) + oDialogModel.insertByName ("btnClose", oButtonModel) + + oButtonModel = oDialogModel.createInstance ( _ + "com.sun.star.awt.UnoControlButtonModel") + oButtonModel.setPropertyValue ("PositionX", 50) + oButtonModel.setPropertyValue ("PositionY", 60) + oButtonModel.setPropertyValue ("Height", 15) + oButtonModel.setPropertyValue ("Width", 40) + oButtonModel.setPropertyValue ("PushButtonType", _ + com.sun.star.awt.PushButtonType.OK) + oButtonModel.setPropertyValue ("DefaultButton", True) + oDialogModel.insertByName ("btnOK", oButtonModel) + + ' Adds the dialog model to the control and runs it. + oDialog = CreateUnoService ("com.sun.star.awt.UnoControlDialog") + oDialog.setModel (oDialogModel) + oDialog.setVisible (True) + nResult = oDialog.execute + oDialog.dispose + + ' Cancelled + If nResult = 0 Then + Exit Sub + End If + + nColumn = oListModel1.getPropertyValue ("SelectedItems") (0) + oRange1 = oRange.getCellRangeByPosition ( _ + nColumn, 0, nColumn, oRange.getRows.getCount - 1) + nColumn = oListModel2.getPropertyValue ("SelectedItems") (0) + oRange2 = oRange.getCellRangeByPosition ( _ + nColumn, 0, nColumn, oRange.getRows.getCount - 1) + + ' Checks the existing report + oSheets = ThisComponent.getSheets + sSheetName = oRange1.getSpreadsheet.getName + sExisted = "" + If oSheets.hasByName (sSheetName & "_ttest") Then + sExisted = sExisted & ", """ & sSheetName & "_ttest""" + End If + If oSheets.hasByName (sSheetName & "_ttesttmp") Then + sExisted = sExisted & ", """ & sSheetName & "_ttesttmp""" + End If + If sExisted <> "" Then + sExisted = Right (sExisted, Len (sExisted) - 2) + If InStr (sExisted, ",") > 0 Then + sExisted = "Spreadsheets " & sExisted & " exist. Overwrite?" + Else + sExisted = "Spreadsheet " & sExisted & " exists. Overwrite?" + End If + nResult = MsgBox(sExisted, MB_YESNO + MB_DEFBUTTON2 + MB_ICONQUESTION) + If nResult = IDNO Then + Exit Sub + End If + + ' Drops the existing report + If oSheets.hasByName (sSheetName & "_ttest") Then + oSheets.removeByname (sSheetName & "_ttest") + End If + If oSheets.hasByName (sSheetName & "_ttesttmp") Then + oSheets.removeByname (sSheetName & "_ttesttmp") + End If + End If + + ' Reports the independent T-test. + subReportIndependentTTest (ThisComponent, oRange1, oRange2) + + ' Makes the report sheet active. + oSheet = oSheets.getByName (sSheetName & "_ttest") + ThisComponent.getCurrentController.setActiveSheet (oSheet) +End Sub + +' subTestIndependentTTest: Tests the independent T-test report. Sub subTestIndependentTTest Dim oDoc As Object, oSheets As Object, sSheetName As String - Dim oSheet As Object, oRange As Object + Dim oSheet As Object, oLabelColumn As Object, oScoreColumn As Object oDoc = fnFindStatsTestDocument - If oDoc = Null Then + If IsNull (oDoc) Then MsgBox "Cannot find statstest.ods in the opened documents." Exit Sub End If @@ -26,12 +187,13 @@ Sub subTestIndependentTTest oSheets.removeByName (sSheetName & "_ttesttmp") End If oSheet = oSheets.getByName (sSheetName) - oRange = oSheet.getCellRangeByName ("A15:B34") - subReportIndependentTTest (oDoc, oRange) + oLabelColumn = oSheet.getCellRangeByName ("A15:A34") + oScoreColumn = oSheet.getCellRangeByName ("B15:B34") + subReportIndependentTTest (oDoc, oLabelColumn, oScoreColumn) End Sub ' subReportIndependentTTest: Reports the independent T-test -Sub subReportIndependentTTest (oDoc As Object, oDataRange As Object) +Sub subReportIndependentTTest (oDoc As Object, oLabelColumn As Object, oScoreColumn As Object) Dim oSheets As Object, sSheetName As String Dim mNames () As String, nI As Integer, nSheetIndex As Integer Dim oSheet As Object, oColumns As Object, nRow As Integer @@ -48,7 +210,7 @@ Sub subReportIndependentTTest (oDoc As Object, oDataRange As Object) Dim sCellF As String, sCellsN As String, sCellN As String oSheets = oDoc.getSheets - sSheetName = oDataRange.getSpreadsheet.getName + sSheetName = oLabelColumn.getSpreadsheet.getName mNames = oSheets.getElementNames For nI = 0 To UBound (mNames) If mNames (nI) = sSheetName Then @@ -58,7 +220,7 @@ Sub subReportIndependentTTest (oDoc As Object, oDataRange As Object) oSheets.insertNewByName (sSheetName & "_ttesttmp", nSheetIndex + 1) oSheet = oSheets.getByName (sSheetName & "_ttesttmp") - oTempDataRange = fnCollectIndependentTTestData (oDataRange, oSheet) + oTempDataRange = fnCollectIndependentTTestData (oSheet, oLabelColumn, oScoreColumn) oSheets.insertNewByName (sSheetName & "_ttest", nSheetIndex + 1) oSheet = oSheets.getByName (sSheetName & "_ttest") @@ -527,7 +689,7 @@ Sub subReportIndependentTTest (oDoc As Object, oDataRange As Object) End Sub ' fnCollectIndependentTTestData: Collects the data for the independent T-test. -Function fnCollectIndependentTTestData (oDataRange As Object, oReportSheet As Object) As Object +Function fnCollectIndependentTTestData (oReportSheet As Object, oLabelColumn As Object, oScoreColumn As Object) As Object Dim nRow As Integer, nNRow As Integer, sCellZMean As String, sCellsN As String Dim oCell As Object, oCells As Object, oCursor As Object Dim sCell As String, sLabel As String, sFormula As String @@ -540,8 +702,8 @@ Function fnCollectIndependentTTestData (oDataRange As Object, oReportSheet As Ob sCellXLabel = "" sCellYLabel = "" - For nRow = 1 To oDataRange.getRows.getCount - 1 - oCell = oDataRange.getCellByPosition (0, nRow) + For nRow = 1 To oLabelColumn.getRows.getCount - 1 + oCell = oLabelColumn.getCellByPosition (0, nRow) sLabel = oCell.getString If sLabel <> "" Then If sCellXLabel = "" Then @@ -551,7 +713,7 @@ Function fnCollectIndependentTTestData (oDataRange As Object, oReportSheet As Ob If sLabel <> sXLabel And sCellYLabel = "" Then sCellYLabel = fnGetRangeName (oCell) sYLabel = sLabel - nRow = oDataRange.getRows.getCount - 1 + nRow = oLabelColumn.getRows.getCount - 1 End If End If End If @@ -568,15 +730,15 @@ Function fnCollectIndependentTTestData (oDataRange As Object, oReportSheet As Ob ' The data nNX = 0 nNY = 0 - For nRow = 1 To oDataRange.getRows.getCount - 1 - If oDataRange.getCellByPosition (0, nRow).getString = sXLabel Then + For nRow = 1 To oLabelColumn.getRows.getCount - 1 + If oLabelColumn.getCellByPosition (0, nRow).getString = sXLabel Then nNX = nNX + 1 - sFormula = "=" & fnGetRangeName (oDataRange.getCellByPosition (1, nRow)) + sFormula = "=" & fnGetRangeName (oScoreColumn.getCellByPosition (0, nRow)) oReportSheet.getCellByPosition (0, nNX).setFormula (sFormula) Else - If oDataRange.getCellByPosition (0, nRow).getString = sYLabel Then + If oLabelColumn.getCellByPosition (0, nRow).getString = sYLabel Then nNY = nNY + 1 - sFormula = "=" & fnGetRangeName (oDataRange.getCellByPosition (1, nRow)) + sFormula = "=" & fnGetRangeName (oScoreColumn.getCellByPosition (0, nRow)) oReportSheet.getCellByPosition (1, nNY).setFormula (sFormula) End If End If diff --git a/_4ANOVA.vb b/_4ANOVA.vb index 5497479..a3bd82b 100644 --- a/_4ANOVA.vb +++ b/_4ANOVA.vb @@ -1,14 +1,175 @@ ' _4ANOVA: The macros to for generating the report of ANOVA (Analyze of Variances) ' by imacat , 2016-08-31 + Option Explicit +' subRunANOVA: Runs the ANOVA (Analyze of Variances). +Sub subRunANOVA As Object + Dim oRange As Object + Dim mLabels () As String, nI As Integer, mSelected (0) As Integer + Dim oDialogModel As Object, oDialog As Object, nResult As Integer + Dim oTextModel As Object, oListModel1 As Object, oListModel2 As Object + Dim oButtonModel As Object + Dim nColumn As Integer, oRange1 As Object, oRange2 As Object + Dim oSheets As Object, sSheetName As String, sExisted As String + Dim oSheet As Object + + ' Asks the user for the data range + oRange = fnAskDataRange (ThisComponent) + If IsNull (oRange) Then + Exit Sub + End If + ReDim mLabels (oRange.getColumns.getCount - 1) As String + For nI = 0 To oRange.getColumns.getCount - 1 + mLabels (nI) = oRange.getCellByPosition (nI, 0).getString + Next nI + + ' Creates a dialog + oDialogModel = CreateUnoService ( _ + "com.sun.star.awt.UnoControlDialogModel") + oDialogModel.setPropertyValue ("PositionX", 200) + oDialogModel.setPropertyValue ("PositionY", 200) + oDialogModel.setPropertyValue ("Height", 80) + oDialogModel.setPropertyValue ("Width", 95) + oDialogModel.setPropertyValue ("Title", "Step 2/2: Specify the data") + + ' Adds the prompt. + oTextModel = oDialogModel.createInstance ( _ + "com.sun.star.awt.UnoControlFixedTextModel") + oTextModel.setPropertyValue ("PositionX", 5) + oTextModel.setPropertyValue ("PositionY", 5) + oTextModel.setPropertyValue ("Height", 10) + oTextModel.setPropertyValue ("Width", 85) + oTextModel.setPropertyValue ("Label", "Group column:") + oTextModel.setPropertyValue ("MultiLine", True) + oTextModel.setPropertyValue ("TabIndex", 1) + oDialogModel.insertByName ("txtPromptGroup", oTextModel) + + ' Adds the drop down list + oListModel1 = oDialogModel.createInstance ( _ + "com.sun.star.awt.UnoControlListBoxModel") + oListModel1.setPropertyValue ("PositionX", 5) + oListModel1.setPropertyValue ("PositionY", 15) + oListModel1.setPropertyValue ("Height", 10) + oListModel1.setPropertyValue ("Width", 85) + oListModel1.setPropertyValue ("Dropdown", True) + oListModel1.setPropertyValue ("StringItemList", mLabels) + mSelected (0) = 0 + oListModel1.setPropertyValue ("SelectedItems", mSelected) + oDialogModel.insertByName ("lstGroup", oListModel1) + + ' Adds the prompt. + oTextModel = oDialogModel.createInstance ( _ + "com.sun.star.awt.UnoControlFixedTextModel") + oTextModel.setPropertyValue ("PositionX", 5) + oTextModel.setPropertyValue ("PositionY", 30) + oTextModel.setPropertyValue ("Height", 10) + oTextModel.setPropertyValue ("Width", 85) + oTextModel.setPropertyValue ("Label", "Score column:") + oTextModel.setPropertyValue ("MultiLine", True) + oTextModel.setPropertyValue ("TabIndex", 1) + oDialogModel.insertByName ("txtPromptScore", oTextModel) + + ' Adds the drop down list + oListModel2 = oDialogModel.createInstance ( _ + "com.sun.star.awt.UnoControlListBoxModel") + oListModel2.setPropertyValue ("PositionX", 5) + oListModel2.setPropertyValue ("PositionY", 40) + oListModel2.setPropertyValue ("Height", 10) + oListModel2.setPropertyValue ("Width", 85) + oListModel2.setPropertyValue ("Dropdown", True) + oListModel2.setPropertyValue ("StringItemList", mLabels) + mSelected (0) = 1 + oListModel2.setPropertyValue ("SelectedItems", mSelected) + oDialogModel.insertByName ("lstScore", oListModel2) + + ' Adds the buttons. + oButtonModel = oDialogModel.createInstance ( _ + "com.sun.star.awt.UnoControlButtonModel") + oButtonModel.setPropertyValue ("PositionX", 5) + oButtonModel.setPropertyValue ("PositionY", 60) + oButtonModel.setPropertyValue ("Height", 15) + oButtonModel.setPropertyValue ("Width", 40) + oButtonModel.setPropertyValue ("PushButtonType", _ + com.sun.star.awt.PushButtonType.CANCEL) + oDialogModel.insertByName ("btnClose", oButtonModel) + + oButtonModel = oDialogModel.createInstance ( _ + "com.sun.star.awt.UnoControlButtonModel") + oButtonModel.setPropertyValue ("PositionX", 50) + oButtonModel.setPropertyValue ("PositionY", 60) + oButtonModel.setPropertyValue ("Height", 15) + oButtonModel.setPropertyValue ("Width", 40) + oButtonModel.setPropertyValue ("PushButtonType", _ + com.sun.star.awt.PushButtonType.OK) + oButtonModel.setPropertyValue ("DefaultButton", True) + oDialogModel.insertByName ("btnOK", oButtonModel) + + ' Adds the dialog model to the control and runs it. + oDialog = CreateUnoService ("com.sun.star.awt.UnoControlDialog") + oDialog.setModel (oDialogModel) + oDialog.setVisible (True) + nResult = oDialog.execute + oDialog.dispose + + ' Cancelled + If nResult = 0 Then + Exit Sub + End If + + nColumn = oListModel1.getPropertyValue ("SelectedItems") (0) + oRange1 = oRange.getCellRangeByPosition ( _ + nColumn, 0, nColumn, oRange.getRows.getCount - 1) + nColumn = oListModel2.getPropertyValue ("SelectedItems") (0) + oRange2 = oRange.getCellRangeByPosition ( _ + nColumn, 0, nColumn, oRange.getRows.getCount - 1) + + ' Checks the existing report + oSheets = ThisComponent.getSheets + sSheetName = oRange1.getSpreadsheet.getName + sExisted = "" + If oSheets.hasByName (sSheetName & "_anova") Then + sExisted = sExisted & ", """ & sSheetName & "_anova""" + End If + If oSheets.hasByName (sSheetName & "_anovatmp") Then + sExisted = sExisted & ", """ & sSheetName & "_anovatmp""" + End If + If sExisted <> "" Then + sExisted = Right (sExisted, Len (sExisted) - 2) + If InStr (sExisted, ",") > 0 Then + sExisted = "Spreadsheets " & sExisted & " exist. Overwrite?" + Else + sExisted = "Spreadsheet " & sExisted & " exists. Overwrite?" + End If + nResult = MsgBox(sExisted, MB_YESNO + MB_DEFBUTTON2 + MB_ICONQUESTION) + If nResult = IDNO Then + Exit Sub + End If + + ' Drops the existing report + If oSheets.hasByName (sSheetName & "_anova") Then + oSheets.removeByname (sSheetName & "_anova") + End If + If oSheets.hasByName (sSheetName & "_anovatmp") Then + oSheets.removeByname (sSheetName & "_anovatmp") + End If + End If + + ' Reports the ANOVA (Analyze of Variances) + subReportANOVA (ThisComponent, oRange1, oRange2) + + ' Makes the report sheet active. + oSheet = oSheets.getByName (sSheetName & "_anova") + ThisComponent.getCurrentController.setActiveSheet (oSheet) +End Sub + ' subTestANOVA: Tests the ANOVA (Analyze of Variances) report Sub subTestANOVA Dim oDoc As Object, oSheets As Object, sSheetName As String - Dim oSheet As Object, oRange As Object + Dim oSheet As Object, oLabelColumn As Object, oScoreColumn As Object oDoc = fnFindStatsTestDocument - If oDoc = Null Then + If IsNull (oDoc) Then MsgBox "Cannot find statstest.ods in the opened documents." Exit Sub End If @@ -26,12 +187,13 @@ Sub subTestANOVA oSheets.removeByName (sSheetName & "_anovatmp") End If oSheet = oSheets.getByName (sSheetName) - oRange = oSheet.getCellRangeByName ("A13:B35") - subReportANOVA (oDoc, oRange) + oLabelColumn = oSheet.getCellRangeByName ("A13:A35") + oScoreColumn = oSheet.getCellRangeByName ("B13:B35") + subReportANOVA (oDoc, oLabelColumn, oScoreColumn) End Sub ' subReportANOVA: Reports the ANOVA (Analyze of Variances) -Sub subReportANOVA (oDoc As Object, oDataRange As Object) +Sub subReportANOVA (oDoc As Object, oLabelColumn As Object, oScoreColumn As Object) Dim oSheets As Object, sSheetName As String Dim nI As Integer, nJ As Integer Dim mNames () As String, nSheetIndex As Integer @@ -54,7 +216,7 @@ Sub subReportANOVA (oDoc As Object, oDataRange As Object) Dim sCellMeanDiff As String oSheets = oDoc.getSheets - sSheetName = oDataRange.getSpreadsheet.getName + sSheetName = oLabelColumn.getSpreadsheet.getName mNames = oSheets.getElementNames For nI = 0 To UBound (mNames) If mNames (nI) = sSheetName Then @@ -64,7 +226,7 @@ Sub subReportANOVA (oDoc As Object, oDataRange As Object) oSheets.insertNewByName (sSheetName & "_anovatmp", nSheetIndex + 1) oSheet = oSheets.getByName (sSheetName & "_anovatmp") - oTempDataRange = fnCollectANOVAData (oDataRange, oSheet) + oTempDataRange = fnCollectANOVAData (oSheet, oLabelColumn, oScoreColumn) nGroups = oTempDataRange.getColumns.getCount / 3 oSheets.insertNewByName (sSheetName & "_anova", nSheetIndex + 1) @@ -852,7 +1014,7 @@ Sub subReportANOVA (oDoc As Object, oDataRange As Object) End Sub ' fnCollectANOVAData: Collects the data for the ANOVA (Analyze of Variances). -Function fnCollectANOVAData (oDataRange As Object, oReportSheet As Object) As Object +Function fnCollectANOVAData (oReportSheet As Object, oLabelColumn As Object, oScoreColumn As Object) As Object Dim nRow As Integer, nColumn As Integer, nI As Integer Dim nNRow As Integer, sCellZMean As String, sCellsN As String Dim oCell As Object, oCells As Object, oCursor As Object @@ -865,8 +1027,8 @@ Function fnCollectANOVAData (oDataRange As Object, oReportSheet As Object) As Ob sLabels = " " nGroups = 0 - For nRow = 1 To oDataRange.getRows.getCount - 1 - sLabel = oDataRange.getCellByPosition (0, nRow).getString + For nRow = 1 To oLabelColumn.getRows.getCount - 1 + sLabel = oLabelColumn.getCellByPosition (0, nRow).getString If InStr (sLabels, " " & sLabel & " ") = 0 Then sLabels = sLabels & sLabel & " " nGroups = nGroups + 1 @@ -880,8 +1042,8 @@ Function fnCollectANOVAData (oDataRange As Object, oReportSheet As Object) As Ob sLabels = " " nGroups = 0 - For nRow = 1 To oDataRange.getRows.getCount - 1 - oCell = oDataRange.getCellByPosition (0, nRow) + For nRow = 1 To oLabelColumn.getRows.getCount - 1 + oCell = oLabelColumn.getCellByPosition (0, nRow) sLabel = oCell.getString If InStr (sLabels, " " & sLabel & " ") = 0 Then sLabels = sLabels & sLabel & " " @@ -902,8 +1064,8 @@ Function fnCollectANOVAData (oDataRange As Object, oReportSheet As Object) As Ob For nI = 0 To nGroups - 1 mN (nI) = 0 Next nI - For nRow = 1 To oDataRange.getRows.getCount - 1 - sLabel = oDataRange.getCellByPosition (0, nRow).getString + For nRow = 1 To oLabelColumn.getRows.getCount - 1 + sLabel = oLabelColumn.getCellByPosition (0, nRow).getString For nI = 0 To nGroups - 1 If sLabel = mLabels (nI) Then nColumn = nI @@ -911,7 +1073,7 @@ Function fnCollectANOVAData (oDataRange As Object, oReportSheet As Object) As Ob End If Next nI mN (nColumn) = mN (nColumn) + 1 - sFormula = "=" & fnGetRangeName (oDataRange.getCellByPosition (1, nRow)) + sFormula = "=" & fnGetRangeName (oScoreColumn.getCellByPosition (0, nRow)) oCell = oReportSheet.getCellByPosition (nColumn, mN (nColumn)) oCell.setFormula (sFormula) Next nRow diff --git a/_5Chi2GoF.vb b/_5Chi2GoF.vb index db1b90e..b36d011 100644 --- a/_5Chi2GoF.vb +++ b/_5Chi2GoF.vb @@ -1,14 +1,169 @@ ' _5Chi2GoF: The macros to for generating the report of Chi-square goodness of fit ' by imacat , 2016-09-05 + Option Explicit +' subRunChi2GoodnessOfFit: Runs the chi-square goodness of fit. +Sub subRunChi2GoodnessOfFit As Object + Dim oRange As Object + Dim mLabels () As String, nI As Integer, mSelected (0) As Integer + Dim oDialogModel As Object, oDialog As Object, nResult As Integer + Dim oTextModel As Object, oListModel1 As Object, oListModel2 As Object + Dim oButtonModel As Object + Dim nColumn As Integer, oRange1 As Object, oRange2 As Object + Dim oSheets As Object, sSheetName As String, sExisted As String + Dim oSheet As Object + + ' Asks the user for the data range + oRange = fnAskDataRange (ThisComponent) + If IsNull (oRange) Then + Exit Sub + End If + ReDim mLabels (oRange.getColumns.getCount - 1) As String + For nI = 0 To oRange.getColumns.getCount - 1 + mLabels (nI) = oRange.getCellByPosition (nI, 0).getString + Next nI + + ' Creates a dialog + oDialogModel = CreateUnoService ( _ + "com.sun.star.awt.UnoControlDialogModel") + oDialogModel.setPropertyValue ("PositionX", 200) + oDialogModel.setPropertyValue ("PositionY", 200) + oDialogModel.setPropertyValue ("Height", 80) + oDialogModel.setPropertyValue ("Width", 95) + oDialogModel.setPropertyValue ("Title", "Step 2/2: Specify the data") + + ' Adds the prompt. + oTextModel = oDialogModel.createInstance ( _ + "com.sun.star.awt.UnoControlFixedTextModel") + oTextModel.setPropertyValue ("PositionX", 5) + oTextModel.setPropertyValue ("PositionY", 5) + oTextModel.setPropertyValue ("Height", 10) + oTextModel.setPropertyValue ("Width", 85) + oTextModel.setPropertyValue ("Label", "Group (column) column:") + oTextModel.setPropertyValue ("MultiLine", True) + oTextModel.setPropertyValue ("TabIndex", 1) + oDialogModel.insertByName ("txtPromptGroup", oTextModel) + + ' Adds the drop down list + oListModel1 = oDialogModel.createInstance ( _ + "com.sun.star.awt.UnoControlListBoxModel") + oListModel1.setPropertyValue ("PositionX", 5) + oListModel1.setPropertyValue ("PositionY", 15) + oListModel1.setPropertyValue ("Height", 10) + oListModel1.setPropertyValue ("Width", 85) + oListModel1.setPropertyValue ("Dropdown", True) + oListModel1.setPropertyValue ("StringItemList", mLabels) + mSelected (0) = 0 + oListModel1.setPropertyValue ("SelectedItems", mSelected) + oDialogModel.insertByName ("lstGroup", oListModel1) + + ' Adds the prompt. + oTextModel = oDialogModel.createInstance ( _ + "com.sun.star.awt.UnoControlFixedTextModel") + oTextModel.setPropertyValue ("PositionX", 5) + oTextModel.setPropertyValue ("PositionY", 30) + oTextModel.setPropertyValue ("Height", 10) + oTextModel.setPropertyValue ("Width", 85) + oTextModel.setPropertyValue ("Label", "Event (row) column:") + oTextModel.setPropertyValue ("MultiLine", True) + oTextModel.setPropertyValue ("TabIndex", 1) + oDialogModel.insertByName ("txtPromptScore", oTextModel) + + ' Adds the drop down list + oListModel2 = oDialogModel.createInstance ( _ + "com.sun.star.awt.UnoControlListBoxModel") + oListModel2.setPropertyValue ("PositionX", 5) + oListModel2.setPropertyValue ("PositionY", 40) + oListModel2.setPropertyValue ("Height", 10) + oListModel2.setPropertyValue ("Width", 85) + oListModel2.setPropertyValue ("Dropdown", True) + oListModel2.setPropertyValue ("StringItemList", mLabels) + mSelected (0) = 1 + oListModel2.setPropertyValue ("SelectedItems", mSelected) + oDialogModel.insertByName ("lstScore", oListModel2) + + ' Adds the buttons. + oButtonModel = oDialogModel.createInstance ( _ + "com.sun.star.awt.UnoControlButtonModel") + oButtonModel.setPropertyValue ("PositionX", 5) + oButtonModel.setPropertyValue ("PositionY", 60) + oButtonModel.setPropertyValue ("Height", 15) + oButtonModel.setPropertyValue ("Width", 40) + oButtonModel.setPropertyValue ("PushButtonType", _ + com.sun.star.awt.PushButtonType.CANCEL) + oDialogModel.insertByName ("btnClose", oButtonModel) + + oButtonModel = oDialogModel.createInstance ( _ + "com.sun.star.awt.UnoControlButtonModel") + oButtonModel.setPropertyValue ("PositionX", 50) + oButtonModel.setPropertyValue ("PositionY", 60) + oButtonModel.setPropertyValue ("Height", 15) + oButtonModel.setPropertyValue ("Width", 40) + oButtonModel.setPropertyValue ("PushButtonType", _ + com.sun.star.awt.PushButtonType.OK) + oButtonModel.setPropertyValue ("DefaultButton", True) + oDialogModel.insertByName ("btnOK", oButtonModel) + + ' Adds the dialog model to the control and runs it. + oDialog = CreateUnoService ("com.sun.star.awt.UnoControlDialog") + oDialog.setModel (oDialogModel) + oDialog.setVisible (True) + nResult = oDialog.execute + oDialog.dispose + + ' Cancelled + If nResult = 0 Then + Exit Sub + End If + + nColumn = oListModel1.getPropertyValue ("SelectedItems") (0) + oRange1 = oRange.getCellRangeByPosition ( _ + nColumn, 0, nColumn, oRange.getRows.getCount - 1) + nColumn = oListModel2.getPropertyValue ("SelectedItems") (0) + oRange2 = oRange.getCellRangeByPosition ( _ + nColumn, 0, nColumn, oRange.getRows.getCount - 1) + + ' Checks the existing report + oSheets = ThisComponent.getSheets + sSheetName = oRange1.getSpreadsheet.getName + sExisted = "" + If oSheets.hasByName (sSheetName & "_chi2") Then + sExisted = sExisted & ", """ & sSheetName & "_chi2""" + End If + If sExisted <> "" Then + sExisted = Right (sExisted, Len (sExisted) - 2) + If InStr (sExisted, ",") > 0 Then + sExisted = "Spreadsheets " & sExisted & " exist. Overwrite?" + Else + sExisted = "Spreadsheet " & sExisted & " exists. Overwrite?" + End If + nResult = MsgBox(sExisted, MB_YESNO + MB_DEFBUTTON2 + MB_ICONQUESTION) + If nResult = IDNO Then + Exit Sub + End If + + ' Drops the existing report + If oSheets.hasByName (sSheetName & "_chi2") Then + oSheets.removeByname (sSheetName & "_chi2") + End If + End If + + ' Reports the chi-square goodness of fit + subReportChi2GoodnessOfFit (ThisComponent, oRange1, oRange2) + + ' Makes the report sheet active. + oSheet = oSheets.getByName (sSheetName & "_chi2") + ThisComponent.getCurrentController.setActiveSheet (oSheet) +End Sub + ' subTestChi2GoodnessOfFit: Tests the chi-square goodness of fit report Sub subTestChi2GoodnessOfFit Dim oDoc As Object, oSheets As Object, sSheetName As String - Dim oSheet As Object, oRange As Object + Dim oSheet As Object, oColumnColumn As Object, oRowColumn As Object oDoc = fnFindStatsTestDocument - If oDoc = Null Then + If IsNull (oDoc) Then MsgBox "Cannot find statstest.ods in the opened documents." Exit Sub End If @@ -23,12 +178,13 @@ Sub subTestChi2GoodnessOfFit oSheets.removeByName (sSheetName & "_chi2") End If oSheet = oSheets.getByName (sSheetName) - oRange = oSheet.getCellRangeByName ("A7:B192") - subReportChi2GoodnessOfFit (oDoc, oRange) + oColumnColumn = oSheet.getCellRangeByName ("A7:A192") + oRowColumn = oSheet.getCellRangeByName ("B7:B192") + subReportChi2GoodnessOfFit (oDoc, oColumnColumn, oRowColumn) End Sub ' subReportChi2GoodnessOfFit: Reports the chi-square goodness of fit -Sub subReportChi2GoodnessOfFit (oDoc As Object, oDataRange As Object) +Sub subReportChi2GoodnessOfFit (oDoc As Object, oColumnColumn As Object, oRowColumn As Object) Dim oSheets As Object, sSheetName As String Dim nI As Integer, nJ As Integer, nJ1 As Integer, nJ2 As Integer Dim mNames () As String, nSheetIndex As Integer @@ -54,7 +210,7 @@ Sub subReportChi2GoodnessOfFit (oDoc As Object, oDataRange As Object) Dim sSE2 AS String, nTotalColumns As Integer oSheets = oDoc.getSheets - sSheetName = oDataRange.getSpreadsheet.getName + sSheetName = oColumnColumn.getSpreadsheet.getName mNames = oSheets.getElementNames For nI = 0 To UBound (mNames) If mNames (nI) = sSheetName Then @@ -64,21 +220,21 @@ Sub subReportChi2GoodnessOfFit (oDoc As Object, oDataRange As Object) oSheets.insertNewByName (sSheetName & "_chi2", nSheetIndex + 1) oSheet = oSheets.getByName (sSheetName & "_chi2") - sCellsJData = fnGetRangeName (oDataRange.getCellRangeByPosition (0, 1, 0, oDataRange.getRows.getCount - 1)) - sCellsIData = fnGetRangeName (oDataRange.getCellRangeByPosition (1, 1, 1, oDataRange.getRows.getCount - 1)) + sCellsJData = fnGetRangeName (oColumnColumn.getCellRangeByPosition (0, 1, 0, oColumnColumn.getRows.getCount - 1)) + sCellsIData = fnGetRangeName (oRowColumn.getCellRangeByPosition (0, 1, 0, oRowColumn.getRows.getCount - 1)) ' Counts the number of groups and events sLabelsColumn = " " sLabelsRow = " " nGroups = 0 nEvents = 0 - For nRow = 1 To oDataRange.getRows.getCount - 1 - sLabel = oDataRange.getCellByPosition (0, nRow).getString + For nRow = 1 To oColumnColumn.getRows.getCount - 1 + sLabel = oColumnColumn.getCellByPosition (0, nRow).getString If InStr (sLabelsColumn, " " & sLabel & " ") = 0 Then sLabelsColumn = sLabelsColumn & sLabel & " " nGroups = nGroups + 1 End If - sLabel = oDataRange.getCellByPosition (1, nRow).getString + sLabel = oRowColumn.getCellByPosition (0, nRow).getString If InStr (sLabelsRow, " " & sLabel & " ") = 0 Then sLabelsRow = sLabelsRow & sLabel & " " nEvents = nEvents + 1 @@ -96,15 +252,15 @@ Sub subReportChi2GoodnessOfFit (oDoc As Object, oDataRange As Object) sLabelsRow = " " nJ = 0 nI = 0 - For nRow = 1 To oDataRange.getRows.getCount - 1 - oCell = oDataRange.getCellByPosition (0, nRow) + For nRow = 1 To oColumnColumn.getRows.getCount - 1 + oCell = oColumnColumn.getCellByPosition (0, nRow) sLabel = oCell.getString If InStr (sLabelsColumn, " " & sLabel & " ") = 0 Then sLabelsColumn = sLabelsColumn & sLabel & " " mCellLabelColomn (nJ) = fnGetRangeName (oCell) nJ = nJ + 1 End If - oCell = oDataRange.getCellByPosition (1, nRow) + oCell = oRowColumn.getCellByPosition (0, nRow) sLabel = oCell.getString If InStr (sLabelsRow, " " & sLabel & " ") = 0 Then sLabelsRow = sLabelsRow & sLabel & " "