If I understood well, the script below will transfer the datas as required.
CAUTION. As I don’t like to call sheets or tables by their index I call them by names.
script o
# These properties will be visible from the handler but will not be saved on disk
property peopleNames : {}
property targetName : missing value
end script
set path2Target to (path to desktop as text) & "mainDocument.numbers" # Edit to fit your needs
set folder4peoples to (path to desktop as text) & "peoples" # Edit to fit you needs
tell application "Finder"
set peopleFiles to every file of folder folder4peoples as alias list
end tell
# Now we have a list of the available people files
# The main document is supposed to be
tell application "Numbers"
open path2Target as «class furl»
set o's targetName to name of document 1
tell document 1 to tell sheet "sheet1"
if not (exists table "fake") then
make new table with properties {name:"fake", header row count:1, header column count:0, row count:10, column count:2}
end if
end tell
set o's peopleNames to {}
repeat with aPeopleFile in peopleFiles
set aPeopleDoc to open aPeopleFile
set end of o's peopleNames to name of aPeopleDoc
end repeat
end tell
# Now we have a list of the available people documents
# I'm not sure that the upper row to import is the same in every tables
# So I define several ones.
set upper1_1 to 2
set upper1_2 to 2
set upper1_6 to 2
set upper3_1 to 2
set upperInMain to 2
repeat with aPeopleName in o's peopleNames # Edit sheets and tables names to fit your needs.
my importFrom(aPeopleName, "sheet1", "table1-1", {"B", "I", "K"}, upper1_1, upperInMain)
my importFrom(aPeopleName, "sheet1", "table1-2", {"A", "B", "C", "D", "E"}, upper1_2, upperInMain)
my importFrom(aPeopleName, "sheet1", "table1-6", "A", upper1_6, upperInMain) # I deliberately missed the brackets
my importFrom(aPeopleName, "sheet3", "table3-1", {"A", "F"}, upper3_1, upperInMain)
tell application "Numbers" to tell document (o's targetName) to tell sheet 1 to tell table "fake" to tell column 1
repeat with i from (count rows) to 1 by -1
if value of cell i is not missing value then exit repeat
end repeat
# i is the index of the lower row used
if i = (count rows) then
add row below last row
end if
end tell
set upperInMain to i
# So datas from the next people will be pasted starting from the same row in every table.
end repeat
# Remove the fake table
tell application "Numbers" to tell document (o's targetName) to tell sheet 1 to delete table "fake"
on importFrom(aPeopleName, sheetName, tableName, theColumns, higherRowInPeople, higherRowInTarget)
# Take care of possible omission of brackets when there is a single column to treat
if class of theColumns is not list then set theColumns to {theColumns}
set windowMenu to 11 # Index of the menu "Window"
copy higherRowInTarget to upperRow
tell application "Numbers"
# Scan the table of every peoples to determine the lower row filled
repeat with c from 1 to count theColumns
set aColumn to (item c of theColumns) as text
tell document aPeopleName
tell sheet sheetName to tell table tableName
tell column aColumn
repeat with i from (count rows) to 1 by -1
if value of cell i is not missing value then exit repeat
end repeat
set theRange to (name of cell higherRowInPeople) & ":" & (name of cell i)
end tell
set selection range to range theRange
end tell # sheet.
end tell # document
tell application "System Events" to tell process "Numbers"
set frontmost to true
tell menu bar 1 to tell menu bar item windowMenu to tell menu 1 to click menu item aPeopleName # brings the people window to front
keystroke "c" using {command down}
end tell
# the clipboard contains the datas from the people
tell document (o's targetName) to tell sheet sheetName to tell table tableName
if (upperRow > (count rows)) then
repeat (upperRow - (count rows)) times
add row below last row
end repeat
end if
set selection range to range (name of cell upperRow of column aColumn)
end tell
tell application "System Events" to tell process "Numbers"
tell menu bar 1 to tell menu bar item windowMenu to tell menu 1 to click menu item (o's targetName) # brings the main window to front
keystroke "v" using {command down}
end tell
tell document (o's targetName) to tell sheet 1 to tell table "fake"
if (upperRow > (count rows)) then
repeat (upperRow - (count rows)) times
add row below last row
end repeat
end if
set selection range to range (name of cell upperRow of column 1)
end tell
tell application "System Events" to tell process "Numbers"
--tell menu bar 1 to tell menu bar item windowMenu to tell menu 1 to click menu item (o's targetName) # brings the main window to front
keystroke "v" using {command down}
end tell
end repeat
end tell # Numbers
#
end importFrom
Yvan KOENIG running Sierra 10.12.4 in French (VALLAURIS, France) lundi 10 avril 2017 17:17:48