jmcnamara / xlsxwriter.lua

A lua module for creating Excel XLSX files.
http://xlsxwriterlua.readthedocs.org/
MIT License
142 stars 52 forks source link

Feature request: function protect() #4

Closed pbasslest closed 10 years ago

pbasslest commented 10 years ago

Hi John, first of all thanks for you work :) This feature request is about the function protect() which looks not implemented. According to the documentation and in order to have a cell protected the following lines should be executed:

locked = workbook:add_format()
locked:set_locked(true)

unlocked = workbook:add_format()
locked:set_locked(false)

-- Enable worksheet protection
worksheet:protect()

-- This cell cannot be edited.
worksheet:write("A1", "=1+2", locked)

-- This cell can be edited.
worksheet:write("A2", "=1+2", unlocked)

..but I've come across an error:

/usr/bin/lua: xxxxxxxxx: attempt to call method 'protect' (a boolean value)

Thanks a lot!

jmcnamara commented 10 years ago

I've added initial support for this worksheet protection to the master branch.

The following now works:

local Workbook = require "xlsxwriter.workbook"

local workbook  = Workbook:new("protection.xlsx")
local worksheet = workbook:add_worksheet()

-- Create some cell formats with protection properties.
unlocked = workbook:add_format({locked = false})
hidden   = workbook:add_format({hidden = true})

-- Format the columns to make the text more visible.
worksheet:set_column('A:A', 40)

-- Turn worksheet protection on.
worksheet:protect()

-- Write a locked, unlocked and hidden cell.
worksheet:write('A1', 'Cell B1 is locked. It cannot be edited.')
worksheet:write('A2', 'Cell B2 is unlocked. It can be edited.')
worksheet:write('A3', "Cell B3 is hidden. The formula isn't visible.")

worksheet:write_formula('B1', '=1+2')  -- Locked by default.
worksheet:write_formula('B2', '=1+2', unlocked)
worksheet:write_formula('B3', '=1+2', hidden)

workbook:close()

It still required more tests and password protection isn't supported but it should work for simpler cases.

John

pbasslest commented 10 years ago

This morning I've tested the new feature and it seems to be working. I'll keep on testing. Thanks!

jmcnamara commented 10 years ago

This morning I've tested the new feature and it seems to be working. I'll keep on testing.

That's good, thanks for letting me know.

John

jmcnamara commented 10 years ago

Added in version 0.0.6. This should be up in luarocks and luadist in the next day or so.

See the updated docs on the protect method and the example.