简体   繁体   English

Libreoffice calc - 如何将相同的值写入范围

[英]Libreoffice calc - how to write a same value into a range

I know how to 'select' a range in LO (7.2.4.1) Calc BASIC....我知道如何在 LO (7.2.4.1) Calc BASIC 中“选择”一个范围......

ThisComponent.CurrentController.ActiveSheet.getCellRangeByName("D1:H6")

But how to write a value, eg "1", into that range using BASIC?但是如何使用 BASIC 将值(例如“1”)写入该范围?

myRange = ThisComponent.CurrentController.ActiveSheet.getCellRangeByName("D1:H6")
myRange.Value = 1

Gives an "property or method not found" error.给出“找不到属性或方法”错误。 But I can't find any properties or values to go after Range to allow me to do what I want.但是我在 Range 之后找不到 go 的任何属性或值,以允许我做我想做的事。 Flailing around and trying到处乱跑并尝试

myRange.setValue = 1
myRange.writeValue = 1
myRange.setString = "1"

and numerous other variants don't work either.并且许多其他变体也不起作用。

Would really appreciate the solution.真的很感激这个解决方案。 Thanks.谢谢。

You can edit the value of an individual cell, but not the entire range.您可以编辑单个单元格的值,但不能编辑整个范围。 You will have to iterate over all the cells in the range one at a time, changing the value of each of them.您将不得不一次遍历一个范围内的所有单元格,更改每个单元格的值。

Sub Set1ToD1H6
    myRange = ThisComponent.CurrentController.ActiveSheet.getCellRangeByName("D1:H6")
    For i = 0 To myRange.getRows().getCount()-1
        For j = 0 To myRange.getColumns().getCount()-1
            myRange.getCellByPosition(j, i).setValue(1)
        Next j
    Next i
End Sub

But since the read-write operation to a cell is comparable in time to the read-write operation to a whole range, it is preferable to use another method - to prepare data in an array and write from it to a range in one operation:但是由于对单元格的读写操作在时间上与对整个范围的读写操作相当,因此最好使用另一种方法 - 在数组中准备数据并在一次操作中将数据写入范围:

Sub Set1ToRange
    myRange = ThisComponent.CurrentController.ActiveSheet.getCellRangeByName("D1:H6")
    dataOfRange = myRange.getData()
    For i = LBound(dataOfRange) To UBound(dataOfRange)
        For j = LBound(dataOfRange(i)) To UBound(dataOfRange(i))
            dataOfRange(i)(j) = 1
        Next j
    Next i
    myRange.setData(dataOfRange)
End Sub

The .getData() and.setData() methods work on numeric range values. .getData() 和 .setData()方法适用于数值范围值。 To work with text strings (and numbers), use .getDataArray() and.setDataArray() , for working with cell formulas use .getFormulaArray() and.setFormulaArray()要处理文本字符串(和数字),请使用.getDataArray() 和 .setDataArray() ,要处理单元格公式,请使用.getFormulaArray() 和 .setFormulaArray()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM