[英]How to use rubyXL to lock existing cells but leave all empty cells unlocked?
How do I use rubyXL to lock the existing cells in a worksheet but leave all empty cells unlocked?如何使用 rubyXL 锁定工作表中的现有单元格,但不锁定所有空单元格? (I don't want to make the user jump through any hoops like requiring him or her to create a new column before entering new data.) (我不想让用户跳过任何障碍,比如要求他或她在输入新数据之前创建一个新列。)
I figured out how to lock existing cells using the technique presented in this StackOverflow post: How can we protect some parts of a sheet using rubyXL?我想出了如何使用 StackOverflow 帖子中介绍的技术锁定现有单元格: 我们如何使用 rubyXL 保护工作表的某些部分? (The basic idea is to (1) Create a new xf, (2) register the new xf with the workbook, (3) set the style_index
of all existing cells to use this xf, then (4) set the WorksheetProtection
for the sheet.) (基本思路是(1)创建一个新的 xf,(2)将新的 xf 注册到工作簿,(3)设置所有现有单元格的style_index
以使用此 xf,然后(4)为工作表设置WorksheetProtection
.)
However, when I do this and open the workbook in Excel, all the empty cells in the worksheet are also locked.但是,当我这样做并在 Excel 中打开工作簿时,工作表中的所有空单元格也被锁定。
In principle, I need to add an "unlocked" xf to all unused cells;原则上,我需要为所有未使用的单元格添加一个“未锁定”的 xf; however, since these cells don't exist yet, there is no Cell object to which to set the "unlocked" xf.然而,由于这些单元格尚不存在,因此没有单元格对象可以设置“未锁定”的 xf。
There is a way to do this in Excel:在 Excel 中有一种方法可以做到这一点:
When I follows these steps then examine the worksheet using rubyXL, I see that there are three XL objects:当我执行这些步骤然后使用 rubyXL 检查工作表时,我看到有三个 XL 对象:
The existing cells all use style/XL 2. So, I'm thinking that there must be a way to specify that all unused cells in the worksheet default to style/XL 3;现有的单元格都使用 style/XL 2。因此,我认为必须有一种方法可以指定工作表中所有未使用的单元格默认为 style/XL 3; but, I don't see how to specify this using rubyXL.但是,我不知道如何使用 rubyXL 来指定它。
Any ideas?有任何想法吗?
I figured it out:我想到了:
require "rubyXL"
require "rubyXL/convenience_methods"
workbook = RubyXL::Workbook.new
sheet = workbook.worksheets.first
# Crate an xf that locks the cell
locked_xf = workbook.cell_xfs.first.dup
locked_xf.protection = RubyXL::Protection.new(
locked: true,
hidden: false,
)
locked_id = workbook.register_new_xf(locked_xf)
# Crate an xf that does not lock the cell
unlocked_xf = workbook.cell_xfs.first.dup
unlocked_xf.protection = RubyXL::Protection.new(
locked: false,
hidden: false,
)
unlocked_id = workbook.register_new_xf(unlocked_xf)
# Create new cells. Lock each one.
(0..5).each do |row|
(0..5).each do |col|
cell = sheet.add_cell(row, col, (row * col).to_s)
cell.style_index = locked_id
end
end
# Create a cell range to cover "all" rows. (Upper bound set at 16384)
range = RubyXL::ColumnRange.new
range.min = 1
range.max = 16384
range.width = 10.83203125 # be sure to set this, otherwise columns aren't visible.
range.style_index = unlocked_id
sheet.cols << range
# Lock the sheet
sheet.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
)
workbook.write("lock_test.xlsx")
(I also put a copy of this code here: https://github.com/kurmasz/rubyXL-recipes ) (我还在此处放置了此代码的副本: https ://github.com/kurmasz/rubyXL-recipes)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.