-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexcel_parser.R
More file actions
188 lines (143 loc) · 5.79 KB
/
excel_parser.R
File metadata and controls
188 lines (143 loc) · 5.79 KB
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#!/usr/bin/env Rscript
#######
# Tested on R 3.6.2
## Written by B. Seelbinder, 2020-02-01
##################
#== Try to load packages. Install if not present ====
# Help output string if something goes wrong
CMDstring <- "
Rscript excel_parser.R <Eingabe.xlsx> <Ausgabe> [--add_prices]
<Eingabe.xlsx> Pfad zu einer '.xls' oder '.xlsx' Datei.
<Ausgabe> Präfix zur Ausgabe von Dateien. Für jede valide Seite in der
Eingabedatei wird eine separate Ausgabedatei angelegt.
--add_prices Ist optional. Wenn angegeben, werden Einzelverkaufspreise
mit ausgegeben.
"
#' Loads a package. Installs it if necessary.
#'
#' @param pkg Character(n). Name of CRAN library.
try_load_package <- function(pkg) {
if (!require(pkg, character.only = TRUE, quietly = T)) {
# pkg not installed
install.packages(pkg, character.only = TRUE, quiet = T)
tryCatch({
library(pkg, character.only = T)
}, except = function(e) {
stop("Laden von Paket schlug fehl: ", pkg)
})
}
}
try_load_package("dplyr")
try_load_package("tidyr")
try_load_package("magrittr")
try_load_package("stringr")
try_load_package("readxl")
#try_load_package("tidyxl") # only required of formats shall be parsed
message(print(" >> Script erfolgreich geladen."))
#== Read Input Arguments ====
args = commandArgs(trailingOnly=TRUE)
# for testing, args gets overwritten
if ( length(args) == 1 && args[[1]] == "TEST1" ) {
args <- c("Kontrolle_210-07977-AG_JAB-Hebeanlage_171219.xlsx", "Kontrolle_", "--add_prices")
} else if ( length(args) == 1 && args[[1]] == "TEST2" ) {
args <- c("210-07088-3-MH_Seume__Burstmaschine_220217.xlsx", "Burst_", "--add_prices")
}
# Positional arguments# Positional arguments
if ( length(args) < 2 ) {
stop("Es müssen Eingabe- und Ausgabe-Dateien angegeben werden.\n", CMDstring, call. = F)
}
# Optional Arguments
add_prices <- FALSE
if ( length(args) > 2 ) {
# Currently, there is only one possible input argument
if ( args[3] == "--add_prices" )
add_prices <- T
else
stop(sprintf("Unbekanntes, optionales argument: '%s'\n", args[3]), CMDstring)
}
input_file <- args[1] # file path to input Excel file
output_pref <- args[2] # Prefix for file output. Mutliple files are writting, starting with this string
if (!file.exists(input_file))
stop(sprintf("Eingabedatei nicht gefunden: '%s'", input_file))
if ( output_pref == "" || grepl("^\\.", output_pref) )
stop("Name für Ausgabedatei ungültig. Der Dateiname sollte nicht mit einem '.' beginnen und nicht leer sein. Dateiname: '", output_file, "'")
dir.create(dirname(output_pref), recursive = T, showWarnings = F)
message(sprintf(" >> Eingabedatei: '%s'", input_file))
message(sprintf(" >> Ausgabepräfix: '%s'", output_pref))
#== Parse Excel Files ====
# Tasks
# - Main entries do not have numbers, or fabricants
# - remove items with 0 abundance
# - if type is missing, do not output it
# - some pages are irrelevant for the units. detect & ignore them
# - add prices optional
# - parse each sheet into separat text outputs
sheets <- readxl::excel_sheets(input_file)
# iterate all sheets
for (sheet in sheets) {
datadf <- suppressWarnings(readxl::read_excel(input_file, sheet = sheet))
# check if these are actual pricing sheets
if (!("Bezeichnung" %in% colnames(datadf)))
next()
# clean data
datadf %<>%
select(Bezeichnung, Typ, Hersteller, Anzahl, contains("EP")) %>%
#drop_na(Bezeichnung) %>%
filter(is.na(Anzahl) | Anzahl > 0) %>%
mutate(Titel = NA)
# remove trailing NA
last_valid <- datadf$Bezeichnung %>% is.na %>% not %>% which %>% last
datadf <- datadf[1:last_valid,]
# find Einzelpreis column (in case it is needed)
ep_col <- which(grepl("EP", colnames(datadf)))
# find Bezeichung with 'Klemme' and report the words
klemme_index <- grepl(x = datadf$Bezeichnung, "klemme") %>% which
if ( length(klemme_index) > 0 ) {
message(sprintf("%s\n%s\n%s",
"Mindestens eine Bezeichunng enthält das Wort 'Klemme' und wird zusammengefasst.",
"Bitte alle Bezeichnungen prüfen. Wurde ein falscher Begriff gewählt, muss das",
"Skript angepasst werden!"))
print(unique(datadf$Bezeichnung[klemme_index]))
}
# '1 Satz Klemmen' should occur only once per title. This switch decides if 'Klemme' needs to be written out for the current title.
group_klemme <- TRUE
output_lines <- list() # to be written into a new file
for (ir in 1:nrow(datadf)) {
# A cell can either be:
# A title
# An item
# A blank cell
cell <- datadf[ir,]
if ( cell %$% { !is.na(Bezeichnung) & is.na(Typ) & is.na(Hersteller)} ) {
# Title line
title <- sprintf("%s:", cell$Bezeichnung)
output_lines <- c(output_lines, title)
message(sprintf("Found title line: '%s'", title))
group_klemme <- TRUE
} else if ( !is.na(cell$Bezeichnung) ) {
# Item Line
# These may further be grouped
if ( grepl(x = cell$Bezeichnung, "klemme,")[1] ) {
if (group_klemme) {
output_lines <- c(output_lines, sprintf("1 Satz Klemmen"))
group_klemme <- FALSE
}
} else {
output_lines <- c(output_lines, sprintf("%i Stk %s\n Fabrikat: %s", cell$Anzahl, cell$Bezeichnung, cell$Hersteller)) %>%
str_split("\n") %>%
unlist
if (!is.na(cell$Typ))
output_lines <- c(output_lines, sprintf(" Typ: %s", cell$Typ))
if (add_prices)
output_lines <- c(output_lines, sprintf(" EP: %s", cell[[ep_col]]))
}
} else {
# Blank Line
output_lines <- c(output_lines, "")
}
}
output_lines %<>% unlist %>% str_remove_all("\\r")
output_file <- paste0(output_pref, ".", sheet, ".txt")
message(" >> Writing output to: ", output_file)
writeLines(output_lines, output_file)
}