lib/write_xlsx/worksheet.rb in write_xlsx-0.85.10 vs lib/write_xlsx/worksheet.rb in write_xlsx-0.85.11

- old
+ new

@@ -2527,9 +2527,94 @@ end end end # + # :call-seq: + # update_format_with_params(row, col, format_params) + # + # Update formatting of the cell to the specified row and column (zero indexed). + # + # worksheet.update_format_with_params(0, 0, color: 'red') + # + # This method is used to update formatting of the cell keeping cell contents + # and formatting. + # + # If the cell doesn't have CellData object, this method create a CellData + # using write_blank method. + # If the cell has CellData, this method fetch contents and format of cell from + # the CellData object and recreate CellData using write method. + # + def update_format_with_params(*args) + row, col, params = row_col_notation(args) + raise WriteXLSXInsufficientArgumentError if row.nil? || col.nil? || params.nil? + + # Check that row and col are valid and store max and min values + check_dimensions(row, col) + store_row_col_max_min_values(row, col) + + format = nil + cell_data = nil + if @cell_data_table[row].nil? || @cell_data_table[row][col].nil? + format = @workbook.add_format(params) + write_blank(row, col, format) + else + if @cell_data_table[row][col].xf.nil? + format = @workbook.add_format(params) + cell_data = @cell_data_table[row][col] + else + format = @workbook.add_format + cell_data = @cell_data_table[row][col] + format.copy(cell_data.xf) + format.set_format_properties(params) + end + # keep original value of cell + if cell_data.is_a? FormulaCellData + value = "=#{cell_data.token}" + elsif cell_data.is_a? FormulaArrayCellData + value = "{=#{cell_data.token}}" + elsif cell_data.is_a? StringCellData + value = @workbook.shared_strings.string(cell_data.data[:sst_id]) + else + value = cell_data.data + end + write(row, col, value, format) + end + end + + # + # :call-seq: + # update_range_format_with_params(row_first, col_first, row_last, col_last, format_params) + # + # Update formatting of cells in range to the specified row and column (zero indexed). + # + # worksheet.update_range_format_with_params(0, 0, 3, 3, color: 'red') + # + # This method is used to update formatting of multiple cells keeping cells' contents + # and formatting. + # + # + def update_range_format_with_params(*args) + row_first, col_first, row_last, col_last, params = row_col_notation(args) + + raise WriteXLSXInsufficientArgumentError if [row_first, col_first, row_last, col_last, params].include?(nil) + + # Swap last row/col with first row/col as necessary + row_first, row_last = row_last, row_first if row_first > row_last + col_first, col_last = col_last, col_first if col_first > col_last + + # Check that column number is valid and store the max value + check_dimensions(row_last, col_last) + store_row_col_max_min_values(row_last, col_last) + + (row_first..row_last).each do |row| + (col_first..col_last).each do |col| + update_format_with_params(row, col, params) + end + end + end + + # # The outline_settings() method is used to control the appearance of # outlines in Excel. Outlines are described in # {"OUTLINES AND GROUPING IN EXCEL"}["method-i-set_row-label-OUTLINES+AND+GROUPING+IN+EXCEL"]. # # The +visible+ parameter is used to control whether or not outlines are