Craft79 wrote:
I have an exists Numbers sheet, and inside I already have tables. every table have the same name of *.csv file, and I would like to copy each file in its table.
😉
You can try the script below.
- Copy-paste into Script Editor (in Applications > Utilities)
- Make sure Script Editor.app is checked at System Preferences > Security & Privacy > Privacy > Accessibility.
- Gather your CSV files together into one folder, and make sure their names (minus the extension) match exactly one table in your document. For example if your file is named 'mydata1.csv' then make sure you have one table in your document named 'mydata1'.
- With your Numbers document open, click the 'Run' button and choose the folder at the prompt.
As written it will paste the data at A2 of each table, but you can change that in the first line of the script.
Be sure to test on a backup of your document. As written the script overwrites old data.
SG
property tgtCell : "A2" # paste block at A2 of each table (change to A1, etc as needed)
tell application "Finder"
set fldr to choose folder with prompt "Choose folder with CSV files to import to Numbers"
set ff to fldr'sfiles as alias list
end tell
# process each file
repeat with f in ff
tell application "Finder" to set theExt to f's name extension
if theExt is in {"csv", "txt"} then
# save the name less the .csv or .txt extension
tell application "Finder" to set fileName to (f's name as text)'s text 1 thru -5
# read file
set csvTxt to (readfas «class utf8») -- may need to remove: as «class utf8»
# convert to TSV (for pasting into Numbers)
set tsvTxt to csvToTSV(csvTxt)
set tFound to false# flag in case matching table not found
pasteToNumbers(fileName, tsvTxt, tFound)
end if
end repeat
to pasteToNumbers(fileName, tsvTxt, tFound)
set the clipboard totsvTxt
tell application "Numbers"
tell front document
repeat with s in sheets
repeat with t in s's tables
set tblName to t's name as text
if fileName is tblName then
set tFound to true
deletet'srows 3 thru -1 # remove rows with old data
deletet'scolumns 2 thru -1 # remove columns to right
activate
tell t to set selection range to range tgtCell
delay 0.3
tell application "System Events" to keystroke ¬
"v" using {option down, shift down, command down}
end if
end repeat
end repeat
if tFound is false then display dialog "Table '" & fileName & "' not found" buttons "Ok"
end tell
end tell
end pasteToNumbers
to csvToTSV(csvTxt) # convert to TSV for pasting
set text item delimiters to quote
set ti to csvTxt's text items
repeat with i from 1 to count ti by 2
set nonQItem to (a reference to ti's item i)
-- odd-numbered items are the ones not surrounded by quotes
if nonQItem'slength is 0 then -- if adjacent "" add back a quote
set nonQItem to quote
else -- otherwise replace commas with tabs
set nonQItem'scontents to my replace(nonQItem, ",", tab)
end if
end repeat
return ti as text
end csvToTSV
to replace(tt, x, y) # generic replace
set text item delimiters to x
set tmp to tt's text items
set text item delimiters to y
return tmp as string
end replace