vba - 更快的单元格输入值 LibreOffice Vba

标签 vba libreoffice cells libreoffice-calc

我正在尝试使用宏将数据(cca.70*25*15 单元格)从 txt 文件复制到 libre Office 电子表格中。现在我正在使用以下代码访问单元格:

Example:
Dim CellValue As Object

CellValue = ThisComponent.Sheets(1).getCellByPosition(i, j)
CellValue.String = "Whatever data i get from txt"

通过运行上述代码,如果文本文件有(70*25*15)个单元格条目,则需要 2 分 18 秒才能将所有数据从文本文件复制到电子表格。有没有更好的方法来访问细胞并加速这个过程?

最佳答案

您可以设置实现 XCellRangeData 的对象的 DataArray,形成数组的数组。

示例:

Sub Test()

  aDataArray = array(array("A1", "B1", "C1"), array("A2", 2.2, 2.3), array("A3", 3.2, 3.3))

  oDoc = ThisComponent

  oSheet = oDoc.getSheets().getByIndex(1)

  oRange = oSheet.getCellRangeByName("A1:C3")

  'xray oRange

  oRange.setDataArray(aDataArray)

End Sub

https://www.openoffice.org/api/docs/common/ref/com/sun/star/sheet/XCellRangeData.html#setDataArray

编辑:

更复杂的示例:

Sub Test()

  lRowCount = 1800
  lColCount = 40

  dim aDataArray() as variant
  dim aColumnArray() as variant

  redim aDataArray(lRowCount-1) 'the main array is for rows; 0-based so count-1

  for lRow = lbound(aDataArray) to ubound(aDataArray)

   redim aColumnArray(lColCount-1) 'this array is for column data

   for lCol = lbound(aColumnArray) to ubound(aColumnArray)
    'create some sample data
    select case lCol
     case 1
      aColumnArray(lCol) = CInt(int((10 * rnd()) + 1) 'integer in column 2
     case 2
      aColumnArray(lCol) = CDbl(1000 * rnd()) 'double in column 3, later formatted as currency     
     case 3
      aColumnArray(lCol) = CDbl(date + int(365*rnd())) 'date in column 4, must be double in the array, will be later formatted as date
     case 4
      aColumnArray(lCol) = CInt(lRow mod 2 = 0) 'boolean in column 5, must be integer in the array, will be later formatted as booleann
     case else
      aColumnArray(lCol) = "r" & lRow & "c" & lCol 'all other columns string
    end select
   next
   aDataArray(lRow) = aColumnArray
  next

  oDoc = ThisComponent

  oSheet = oDoc.getSheets().getByIndex(0)

  lStartRow = 1
  lStartCol = 0
  'make sure, the size of the range will exactly match the array size
  oRange = oSheet.getCellRangeByPosition(lStartCol, lStartRow, lStartCol+lColCount-1, lStartRow+lRowCount-1)

  oRange.setDataArray(aDataArray) 'now the data is in the sheet

  'this code is for formatting
  oLocale = new com.sun.star.lang.Locale 'create empty locale

  oNumberFormats = thiscomponent.getNumberFormats() 'get NumberFormats from Calc

  'format column 3 as currency   
  oRange = oSheet.getCellRangeByPosition(2, lStartRow, 2, lStartRow+lRowCount-1)
  lCurrencyFormat = oNumberFormats.getStandardFormat(com.sun.star.util.NumberFormat.CURRENCY, oLocale)  
  oRange.NumberFormat = lCurrencyFormat

  'format column 4 as date
  oRange = oSheet.getCellRangeByPosition(3, lStartRow, 3, lStartRow+lRowCount-1)
  lFormat = oNumberFormats.getStandardFormat(com.sun.star.util.NumberFormat.DATE, oLocale)  
  oRange.NumberFormat = lFormat

  'format column 5 as boolean
  oRange = oSheet.getCellRangeByPosition(4, lStartRow, 4, lStartRow+lRowCount-1)
  lFormat = oNumberFormats.getStandardFormat(com.sun.star.util.NumberFormat.LOGICAL, oLocale)  
  oRange.NumberFormat = lFormat

End Sub

关于vba - 更快的单元格输入值 LibreOffice Vba,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34943297/

相关文章:

r - 突出显示热图中的单元格

vba - 如何向 VBA ListBox 添加水平滚动条

excel - 折叠前几年的数据透视表详细信息

java - Windows 7 java中的权限批量无法识别

vba - 无法弄清楚 Object required 错误

java - Java 应用程序可以在 LibreOffice 中创建报告吗?

python - 子进程在 docker 容器中不起作用

regex - 使用正则表达式删除任何空白或空白行

java - GridLayout 的一个单元格中有多个 Java GUI 组件?

php explode vs 列