我希望我的 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我发现,我了解到可以通过单元格样式实现单元格的“解除保护”。因此,我尝试创建一种样式并将其放入单元格中,但事实证明,由于缺乏指南,该样式也很困难。
在cell_style.rb在 github 存储库中,我发现了一些关于 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
。
关于ruby - 我们如何使用 rubyXL 保护工作表的某些部分?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51261261/