在Applescript中创建给定起始单元格和行/列偏移量的Excel范围

在Applescript中创建给定起始单元格和行/列偏移量的Excel范围,applescript,Applescript,我正在尝试使用Applescript在Excel工作表的单元格中插入值。这些值作为列表列表保存在Applescript变量中(请参见示例代码)。我已经计算出,如果我将这个变量插入到一个正确大小的范围(行和列),它将正确地插入,并且所有内容都在正确的位置 不幸的是,我插入的数据量可能会有所不同,因此我不需要硬编码范围大小,而需要根据列表中的项目数来计算范围大小。我使用列表大小计算出从起始单元格的偏移量,然后尝试创建一个从起始单元格到计算出的单元格偏移量的范围 最低限度,代码如下所示: set my

我正在尝试使用Applescript在Excel工作表的单元格中插入值。这些值作为列表列表保存在Applescript变量中(请参见示例代码)。我已经计算出,如果我将这个变量插入到一个正确大小的范围(行和列),它将正确地插入,并且所有内容都在正确的位置

不幸的是,我插入的数据量可能会有所不同,因此我不需要硬编码范围大小,而需要根据列表中的项目数来计算范围大小。我使用列表大小计算出从起始单元格的偏移量,然后尝试创建一个从起始单元格到计算出的单元格偏移量的范围

最低限度,代码如下所示:

set myList to {{"Red", "Grapefruit", "1", ""}, {"Julie", "Heron", "2", "*"}}

tell application "Microsoft Excel"
    set myStartCell to range ("B18") of sheet 1 of active workbook
    set myEndCell to get offset myStartCell row offset ((count myList) -1) column offset ((count item 1 of myList) -1)
    set myRange to range {myStartCell, myEndCell} of sheet 1 of active workbook
    set value of myRange to myList
end tell
With ThisWorkbook.Worksheets(1)

Dim myStartCell As Range
Dim myEndCell As Range
Dim myRange As Range

Set myStartCell = Range("B18").Cells

Set myEndCell = myStartCell.Offset(rowOffset:=1, columnOffset:=3).Cells

Set myRange = .Range(myStartCell, myEndCell)

End With
使用上面的代码,我收到与该行相关的错误消息“您试图访问的对象不存在”

    set myRange to range {myStartCell, myEndCell} of sheet 1 of active workbook
在VBA中,类似代码如下所示:

set myList to {{"Red", "Grapefruit", "1", ""}, {"Julie", "Heron", "2", "*"}}

tell application "Microsoft Excel"
    set myStartCell to range ("B18") of sheet 1 of active workbook
    set myEndCell to get offset myStartCell row offset ((count myList) -1) column offset ((count item 1 of myList) -1)
    set myRange to range {myStartCell, myEndCell} of sheet 1 of active workbook
    set value of myRange to myList
end tell
With ThisWorkbook.Worksheets(1)

Dim myStartCell As Range
Dim myEndCell As Range
Dim myRange As Range

Set myStartCell = Range("B18").Cells

Set myEndCell = myStartCell.Offset(rowOffset:=1, columnOffset:=3).Cells

Set myRange = .Range(myStartCell, myEndCell)

End With
如何使用Applescript实现与VBA行相同的功能

    Set myRange = .Range(myStartCell, myEndCell)

??或者在给定起始单元格以及行数和列数的情况下,有没有更好的方法来创建一个范围?

在Excel中根据行数和列数创建一个范围是一件痛苦的事情。这将使用处理程序将数字转换为字母

set myList to {{"Red", "Grapefruit", "1", ""}, {"Julie", "Heron", "2", "*"}}

set numberOfColumns to (count item 1 of myList) - 1
set numberOfRows to (count myList) - 1
tell application "Microsoft Excel"
    set myStartCell to range "B18" of active sheet
    set myEndCell to get offset myStartCell row offset numberOfRows column offset numberOfColumns
    set columnLetter to my excelColumnToLetters(first column index of myEndCell)
    set myRange to range ("B18:" & columnLetter & first row index of myEndCell)
    set value of myRange to myList
end tell

-- converts the column number to the letter equivalent
to excelColumnToLetters(column)
    set letters to {}
    repeat while column > 0
        set remainder to column mod 26
        if remainder = 0 then set remainder to 26
        set beginning of letters to (remainder + 64)
        set column to (column - remainder) div 26
    end repeat
    return string id letters
end excelColumnToLetters

您可以使用
get address
命令

set myRange to range ("B18:" & (get address myEndCell)) of sheet 1 of active workbook
set value of myRange to myList


工作得很好。非常感谢。