Convert Numbers Table to .txt file

Looking to take data from a table in numbers and create a plain text .txt file from it. Trying to find a better solution than copy/paste.


I don't think its supported natively in Numbers, but feel like it should be doable via Script Editor. An old post I found tries to achieve it, but the code doesn't seem to execute for me even after I clean it up, and it creates a TSV file whereas I need a .txt file. Anyone had any success with something similar?


Old post: https://discussions.apple.com/thread/7959743

Posted on Mar 31, 2019 6:00 PM

Reply
8 replies

Mar 31, 2019 8:42 PM in response to SGIII

Getting into the reasoning for the underscore will be confusing, so will just stick to the issue I'm having. The cells that require the underscore prefix in Numbers are formatted using a custom Data Format, which I apply to an entire column. Some cells in that column are blank, and display as blank in Numbers. But when I export the CSV, those blank cells export with an underscore.


My workaround has been Find and Replace "_," in the .txt file, but again just adds another step to the process, so trying to do things properly in Numbers. Attached are screenshots as requested.


Mar 31, 2019 6:31 PM in response to SGIII

Thanks so much for the reply! Admittedly I'm not fluent with scripting, but using your previous sample script, I attempted replacing the TSV instances with TXT but still hitting some sticking points where I can't get the script to execute properly. Its creating a blank file, but not filling values, and returns an error:


error "The variable tabbedTxttof is not defined." number -2753 from "tabbedTxttof"


Thinking "tabbedTxtof" just ins't copying from the original post properly, but not sure how it should actually be.


Yes, importing the .txt into another program which will read the values - a photo editor that will use the values as Keywords for file metadata. Unfortunately it will only accept .txt. Having the Keywords in an orderly spreadsheet which I update frequently is ideal, just searching to transfer them to a text file in the least cumbersome way.



Mar 31, 2019 7:23 PM in response to Wayne Contello

I didn't realize just changing the extension would work so easily. Originally my workflow involves separating values with semi colons, but was able to change it to look for coma's instead. Thanks for that idea!


Part of why I don't want to use copy / paste is I'm combining multiple sheets into one CSV, so just trying to reduce the number of steps - going back and forth between sheets and copying pasting on a daily basis, etc. I also was having trouble having the coma/semicolon separators copy over into the text file. But generally seems things are resolved by changing the file extension.


Somewhat unrelated but part of the same project, I'm using a custom Data Format on some cells to append an underscore prefix so some of the Keywords so they appear top of list. It displays perfectly in the spreadsheet, but it seems during the convert to CSV process, any empty cell with this Data Format is exporting the underscore still. Any ideas on how to have empty cells export as blank with CSV or only apply Data Format to cells with a non-zero data in them? Thanks again!

Mar 31, 2019 8:27 PM in response to Teefussurf

Glad my suggestion of changing the file extension helps. I don't understand your use of underscores. Could you post a screenshot showing specifics of what you are doing and hoping to do?


Shift-command-4 will capture a screenshot of part of the display. Then use the 'mountains' icon in the editor here to insert it into your post.


SG

Mar 31, 2019 10:24 PM in response to Teefussurf

You can use a script like the one below to export comma-separated data in a file with a .txt extension, stripping any leading underscores.


This requires you to click a table you want to export before running.


Note that it will overwrite any existing file you have of that name.


It is of course possible to have the script export more than one table at a time but that would requires specifics on how your document is set up.


The first time make sure Script Editor and Numbers are listed and checked at System Preferences > Security & Privacy > Privacy > Accessibility.


SG


tell application "Numbers"
	tell front document's active sheet
		tell (first table whose selection range's class is range)
			set vv to rows's (cells whose value is not missing value)'s value
		end tell
	end tell
end tell

-- convert to comma-separated values
set commafiedTxt to ""
set text item delimiters to {","}
repeat with r in vv's items
	-- strip any leading underscores
	repeat with i from 1 to r's length
		set iTxt to r's item i's contents
		if iTxt starts with "_" then set r's item i to iTxt's text 2 thru -1
	end repeat
	set commafiedTxt to commafiedTxt & r & return
end repeat

set the clipboard to commafiedTxt

-- save to file wherever convenient
set fName to (choose file name with prompt "Save as File" default name "My Text File") as string
if fName does not end with ".txt" then set fName to fName & ".txt"
set f to open for access file fName with write permission
set eof f to 0 # overwrite existing contents from beginning
write commafiedTxt to f
close access f



This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Convert Numbers Table to .txt file

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.