Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Escape formulas in XLSX #38

Merged
merged 8 commits into from
Jan 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,7 @@ See this file for more details: [test/unit/multi_sheet_test.rb](./test/unit/mult
|**freeze_headers**<br>*Boolean*||Make all header rows frozen/fixed so they do not scroll.|
|**freeze**<br>*Hash*|`{rows: (1..4), columns: :all}`|Make all specified rows and columns frozen/fixed so they do not scroll.|
|**skip_defaults**<br>*Boolean*|`false`|Removes defaults and default styles. Particularily useful for heavily customized spreadsheets where the default styles get in the way.|
|**escape_formulas**<br>*Boolean* or *Array*|`true`|Pass a single boolean to apply to all cells, or an array of booleans to control column-by-column. Advisable to be set true when involved with untrusted user input. See [an example of the underlying functionality](https://github.com/caxlsx/caxlsx/blob/master/examples/escape_formula_example.md). NOTE: Header row cells are not escaped. |

## `to_axlsx_spreadsheet(options={}, axlsx_package_to_join=nil)`
Same options as `to_xlsx`
Expand Down
2 changes: 1 addition & 1 deletion lib/spreadsheet_architect/class_methods/xlsx.rb
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ def to_axlsx_package(opts={}, package=nil)
end
end

sheet.add_row row_data, style: styles, types: types
sheet.add_row row_data, style: styles, types: types, escape_formulas: options[:escape_formulas]
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we make the headers rows use escape_formulas: options[:escape_formulas] just the same as the data rows


if options[:conditional_row_styles]
options[:conditional_row_styles] = SpreadsheetArchitect::Utils.hash_array_symbolize_keys(options[:conditional_row_styles])
Expand Down
5 changes: 5 additions & 0 deletions lib/spreadsheet_architect/utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,10 @@ def self.get_options(options, klass)
end
end

if options[:escape_formulas].nil?
options[:escape_formulas] = true
end

return options
end

Expand Down Expand Up @@ -290,6 +294,7 @@ def self.hash_array_symbolize_keys(array)
row_style: Hash,
sheet_name: String,
spreadsheet_columns: [Proc, Symbol, String],
escape_formulas: [TrueClass, FalseClass, Array]
}.freeze


Expand Down
11 changes: 7 additions & 4 deletions test/unit/utils_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,14 @@ def teardown
assert_equal klass.get_options({sheet_name: false}, Post)[:sheet_name], 'Posts'

### removes default styles
assert_equal klass.get_options({skip_defaults: true}, SpreadsheetArchitect), {skip_defaults: true, sheet_name: "Sheet1"}
assert_equal klass.get_options({skip_defaults: true}, Post), {skip_defaults: true, sheet_name: "Posts"}
assert_equal klass.get_options({skip_defaults: true}, SpreadsheetArchitect), {skip_defaults: true, sheet_name: "Sheet1", escape_formulas: true}
assert_equal klass.get_options({skip_defaults: true}, Post), {skip_defaults: true, sheet_name: "Posts", escape_formulas: true}

assert_not_equal klass.get_options({skip_defaults: false}, SpreadsheetArchitect), {skip_defaults: true, sheet_name: "Sheet1"}
assert_not_equal klass.get_options({skip_defaults: false}, Post), {skip_defaults: true, sheet_name: "Posts"}
assert_not_equal klass.get_options({skip_defaults: false}, SpreadsheetArchitect), {skip_defaults: true, sheet_name: "Sheet1", escape_formulas: true}
assert_not_equal klass.get_options({skip_defaults: false}, Post), {skip_defaults: true, sheet_name: "Posts", escape_formulas: true}

### sets :escape_formulas if not given
assert_equal klass.get_options({}, SpreadsheetArchitect)[:escape_formulas], true
end

test "convert_styles_to_ods" do
Expand Down