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()
}