ruby - 我们如何使用 ruby​​XL 保护工作表的某些部分?

标签 ruby excel xlsx rubyxl

我希望我的 xlsx 电子表格的用户可以编辑工作表的某些部分,但不能编辑大部分。或者换句话说,我只想保护工作表的某些部分

learned如何使用带有以下代码的 ruby​​XL 保护工作表:

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 存储库中,我发现了一些关于 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>

我什至不确定我是否在正确的轨道上。请帮忙。

最佳答案

也许你已经想通了,但这对我有用......

给定一个 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 - 我们如何使用 ruby​​XL 保护工作表的某些部分?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51261261/

相关文章:

excel - 如何遍历高级筛选器中的条件?

excel - 如何在itextSharp(dll)中保留原始旋转页面

vba - 从句子中提取字母数字

python - 从 Python Pyramid 项目导出到 Excel .xlsx

ruby-on-rails - 使用 link_to 创建分层模型的链接

ruby - 如何使用 Ruby gem Mechanize 获取某些属性(例如 colspan)的 HTML 标签中的内容?

ruby-on-rails - 给数组设置一定的大小和内容

python - 在保持时间戳的同时将 XLSX 转换为 CSV

r - 使用 RestRserve 上传 XLSX 数据

ruby - Sinatra 条件