I am just too lazy to move my Numbers spreadsheet to Excel. So it is harder to automate.
Here is an AppleScript snippet that can help with copy and pasting data between Numbers spreadsheets. You can use it to open a CSV file, then run this script snippet to copy and paste the data into the destination spreadsheet.
Why is this useful? If you have Excel, it is very easy to just manipulate the xlsx file with a python library and it works well. There is a numbers-parser python library, which you can do very basic stuff, but it messes up formulas and pivot tables. The safer way, but slower, is to do it with AppleScript which is kind of like automating your manual steps to copy and paste each cell to the right location. This will preserve your formulas and formats.
-- ================================================================
-- NUMBERS TABLE DATA COPY SCRIPT
-- ================================================================
-- This script copies all data from a source table to a destination table
-- in Apple Numbers. It includes error handling, user configuration, and
-- automatic table resizing.
-- ================================================================
-- USER CONFIGURATION SECTION
-- ================================================================
-- Modify these variables to match your specific documents and tables
-- Source document and table information
set sourceDocName to "sourceDocName" -- Name of source document (without .numbers extension)
set sourceTableName to "sourceTableName" -- Name of source table
set sourceSheetIndex to 1 -- Sheet number in source document (1-based)
-- Destination document and table information
set destDocName to "destDocName.numbers" -- Full name of destination document (with .numbers extension)
set destTableName to "destTableName" -- Name of destination table
set destSheetName to "destSheetName" -- Name of destination sheet
-- Copy settings
set startRow to 1 -- Starting row in destination table
set startColumn to 1 -- Starting column in destination table
set includeHeaders to true -- Whether to copy header rows
set clearDestination to false -- Whether to clear destination table first
-- ================================================================
-- MAIN SCRIPT EXECUTION
-- ================================================================
try
-- Activate Numbers application
tell application "Numbers"
activate
-- ================================================================
-- STEP 1: VALIDATE AND REFERENCE SOURCE DOCUMENT/TABLE
-- ================================================================
-- Check if source document exists
if not (exists document sourceDocName) then
error "Source document '" & sourceDocName & "' not found. Please ensure it's open in Numbers."
end if
-- Reference the source document and validate sheet
set sourceDoc to document sourceDocName
tell sourceDoc
if (count of sheets) < sourceSheetIndex then
error "Source document doesn't have sheet " & sourceSheetIndex
end if
set sourceSheet to sheet sourceSheetIndex
end tell
-- Reference and validate source table
tell sourceSheet
if not (exists table sourceTableName) then
error "Source table '" & sourceTableName & "' not found in sheet " & sourceSheetIndex
end if
set sourceTable to table sourceTableName
end tell
-- ================================================================
-- STEP 2: VALIDATE AND REFERENCE DESTINATION DOCUMENT/TABLE
-- ================================================================
-- Check if destination document exists
if not (exists document destDocName) then
error "Destination document '" & destDocName & "' not found. Please ensure it's open in Numbers."
end if
-- Reference the destination document and validate sheet
set destDoc to document destDocName
tell destDoc
if not (exists sheet destSheetName) then
error "Destination sheet '" & destSheetName & "' not found"
end if
set destSheet to sheet destSheetName
end tell
-- Reference and validate destination table
tell destSheet
if not (exists table destTableName) then
error "Destination table '" & destTableName & "' not found in sheet '" & destSheetName & "'"
end if
set destTable to table destTableName
end tell
-- ================================================================
-- STEP 3: EXTRACT DATA FROM SOURCE TABLE
-- ================================================================
tell sourceTable
-- Get table dimensions
set rowCount to count of rows
set columnCount to count of columns
-- Validate that source table has data
if rowCount = 0 or columnCount = 0 then
error "Source table is empty (no rows or columns)"
end if
-- Extract all cell values as a flat list
-- Numbers returns values row by row, left to right
set sourceData to value of every cell
-- Get row and column information for better error reporting
display notification "Copying " & rowCount & " rows and " & columnCount & " columns" with title "Numbers Copy Script"
end tell
-- ================================================================
-- STEP 4: PREPARE DESTINATION TABLE
-- ================================================================
tell destTable
-- Clear destination table if requested
if clearDestination then
-- Clear all existing data (set to empty string)
set value of every cell to ""
display notification "Cleared destination table" with title "Numbers Copy Script"
end if
-- Get current destination table dimensions
set currentRows to count of rows
set currentColumns to count of columns
-- Calculate required dimensions based on starting position and source data
set neededRows to startRow + rowCount - 1
set neededColumns to startColumn + columnCount - 1
-- ================================================================
-- STEP 5: RESIZE DESTINATION TABLE IF NEEDED
-- ================================================================
-- Add rows if destination table doesn't have enough
if neededRows > currentRows then
set rowsToAdd to neededRows - currentRows
repeat rowsToAdd times
add row below last row
end repeat
display notification "Added " & rowsToAdd & " rows to destination table" with title "Numbers Copy Script"
end if
-- Add columns if destination table doesn't have enough
if neededColumns > currentColumns then
set columnsToAdd to neededColumns - currentColumns
repeat columnsToAdd times
add column after last column
end repeat
display notification "Added " & columnsToAdd & " columns to destination table" with title "Numbers Copy Script"
end if
end tell
-- ================================================================
-- STEP 6: COPY DATA CELL BY CELL
-- ================================================================
tell destTable
-- Initialize data index for flat list traversal
set dataIndex to 1
set cellsCopied to 0
-- Loop through each row of source data
repeat with r from 1 to rowCount
-- Loop through each column of current row
repeat with c from 1 to columnCount
-- Calculate target position in destination table
set targetRow to startRow + r - 1
set targetColumn to startColumn + c - 1
-- Copy data from flat list to specific cell
-- Handle potential null/missing values
set cellValue to item dataIndex of sourceData
if cellValue is missing value then
set cellValue to ""
end if
set value of cell targetRow of column targetColumn to cellValue
-- Increment counters
set dataIndex to dataIndex + 1
set cellsCopied to cellsCopied + 1
end repeat
-- Optional: Add small delay every 10 rows for stability with large datasets
if r mod 10 = 0 then
delay 0.1
end if
end repeat
-- ================================================================
-- STEP 7: COMPLETION NOTIFICATION
-- ================================================================
display notification "Successfully copied " & cellsCopied & " cells!" with title "Numbers Copy Complete"
end tell
end tell
-- ================================================================
-- ERROR HANDLING
-- ================================================================
on error errMsg number errNum
-- Display user-friendly error message
tell application "Numbers"
display alert "Copy Operation Failed" message "Error " & errNum & ": " & errMsg buttons {"OK"} default button "OK"
end tell
-- Log error for debugging
log "Numbers Copy Script Error: " & errMsg
return false
end try
-- Script completed successfully
return true
Reference:
* https://www.macscripter.net/t/applying-formats-in-copy-paste-script-from-one-numbers-file-to-another/73448/3
* https://github.com/masaccio/numbers-parser/issues/73
* https://pypi.org/project/numbers-parser/
Leave a Comment