class Axlsx::SheetProtection
The SheetProtection
object manages worksheet protection options per sheet.
Attributes
Password hash @return [String] default nil
Specifies the salt which was prepended to the user-supplied password before it was hashed using the hashing algorithm @return [String]
Public Class Methods
Creates a new SheetProtection
instance @option options [Boolean] sheet @see SheetProtection#sheet @option options [Boolean] objects @see SheetProtection#objects @option options [Boolean] scenarios @see SheetProtection#scenarios @option options [Boolean] format_cells @see SheetProtection#objects @option options [Boolean] format_columns @see SheetProtection#format_columns @option options [Boolean] format_rows @see SheetProtection#format_rows @option options [Boolean] insert_columns @see SheetProtection#insert_columns @option options [Boolean] insert_rows @see SheetProtection#insert_rows @option options [Boolean] insert_hyperlinks @see SheetProtection#insert_hyperlinks @option options [Boolean] delete_columns @see SheetProtection#delete_columns @option options [Boolean] delete_rows @see SheetProtection#delete_rows @option options [Boolean] select_locked_cells @see SheetProtection#select_locked_cells @option options [Boolean] sort @see SheetProtection#sort @option options [Boolean] auto_filter @see SheetProtection#auto_filter @option options [Boolean] pivot_tables @see SheetProtection#pivot_tables @option options [Boolean] select_unlocked_cells @see SheetProtection#select_unlocked_cells @option options [String] password. The password required for unlocking. @see SheetProtection#password=
# File lib/axlsx/workbook/worksheet/sheet_protection.rb, line 29 def initialize(options={}) @objects = @scenarios = @select_locked_cells = @select_unlocked_cells = false @sheet = @format_cells = @format_rows = @format_columns = @insert_columns = @insert_rows = @insert_hyperlinks = @delete_columns = @delete_rows = @sort = @auto_filter = @pivot_tables = true @password = nil parse_options options end
Public Instance Methods
encodes password for protection locking
# File lib/axlsx/workbook/worksheet/sheet_protection.rb, line 70 def password=(v) return if v == nil @password = create_password_hash(v) end
Serialize the object @param [String] str @return [String]
# File lib/axlsx/workbook/worksheet/sheet_protection.rb, line 78 def to_xml_string(str = '') str << '<sheetProtection ' serialized_attributes str str << '/>' end
Private Instance Methods
Creates a password hash for a given password @return [String]
# File lib/axlsx/workbook/worksheet/sheet_protection.rb, line 87 def create_password_hash(password) encoded_password = encode_password(password) password_as_hex = [encoded_password].pack("v") password_as_string = password_as_hex.unpack("H*").first.upcase password_as_string[2..3] + password_as_string[0..1] end
Encodes a given password Based on the algorithm provided by Daniel Rentz of OpenOffice. www.openoffice.org/sc/excelfileformat.pdf, Revision 1.42, page 115 (21.05.2012) @return [String]
# File lib/axlsx/workbook/worksheet/sheet_protection.rb, line 100 def encode_password(password) i = 0 chars = password.split(//) count = chars.size chars.collect! do |char| i += 1 char = char.unpack('c')[0] << i #ord << i low_15 = char & 0x7fff high_15 = char & 0x7fff << 15 high_15 = high_15 >> 15 char = low_15 | high_15 end encoded_password = 0x0000 chars.each { |c| encoded_password ^= c } encoded_password ^= count encoded_password ^= 0xCE4B end