Skip to content

Instantly share code, notes, and snippets.

@schaunwheeler
Last active December 18, 2015 15:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save schaunwheeler/5805242 to your computer and use it in GitHub Desktop.
Save schaunwheeler/5805242 to your computer and use it in GitHub Desktop.
A function to convert an Excel workbook to CSV files by using Excel's native Visual Basic capabilities. Only works on Windows.
# The MIT License (MIT)
#
# Copyright (c) 2012 Schaun Jacob Wheeler
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
excelToCsv <- function(file_path, keep_sheets = NULL, target_dir = NULL, ...) {
temp_already <- list.files(tempdir())
if(is.null(target_dir)) {
file_root <- gsub("([[:print:]]+(/|\\\\))[[:print:]]+", "\\1", file_path)
} else if(!is.null(target_dir) & target_dir != FALSE) {
file_root <- target_dir
}
file_name <- gsub("[[:print:]]+(/|\\\\)", "", file_path)
file_ext <- gsub("[[:print:]]+(.xls.?)", "\\1", file_path)
converter_file <- file(paste0(tempdir(),"/", "converter.vbs"))
writeLines(
c('rem XLS_To_CSV.vbs',
'rem =============================================================',
'rem convert all NON-empty worksheets in an Excel file to csv',
'rem CSV file names will default to Sheet names',
'rem output folder defaults to the folder where the script resides or',
'rem if path is specified with the input file, that path is used',
'rem ',
'rem input parameter 1: Excel path\\file in argument 1 ',
'rem (if path is not specified, the current path is defaulted)',
'rem ',
'rem ============================================================',
'',
'Dim strExcelFileName',
'Dim strCSVFileName',
'',
'strExcelFileName = WScript.Arguments.Item(0)',
'',
'rem get path where script is running',
'Set fso = CreateObject ("Scripting.FileSystemObject")',
'strScript = Wscript.ScriptFullName',
'strScriptPath = fso.GetAbsolutePathName(strScript & "\\..")',
'',
'rem If the Input file is NOT qualified with a path, default the current path',
'LPosition = InStrRev(strExcelFileName, "\\") ',
'if LPosition = 0 Then ',
' strExcelFileName = strScriptPath & "\\" & strExcelFileName',
'strScriptPath = strScriptPath & "\\" ',
'else ',
'strScriptPath = Mid(strExcelFileName, 1, LPosition) ',
'End If',
'rem msgbox LPosition & " - " & strExcelFileName & " - " & strScriptPath',
'',
'Set objXL = CreateObject("Excel.Application")',
'Set objWorkBook = objXL.Workbooks.Open(strExcelFileName)',
'objXL.DisplayAlerts = False',
'',
'rem loop over worksheets',
' For Each sheet In objWorkBook.Sheets ',
'if objXL.Application.WorksheetFunction.CountA(sheet.Cells) <> 0 Then ',
'rem sheet.Rows(1).delete',
'sheet.SaveAs strScriptPath & sheet.Name & ".csv", 6',
' End If',
' Next',
'',
'rem clean up ',
'objWorkBook.Close ',
'objXL.quit',
'Set objXL = Nothing ',
'Set objWorkBook = Nothing',
'Set fso = Nothing',
'',
'rem end script'),
con = converter_file)
close(converter_file)
file.copy(file_path, tempdir())
orig_wd <- getwd()
setwd(tempdir())
file.rename(file_name, paste0("filetoconvert", file_ext))
shell(paste("converter.vbs",
paste0("filetoconvert", file_ext)), intern = TRUE)
setwd(orig_wd)
if(is.null(keep_sheets)) {
keep_sheets <- gsub("\\.csv", "", list.files(tempdir(), pattern = "\\.csv"))
}
file_flags <- paste0(keep_sheets, ".csv")
if(is.null(target_dir) | (!is.null(target_dir) & target_dir != FALSE)) {
for(i in 1:length(file_flags)) {
file.copy(
paste0(tempdir(), "/", file_flags[i]), file_root, overwrite = TRUE)
}
} else {
all_files <- lapply(file_flags, function(x) {
csv_file <- read.csv(paste0(tempdir(), "/", x),
as.is = TRUE, na.strings = c("#N/A", "NA", "N/A", "?", ""))
csv_file[,sapply(csv_file, function(y) mean(is.na(y), na.rm = TRUE)) < 1]
})
if(length(all_files) == 1) {
all_files <- all_files[[1]]
} else {
names(all_files) <- keep_sheets
}
}
suppressWarnings(file.remove(
paste0(tempdir(),
"/",
list.files(tempdir())[!(list.files(tempdir()) %in% temp_already)])))
if(!is.null(target_dir) & target_dir == FALSE) {
all_files
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment