Estoy tratando de usar Applescript para insertar valoresen celdas en una hoja de cálculo de Excel. Los valores se mantienen en una variable Applescript como una lista de listas (ver código de ejemplo). Ya me di cuenta de que si inserto esta variable en un rango del tamaño correcto (filas y columnas), aparece correctamente con todo en el lugar correcto.
Desafortunadamente, la cantidad de datos que estoy insertandopuede variar, así que en lugar de codificar el tamaño del rango, necesito calcularlo a partir del número de elementos en la lista. Estoy usando el tamaño de la lista para calcular un desplazamiento desde la celda inicial y luego intento crear un rango que se extienda desde la celda inicial hasta el desplazamiento calculado de la celda.
Reducir al mínimo, el código es el siguiente:
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
Con el código anterior, recibo el mensaje de error "El objeto al que intenta acceder no existe", relacionado con la línea
set myRange to range {myStartCell, myEndCell} of sheet 1 of active workbook
En VBA, un código similar sería el siguiente:
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
¿Cómo puedo usar Applescript para cumplir la misma función que la línea VBA?
Set myRange = .Range(myStartCell, myEndCell)
? ¿O hay una mejor manera de crear un rango dada la celda de inicio y el número de filas y columnas?
Respuestas
1 para la respuesta № 1Puedes usar el get address
mando.
set myRange to range ("B18:" & (get address myEndCell)) of sheet 1 of active workbook
set value of myRange to myList
O
set myRange to range ((get address myStartCell) & ":" & (get address myEndCell)) of sheet 1 of active workbook
set value of myRange to myList
1 para la respuesta № 2
Crear un rango en Excel a partir de números de filas y columnas es un dolor. Esto utiliza un controlador para convertir números en 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