/ / Criando intervalo do Excel dado célula inicial e deslocamento de linha / coluna, em Applescript - applescript

Criando intervalo do Excel dado célula inicial e deslocamento de linha / coluna, em Applescript - applescript

Eu estou tentando usar o Applescript para inserir valoresem células em uma planilha do Excel. Os valores são mantidos em uma variável Applescript como uma lista de listas (consulte o código de exemplo). Eu já trabalhei que se eu inserir essa variável em um intervalo do tamanho correto (linhas e colunas), ele entra corretamente com tudo no lugar certo.

Infelizmente, a quantidade de dados que estou inserindopode variar, então, em vez de codificar o tamanho do intervalo, preciso calculá-lo do número de itens na lista. Eu estou usando o tamanho da lista para elaborar um deslocamento da célula inicial e, em seguida, tentando criar um intervalo que se estende da célula inicial para o deslocamento de célula calculado.

Reduzido ao mínimo, o código é o seguinte:

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

Com o código acima, recebo a mensagem de erro "O objeto que você está tentando acessar não existe", relacionado à linha

    set myRange to range {myStartCell, myEndCell} of sheet 1 of active workbook

No VBA, código semelhante seria o seguinte:

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

Como posso usar o Applescript para cumprir a mesma função que a linha VBA?

    Set myRange = .Range(myStartCell, myEndCell)

? Ou existe uma maneira melhor de criar um intervalo, dada a célula inicial e o número de linhas e colunas?

Respostas:

1 para resposta № 1

Você pode usar o get address comando.

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

Ou

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

1 para resposta № 2

Criar um intervalo no Excel a partir de números de linha e coluna é uma dor. Isso usa um manipulador para converter números em letras

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