我们如何使用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的文档中找到如何做到这一点的语法,也无法使用该文档(请原谅我的无知(。当我点击页面一侧的任何链接时,我都不知所措,因为对我来说,似乎唯一友好的是主页。谷歌也于事无补。在上面的代码中,我不知道他们是如何获得工作表的sheet_protection属性的。

在我发现的最接近的线索中,我了解到细胞的"脱保护"可以通过细胞类型来实现。所以我试着创造一种风格,并把它放在一个单元格里,但由于缺乏指南,这个风格也很难。

在github repo的cell_style.rb中,我发现了一些关于ProtectionCellStyle类的内容。

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>

我甚至不确定自己是否走上了正轨。请帮忙。

也许你已经想明白了,但这对我有用…

给定worksheetcell,我做了:

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
)

它保护了除cell之外的整个worksheet

相关内容

最新更新