Last active

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist

A function to convert an Excel workbook to CSV files by using Excel's native Visual Basic capabilities. Only works on Windows.

View excelToCsv.R
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
# 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
Something went wrong with that request. Please try again.