我希望我的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的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>我甚至不确定我是不是走对了路。请帮帮忙。
发布于 2019-06-10 23:03:48
也许你已经想出来了,但这对我有用.
有了一个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。
https://stackoverflow.com/questions/51261261
复制相似问题