Загрузка данных


Sub OnClick()
    Dim oDoc As Object
    Dim oSheets As Object
    Dim oSheet As Object
    Dim i As Integer, j As Integer
    Dim q As Double
    Dim place As Integer

    ' Получаем текущий документ
    oDoc = ThisComponent

    ' Проверка, что текущий документ - таблица
    If oDoc.supportsService("com.sun.star.sheet.SpreadsheetDocument") Then
        Set oSheets = oDoc.getSheets()
    Else
        MsgBox "Текущий документ не является таблицей."
        Exit Sub
    End If
    
    ' Пытаемся получить лист с именем "Лист1"
    On Error GoTo ErrSheet
    Set oSheet = oSheets.getByName("Лист1")
    On Error GoTo 0
    
    ' Расчет суммы за 3 месяца по столбцам 2-4
    For i = 1 To 4
        oSheet.getCellByPosition(4, i).Value = oSheet.getCellByPosition(1, i).Value + _
                                               oSheet.getCellByPosition(2, i).Value + _
                                               oSheet.getCellByPosition(3, i).Value
    Next i

    ' Заполнение массива выручки
    Dim box(0 To 3) As Double
    For i = 1 To 4
        box(i - 1) = oSheet.getCellByPosition(4, i).Value
    Next i

    ' Сортировка массива по убыванию методом пузырька
    For i = 0 To 2
        For j = 0 To 2 - i
            If box(j) < box(j + 1) Then
                q = box(j + 1)
                box(j + 1) = box(j)
                box(j) = q
            End If
        Next j
    Next i

    ' Назначение места для каждого месяца
    For i = 1 To 4
        Dim currentVal As Double
        currentVal = oSheet.getCellByPosition(4, i).Value
        place = 0
        For j = 0 To 3
            If Abs(currentVal - box(j)) < 0.000001 Then
                place = j + 1
                Exit For
            End If
        Next j
        ' Запись места в столбец G (индекс 6)
        oSheet.getCellByPosition(6, i).Value = place
    Next i

    ' Проверка условия и начисление значений
    For i = 1 To 4
        ' Предположим, что сравниваем выручку с значением в ячейке (например, H1 - кол. 10, строка 1)
        If oSheet.getCellByPosition(4, i).Value >= oSheet.getCellByPosition(10, 0).Value Then
            oSheet.getCellByPosition(5, i).Value = 2
        Else
            oSheet.getCellByPosition(5, i).Value = 0
        End If
    Next i

    ' Расчет суммы по колонкам 5 и 6
    For i = 1 To 4
        oSheet.getCellByPosition(7, i).Value = oSheet.getCellByPosition(5, i).Value + oSheet.getCellByPosition(6, i).Value
    Next i

    ' Расчет итогового значения в колонке 8
    For i = 1 To 4
        oSheet.getCellByPosition(8, i).Value = oSheet.getCellByPosition(4, i).Value/100 * oSheet.getCellByPosition(7, i).Value
    Next i

    Exit Sub

ErrSheet:
    MsgBox "Лист 'Лист1' не найден. Проверьте название листа."
End Sub