【问题标题】:How can we protect some parts of a sheet using rubyXL?我们如何使用 ruby​​XL 保护工作表的某些部分?
【发布时间】: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 中,我发现了一些关于 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>

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

【问题讨论】:

    标签: ruby excel xlsx rubyxl


    【解决方案1】:

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

    鉴于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
    )
    

    它保护了整个worksheet,除了cell

    【讨论】:

      猜你喜欢
      • 2020-06-17
      • 2012-02-13
      • 2022-06-15
      • 2017-09-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多