You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

111 lines
1.7 KiB

#!/bin/sh
usage() {
cat << EOF
USAGE:
$0 [options]
OPTIONS:
-i Input file (required)
-o Output file (required)
-v Verbose
-h Show this help
EOF
}
error() {
if [ -n "$1" ]; then
printf "%s\\n" "$1" >/dev/stderr
fi
exit 1
}
exitIfEmpty() {
if [ -z "$1" ]; then
error "$2"
fi
}
log() {
if [ "$VERBOSE" -eq 1 ]; then
printf "%s\\n" "$1"
fi
}
unzipExcel() {
if [ "$VERBOSE" -eq 0 ]; then
verbosity=-qq
fi
unzip $verbosity -d "$TEMP_DIRECTORY/excel" "$INPUT_FILE"
}
createExcel() {
current_dir="$(pwd)"
if [ "$VERBOSE" -eq 0 ]; then
verbosity=-q
fi
cd "$TEMP_DIRECTORY/excel"
zip $verbosity -r "../result.xslx" ./*
cd "$current_dir"
mv "$TEMP_DIRECTORY/result.xslx" "$OUTPUT_FILE"
log "$OUTPUT_FILE generated"
}
createTmpDirectory() {
TEMP_DIRECTORY="$(mktemp -d)"
log "Temp directory created: $TEMP_DIRECTORY"
}
removePassword() {
sed -i 's#<sheetProtection[^>]*>##' "$1"
}
removePasswords() {
grep -Rn sheetProtection "$TEMP_DIRECTORY" | cut -d: -f1 | while read -r file; do
removePassword "$file"
log "Password removed in: $file"
done
}
cleanUp() {
rm -fr "$TEMP_DIRECTORY" && log "Temp directory removed: $TEMP_DIRECTORY"
}
ERROR=0
VERBOSE=0
while getopts "hvi:o:" option; do
case "$option" in
i) INPUT_FILE="$OPTARG";;
o) OUTPUT_FILE="$OPTARG";;
h) usage; exit 0;;
v) VERBOSE=1;;
:) ERROR=1;;
?) ERROR=1;;
esac
done
if [ "$ERROR" -eq 1 ]; then
error "$(usage)"
fi
exitIfEmpty "$INPUT_FILE" "Input file is missing (-i)"
exitIfEmpty "$OUTPUT_FILE" "Output file is missing (-o)"
if [ ! -f "$INPUT_FILE" ]; then
error "No such file: $INPUT_FILE"
fi
createTmpDirectory
unzipExcel
removePasswords
createExcel
cleanUp