【发布时间】:2018-07-10 09:07:32
【问题描述】:
我希望我的 xlsx 电子表格的用户编辑工作表的某些部分,但不是大部分。或者换句话说,我只想保护工作表的某些部分。
我learned如何使用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。
我无法从documentation of the rubyXL 中找到有关如何执行此操作的语法,也找不到如何使用该文档(请原谅我的无知)。当我单击该页面侧面的任何链接时,我不知所措,因为对我来说,似乎唯一友好的就是主页。谷歌没有帮助。在上面的代码中,我不知道他们是如何获得工作表的sheet_protection 属性可供使用的。
在我找到的closest clue 中,我了解到可以通过单元格样式来实现单元格的“取消保护”。因此,我尝试创建一种样式并将其放入单元格中,但由于缺乏指南,该样式也被证明很困难。
在 github repo 的 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>
我什至不确定我是否走在正确的轨道上。请帮忙。
【问题讨论】: