I have checked the excelize package, there's the ProtectSheet
function. However this only protects the sheet from changes etc not protecting from the unauthorised access.
I have look for several other options/packages and they don't seem to offer such capabilities.
I know that ultimately I could still zip-password protect the excel file, but being able to protect the workbook itself is much preferable.
After some research it seems there's no Go packages that can achieve this. However I've come across a npm package - secure-spreadsheet that is able to password protect an excel file.
As I needed to program the password protect operation in Go, what i did was to trigger an os command in the Go code to invoke the npm package to password protect the file.
The Go program is deployed & executed in an alpine container, which i can install the node-npm
and have access to npx
command.
code sample:
func passProtectExcelWorkbook(filename, outFilename string) error {
passwd := "password"
cat := exec.Command("cat", filename)
excel := exec.Command("npx", "secure-spreadsheet", "--password", passwd, "--input-format", excelExt)
file, err := os.Create(outFilename)
if err != nil {
return fmt.Errorf("error when creating excel file: %v err: %v", outFilename, err)
}
defer file.Close()
excel.Stdin, err = cat.StdoutPipe()
if err != nil {
return fmt.Errorf("error when reading from cat command output: %v", err)
}
excel.Stdout = file
if err := excel.Start(); err != nil {
return fmt.Errorf("error when starting npx command: %v", err)
}
if err := cat.Run(); err != nil {
return fmt.Errorf("error when running cat command: %v", err)
}
return excel.Wait()
}