[英]How can we protect some parts of a sheet using rubyXL?
我希望xlsx電子表格的用戶可以編輯工作表的某些部分,但不能編輯大部分。 換句話說, 我只希望保護工作表的某些部分 。
我學習了如何使用rubyXL通過以下代碼保護工作表:
sheetProtection = RubyXL::WorksheetProtection.new(
password: hashedPass,
sheet: true,
objects: true,
scenarios: true,
format_cells: true,
format_columns: true,
insert_columns: true,
delete_columns: true,
insert_rows: true,
delete_rows: true
);
wsData = workbook['data'];
wsData.sheet_protection = sheetProtection;
說,我希望用戶僅編輯所述工作表的單元格區域C2:C13
。
我無法從rubyXL的文檔中找到語法,也無法找到如何使用該文檔的語法(請原諒我的無知)。 當我單擊該頁面側面的任何鏈接時,我很茫然,因為對我來說,似乎唯一友好的鏈接是主頁。 Google沒有幫助。 在上面的代碼中,我不知道它們如何獲得可使用的工作表的sheet_protection
屬性。
在我發現的最接近線索中 ,我了解到,通過單元格樣式可以實現單元格的“非保護”。 因此,我嘗試創建一種樣式並將其放在單元格中,但是由於缺乏指南,事實證明該樣式也很困難。
在github存儲庫的cell_style.rb中,我找到了有關Protection
和CellStyle
類的內容。
unprotected = RubyXL::Protection.new(
locked: false,
hidden: false
);
unprotecStyle = RubyXL::CellStyle.new(
name: 'unprotected style'
);
我在文檔中找不到如何將它們放在一起,甚至無法在單元格上應用樣式:
wsData[1][2].cell_style = unprotecStyle;
# undefined method `cell_style=' for #<RubyXL::Cell(1,2): "cell-content", datatype="str", style_index=8>
我什至不確定自己是否走對了。 請幫忙。
也許您已經知道了,但這對我有用。
給定一個worksheet
和一個cell
,我做了:
worksheet.
workbook.
cell_xfs[cell.style_index || 0].
protection = RubyXL::Protection.new(
locked: false,
hidden: false
)
然后我做了:
worksheet.sheet_protection = RubyXL::WorksheetProtection.new(
sheet: true,
objects: true,
scenarios: true,
format_cells: true,
format_columns: true,
insert_columns: true,
delete_columns: true,
insert_rows: true,
delete_rows: true
)
它保護了整個worksheet
( cell
除外)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.