Skip to content

Instantly share code, notes, and snippets.

@philadams-zz
Created June 30, 2011 16:04
Show Gist options
  • Save philadams-zz/1056551 to your computer and use it in GitHub Desktop.
Save philadams-zz/1056551 to your computer and use it in GitHub Desktop.
Excel: save all sheets as individual .csv files
set f to choose file
set outputDirectory to (path to downloads folder as text) & "Excel:converted:"
-- make sure the outputDirectory exists and if not create it
if outputDirectory ends with ":" then set outputDirectory to text 1 thru -2 of outputDirectory
do shell script "mkdir -p " & quoted form of POSIX path of outputDirectory
-- save all worksheets as csv files
set csvPaths to {}
tell application "Microsoft Excel"
open f
set theSheets to worksheets of active workbook
set workbookName to name of active workbook
if workbookName ends with ".xls" then set workbookName to text 1 thru -5 of workbookName
repeat with aSheet in theSheets
set thisPath to outputDirectory & ":" & workbookName & "." & name of aSheet & ".csv"
set end of csvPaths to thisPath
save aSheet in thisPath as CSV file format
end repeat
close active workbook without saving
end tell
-- convert csv files to UTF8 format
repeat with csvPath in csvPaths
try
set utf8Path to text 1 thru -4 of csvPath & "utf8.csv"
set csvText to read file csvPath
writeTo_UTF8(utf8Path, csvText, true)
end try
tell application "Finder" to delete file csvPath
end repeat
(*============= SUBROUTINES ================*)
on writeTo_UTF8(targetFile, theText, appendText)
try
set targetFile to targetFile as text
set openFile to open for access file targetFile with write permission
if appendText is false then
set eof of openFile to 0
write «data rdatEFBBBF» to openFile starting at eof -- UTF-8 BOM
else
tell application "Finder" to set fileExists to exists file targetFile
if fileExists is false then
set eof of openFile to 0
write «data rdatEFBBBF» to openFile starting at eof -- UTF-8 BOM
end if
end if
write theText as «class utf8» to openFile starting at eof
close access openFile
return true
on error theError
try
close access file targetFile
end try
return theError
end try
end writeTo_UTF8
@philadams-zz
Copy link
Author

now to figure out how to do this in Google Drive Spreadsheets...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment