### Set up Sheets with Data and Formatting Source: https://github.com/cxn03651/write_xlsx/blob/main/docs/examples.md Initializes workbook sheets, sets column widths, row formatting, and writes headings. This is a common setup for subsequent filtering examples. ```ruby headings = DATA.gets.split data = DATA.map(&:split) workbook.worksheets.each do |worksheet| worksheet.set_column('A:D', 12) worksheet.set_row(0, 20, bold) worksheet.write('A1', headings) end ``` -------------------------------- ### Extending the write() Method - Step 1 Source: https://github.com/cxn03651/write_xlsx/blob/main/docs/examples.md Example of extending the write() method. This is the first step, showing basic setup. ```ruby require 'write_xlsx' # Define a custom writer class that inherits from WriteXLSX. class MyWriter < WriteXLSX def write_row(row, col, data, format=nil) # Call the parent method to do the actual writing. super(row, col, data, format) end end workbook = MyWriter.new('write_handler1.xlsx') worksheet = workbook.add_worksheet worksheet.write_row('A1', ['Header 1', 'Header 2']) workbook.close ``` -------------------------------- ### Complete Line Chart Example Source: https://github.com/cxn03651/write_xlsx/blob/main/docs/line.md A comprehensive example demonstrating various features for creating a Line chart, including multiple series, titles, axes, and styles. Ensure the 'write_xlsx' gem is installed. ```ruby require 'write_xlsx' workbook = WriteXLSX.new('chart_line.xlsx') worksheet = workbook.add_worksheet bold = workbook.add_format(bold: 1) # Add the worksheet data that the charts will refer to. headings = [ 'Number', 'Batch 1', 'Batch 2' ] data = [ [ 2, 3, 4, 5, 6, 7 ], [ 10, 40, 50, 20, 10, 50 ], [ 30, 60, 70, 50, 40, 30 ] ] worksheet.write('A1', headings, bold) worksheet.write('A2', data) # Create a new chart object. In this case an embedded chart. chart = workbook.add_chart(type: 'line', embedded: 1) # Configure the first series. chart.add_series( name: '=Sheet1!$B$1', categories: '=Sheet1!$A$2:$A$7', values: '=Sheet1!$B$2:$B$7' ) # Configure second series. Note alternative use of array ref to define # ranges: [ sheetname, row_start, row_end, col_start, col_end ]. chart.add_series( name: '=Sheet1!$C$1', categories: [ 'Sheet1', 1, 6, 0, 0 ], values: [ 'Sheet1', 1, 6, 2, 2 ] ) # Add a chart title and some axis labels. chart.set_title(name: 'Results of sample analysis') chart.set_x_axis(name: 'Test Number') chart.set_y_axis(name: 'Sample length(mm)') # Set an Excel chart style. Colors with white outline and shadow. chart.set_style(10) # Insert the chart into the worksheet (with an offset). worksheet.insert_chart('D2', chart, 25, 10) workbook.close ``` -------------------------------- ### Complete Bar Chart Example Source: https://github.com/cxn03651/write_xlsx/blob/main/docs/bar.md A comprehensive example demonstrating various features for creating a bar chart, including multiple series, custom formatting, and chart styling. This requires data setup and chart configuration. ```ruby require 'write_xlsx' workbook = WriteXLSX.new('chart_bar.xlsx') worksheet = workbook.add_worksheet bold = workbook.add_format(bold: 1) # Add the worksheet data that the charts will refer to. headings = [ 'Number', 'Batch 1', 'Batch 2' ] data = [ [ 2, 3, 4, 5, 6, 7 ], [ 10, 40, 50, 20, 10, 50 ], [ 30, 60, 70, 50, 40, 30 ] ] worksheet.write('A1', headings, bold) worksheet.write('A2', data) # Create a new chart object. In this case an embedded chart. chart = workbook.add_chart(type: 'bar', embedded: 1) # Configure the first series. chart.add_series( name: '=Sheet1!$B$1', categories: '=Sheet1!$A$2:$A$7', values: '=Sheet1!$B$2:$B$7' ) # Configure second series. Note alternative use of array ref to define # ranges: [ sheetname, row_start, row_end, col_start, col_end ]. chart.add_series( name: '=Sheet1!$C$1', categories: [ 'Sheet1', 1, 6, 0, 0 ], values: [ 'Sheet1', 1, 6, 2, 2 ] ) # Add a chart title and some axis labels. chart.set_title(name: 'Results of sample analysis') chart.set_x_axis(name: 'Test Number') chart.set_y_axis(name: 'Sample length(mm)') # Set an Excel chart style. Blue colors with white outline and shadow. chart.set_style(11) # Insert the chart into the worksheet (with an offset). worksheet.insert_chart('D2', chart, 25, 10) workbook.close ``` -------------------------------- ### Complete Pie Chart Example Source: https://github.com/cxn03651/write_xlsx/blob/main/docs/pie.md A comprehensive example demonstrating the creation of a Pie chart with data, title, style, and insertion into a worksheet. ```APIDOC ## Complete Pie Chart Example ### Description This example illustrates the full process of creating a Pie chart, including adding data, setting a title, applying a style, and embedding it in a worksheet. ### Method N/A (This is a complete script example) ### Endpoint N/A ### Parameters N/A ### Request Example ```ruby require 'write_xlsx' workbook = WriteXLSX.new('chart_pie.xlsx') worksheet = workbook.add_worksheet bold = workbook.add_format(bold: 1) # Add the worksheet data that the charts will refer to. headings = [ 'Category', 'Values' ] data = [ [ 'Glazed', 'Chocolate', 'Cream' ], [ 50, 35, 15 ] ] worksheet.write('A1', headings, bold) worksheet.write('A2', data) # Create a new chart object. In this case an embedded chart. chart = workbook.add_chart(type: 'pie', embedded: 1) # Configure the series. Note the use of the array ref to define ranges: # [ sheetname, row_start, row_end, col_start, col_end ]. chart.add_series( name: 'Pie sales data', categories: [ 'Sheet1', 1, 3, 0, 0 ], values: [ 'Sheet1', 1, 3, 1, 1 ] ) # Add a title. chart.set_title(name: 'Popular Pie Types') # Set an Excel chart style. Colors with white outline and shadow. chart.set_style(10) # Insert the chart into the worksheet (with an offset). worksheet.insert_chart('C2', chart, 25, 10) workbook.close ``` ### Response #### Success Response (200) N/A (The script generates an Excel file named 'chart_pie.xlsx') #### Response Example N/A ``` -------------------------------- ### Simple Sparklines Demo in Excel Source: https://github.com/cxn03651/write_xlsx/blob/main/docs/examples.md A basic example demonstrating the creation of sparklines. ```ruby require 'write_xlsx' workbook = WriteXLSX.new('sparklines1.xlsx') worksheet = workbook.add_worksheet # Add some data for sparklines. worksheet.write('A1', 1) worksheet.write('A2', 2) worksheet.write('A3', 3) worksheet.write('A4', 4) worksheet.write('A5', 5) # Add a line sparkline. worksheet.add_sparkline('C1', 'A1:A5') workbook.close ``` -------------------------------- ### Complete Column Chart Example Source: https://github.com/cxn03651/write_xlsx/blob/main/docs/column.md A comprehensive example demonstrating various features for creating and configuring a column chart, including multiple series, titles, axis labels, and styles. Requires the 'write_xlsx' gem. ```ruby require 'write_xlsx' workbook = WriteXLSX.new('chart_column.xlsx') worksheet = workbook.add_worksheet bold = workbook.add_format(bold: 1) # Add the worksheet data that the charts will refer to. headings = [ 'Number', 'Batch 1', 'Batch 2' ] data = [ [ 2, 3, 4, 5, 6, 7 ], [ 10, 40, 50, 20, 10, 50 ], [ 30, 60, 70, 50, 40, 30 ] ] worksheet.write('A1', headings, bold) worksheet.write('A2', data) # Create a new chart object. In this case an embedded chart. chart = workbook.add_chart(type: 'column', embedded: 1) # Configure the first series. chart.add_series( name: '=Sheet1!$B$1', categories: '=Sheet1!$A$2:$A$7', values: '=Sheet1!$B$2:$B$7' ) # Configure second series. Note alternative use of array ref to define # ranges: [ sheetname, row_start, row_end, col_start, col_end ]. chart.add_series( name: '=Sheet1!$C$1', categories: [ 'Sheet1', 1, 6, 0, 0 ], values: [ 'Sheet1', 1, 6, 2, 2 ] ) # Add a chart title and some axis labels. chart.set_title(name: 'Results of sample analysis') chart.set_x_axis(name: 'Test Number') chart.set_y_axis(name: 'Sample length(mm)') # Set an Excel chart style. Blue colors with white outline and shadow. chart.set_style(11) # Insert the chart into the worksheet (with an offset). worksheet.insert_chart('D2', chart, 25, 10) workbook.close ``` -------------------------------- ### Apply Page Setup to All Worksheets Source: https://github.com/cxn03651/write_xlsx/blob/main/docs/page_set_up.md Iterate through all worksheets in a workbook to apply common page setup settings like landscape orientation. ```ruby workbook.sheets.each do |worksheet| worksheet.set_landscape end ``` -------------------------------- ### Create a Complete Pie Chart Example Source: https://github.com/cxn03651/write_xlsx/blob/main/docs/pie.md This comprehensive example demonstrates creating an embedded Pie chart with data, titles, styles, and inserting it into a worksheet. It uses array references for defining chart ranges. ```ruby require 'write_xlsx' workbook = WriteXLSX.new('chart_pie.xlsx') worksheet = workbook.add_worksheet bold = workbook.add_format(bold: 1) # Add the worksheet data that the charts will refer to. headings = [ 'Category', 'Values' ] data = [ [ 'Glazed', 'Chocolate', 'Cream' ], [ 50, 35, 15 ] ] worksheet.write('A1', headings, bold) worksheet.write('A2', data) # Create a new chart object. In this case an embedded chart. chart = workbook.add_chart(type: 'pie', embedded: 1) # Configure the series. Note the use of the array ref to define ranges: # [ sheetname, row_start, row_end, col_start, col_end ]. chart.add_series( name: 'Pie sales data', categories: [ 'Sheet1', 1, 3, 0, 0 ], values: [ 'Sheet1', 1, 3, 1, 1 ] ) # Add a title. chart.set_title(name: 'Popular Pie Types') # Set an Excel chart style. Colors with white outline and shadow. chart.set_style(10) # Insert the chart into the worksheet (with an offset). worksheet.insert_chart('C2', chart, 25, 10) workbook.close ``` -------------------------------- ### Outlines and Grouping in Excel Source: https://github.com/cxn03651/write_xlsx/blob/main/docs/examples.md An example demonstrating how to create outlines and group rows/columns. ```ruby require 'write_xlsx' workbook = WriteXLSX.new('outline.xlsx') worksheet = workbook.add_worksheet # Add some data. worksheet.write('A1', 'Region') worksheet.write('B1', 'Sales') worksheet.write('A2', 'East') worksheet.write('B2', 1000) worksheet.write('A3', 'West') worksheet.write('B3', 1500) # Group rows 2 and 3 (East and West). worksheet.set_row(1, nil, nil, 0, 1) # Row 2 worksheet.set_row(2, nil, nil, 0, 1) # Row 3 worksheet.group_rows(1, 2) # Group rows 2 and 3 worksheet.write('A4', 'North') worksheet.write('B4', 1200) worksheet.write('A5', 'South') worksheet.write('B5', 1800) # Group rows 4 and 5. worksheet.set_row(3, nil, nil, 0, 2) # Row 4 worksheet.set_row(4, nil, nil, 0, 2) # Row 5 worksheet.group_rows(3, 4) # Group rows 4 and 5 # Create an outline for the groups. worksheet.outline_row_level(1) worksheet.outline_row_level(2) workbook.close ``` -------------------------------- ### Complete Radar chart example with multiple series Source: https://github.com/cxn03651/write_xlsx/blob/main/docs/radar.md Demonstrates creating a Radar chart with multiple series, custom titles, axis labels, and styles. Includes data setup and chart insertion into the worksheet. ```ruby require 'write_xlsx' workbook = WriteXLSX.new('chart_radar.xlsx') worksheet = workbook.add_worksheet bold = workbook.add_format(bold: 1) # Add the worksheet data that the charts will refer to. headings = [ 'Number', 'Batch 1', 'Batch 2' ] data = [ [ 2, 3, 4, 5, 6, 7 ], [ 30, 60, 70, 50, 40, 30 ], [ 25, 40, 50, 30, 50, 40 ] ] worksheet.write('A1', headings, bold) worksheet.write('A2', data) # Create a new chart object. In this case an embedded chart. chart = workbook.add_chart(type: 'radar', embedded: 1) # Configure the first series. chart.add_series( name: '=Sheet1!$B$1', categories: '=Sheet1!$A$2:$A$7', values: '=Sheet1!$B$2:$B$7' ) # Configure second series. Note alternative use of array ref to define # ranges: [ sheetname, row_start, row_end, col_start, col_end ]. chart.add_series( name: '=Sheet1!$C$1', categories: [ 'Sheet1', 1, 6, 0, 0 ], values: [ 'Sheet1', 1, 6, 2, 2 ] ) # Add a chart title and some axis labels. chart.set_title(name: 'Results of sample analysis') chart.set_x_axis(name: 'Test Number') chart.set_y_axis(name: 'Sample length(mm)') # Set an Excel chart style. Colors with white outline and shadow. chart.set_style(10) # Insert the chart into the worksheet (with an offset). worksheet.insert_chart('D2', chart, 25, 10) workbook.close ``` -------------------------------- ### Set Diagonal Border Example Source: https://github.com/cxn03651/write_xlsx/blob/main/docs/format.md An example demonstrating how to set both the type, style, and color for a diagonal cell border. ```python format.set_diag_type(3) format.set_diag_border(7) format.set_diag_color('red') ``` -------------------------------- ### Collapsed Outlines in Excel Source: https://github.com/cxn03651/write_xlsx/blob/main/docs/examples.md An example showing how to create outlines that are initially collapsed. ```ruby require 'write_xlsx' workbook = WriteXLSX.new('outline_collapsed.xlsx') worksheet = workbook.add_worksheet # Add some data. worksheet.write('A1', 'Region') worksheet.write('B1', 'Sales') worksheet.write('A2', 'East') worksheet.write('B2', 1000) worksheet.write('A3', 'West') worksheet.write('B3', 1500) # Group rows 2 and 3. worksheet.set_row(1, nil, nil, 0, 1) worksheet.set_row(2, nil, nil, 0, 1) worksheet.group_rows(1, 2) worksheet.write('A4', 'North') worksheet.write('B4', 1200) worksheet.write('A5', 'South') worksheet.write('B5', 1800) # Group rows 4 and 5. worksheet.set_row(3, nil, nil, 0, 2) worksheet.set_row(4, nil, nil, 0, 2) worksheet.group_rows(3, 4) # Create an outline and set the level to collapsed. worksheet.outline_row_level(1) worksheet.outline_row_level(2, 1) # Set level 2 to collapsed workbook.close ``` -------------------------------- ### Simple Sales Spreadsheet in Excel Source: https://github.com/cxn03651/write_xlsx/blob/main/docs/examples.md An example of creating a basic sales spreadsheet with some calculations. ```ruby require 'write_xlsx' workbook = WriteXLSX.new('sales.xlsx') worksheet = workbook.add_worksheet # Add headers. worksheet.write('A1', 'Product') worksheet.write('B1', 'Quantity') worksheet.write('C1', 'Price') worksheet.write('D1', 'Total') # Add data. worksheet.write('A2', 'Apple') worksheet.write('B2', 10) worksheet.write('C2', 0.5) worksheet.write('A3', 'Banana') worksheet.write('B3', 15) worksheet.write('C3', 0.3) # Add formulas. worksheet.write('D2', '=B2*C2') worksheet.write('D3', '=B3*C3') # Add a total row. worksheet.write('C5', 'Total') worksheet.write('D5', '=SUM(D2:D3)') workbook.close ``` -------------------------------- ### Cell Indentation in Excel Source: https://github.com/cxn03651/write_xlsx/blob/main/docs/examples.md An example demonstrating how to apply indentation to cells. ```ruby require 'write_xlsx' workbook = WriteXLSX.new('indent.xlsx') worksheet = workbook.add_worksheet # Define a format with indentation. indent_format = workbook.add_format(:indent => 1) # Write data with indentation. worksheet.write('A1', 'Indented text', indent_format) workbook.close ``` -------------------------------- ### Comprehensive Scatter Chart Example Source: https://github.com/cxn03651/write_xlsx/blob/main/docs/scatter.md A complete example demonstrating advanced scatter chart features including multiple series, custom data ranges, chart titles, axis labels, and styling. Ensure the 'write_xlsx' library is required. ```ruby require 'write_xlsx' workbook = WriteXLSX.new('chart_scatter.xlsx') worksheet = workbook.add_worksheet bold = workbook.add_format(bold: 1) # Add the worksheet data that the charts will refer to. headings = [ 'Number', 'Batch 1', 'Batch 2' ] data = [ [ 2, 3, 4, 5, 6, 7 ], [ 10, 40, 50, 20, 10, 50 ], [ 30, 60, 70, 50, 40, 30 ] ] worksheet.write('A1', headings, bold) worksheet.write('A2', data) # Create a new chart object. In this case an embedded chart. chart = workbook.add_chart(type: 'scatter', embedded: 1) # Configure the first series. chart.add_series( name: '=Sheet1!$B$1', categories: '=Sheet1!$A$2:$A$7', values: '=Sheet1!$B$2:$B$7' ) # Configure second series. Note alternative use of array ref to define # ranges: [ sheetname, row_start, row_end, col_start, col_end ]. chart.add_series( name: '=Sheet1!$C$1', categories: [ 'Sheet1', 1, 6, 0, 0 ], values: [ 'Sheet1', 1, 6, 2, 2 ] ) # Add a chart title and some axis labels. chart.set_title(name: 'Results of sample analysis') chart.set_x_axis(name: 'Test Number') chart.set_y_axis(name: 'Sample length(mm)') # Set an Excel chart style. Colors with white outline and shadow. chart.set_style(10) # Insert the chart into the worksheet (with an offset). worksheet.insert_chart('D2', chart, 25, 10) workbook.close ``` -------------------------------- ### Create Panes in Excel Source: https://github.com/cxn03651/write_xlsx/blob/main/docs/examples.md An example of how to create split panes in an Excel worksheet. ```ruby require 'write_xlsx' workbook = WriteXLSX.new('panes.xlsx') worksheet = workbook.add_worksheet # Add some data. (0..10).each do |row_num| (0..5).each do |col_num| worksheet.write(row_num, col_num, "R#{row_num}C#{col_num}") end end # Create panes. The arguments are: # row, col, top_row, left_col worksheet.split_panes(2, 1) # Optionally, freeze the panes. # worksheet.freeze_panes(2, 1) workbook.close ``` -------------------------------- ### Complete Area Chart Example Source: https://github.com/cxn03651/write_xlsx/blob/main/docs/area.md A comprehensive example showing how to create an embedded Area chart with multiple series, custom data ranges, titles, axis labels, and chart styling. It also demonstrates writing bold headings to the worksheet. ```ruby require 'write_xlsx' workbook = WriteXLSX.new('chart_area.xlsx') worksheet = workbook.add_worksheet bold = workbook.add_format(bold: 1) # Add the worksheet data that the charts will refer to. headings = [ 'Number', 'Batch 1', 'Batch 2' ] data = [ [ 2, 3, 4, 5, 6, 7 ], [ 40, 40, 50, 30, 25, 50 ], [ 30, 25, 30, 10, 5, 10 ] ] worksheet.write('A1', headings, bold) worksheet.write('A2', data) # Create a new chart object. In this case an embedded chart. chart = workbook.add_chart(type: 'area', embedded: 1) # Configure the first series. chart.add_series( name: '=Sheet1!$B$1', categories: '=Sheet1!$A$2:$A$7', values: '=Sheet1!$B$2:$B$7' ) # Configure second series. Note alternative use of array ref to define # ranges: [ sheetname, row_start, row_end, col_start, col_end ]. chart.add_series( name: '=Sheet1!$C$1', categories: [ 'Sheet1', 1, 6, 0, 0 ], values: [ 'Sheet1', 1, 6, 2, 2 ] ) # Add a chart title and some axis labels. chart.set_title(name: 'Results of sample analysis') chart.set_x_axis(name: 'Test Number') chart.set_y_axis(name: 'Sample length(mm)') # Set an Excel chart style. Blue colors with white outline and shadow. chart.set_style(11) # Insert the chart into the worksheet (with an offset). worksheet.insert_chart('D2', chart, 25, 10) workbook.close ``` -------------------------------- ### Simple mod_perl 2 Program Source: https://github.com/cxn03651/write_xlsx/blob/main/docs/examples.md A basic example of a mod_perl 2 program. ```perl use strict; use warnings; package MyHandler; sub handler { my $conn = shift; $conn->protocol("HTTP/1.0"); $conn->status_line("200 OK"); $conn->content_type('text/plain'); $conn->print("Hello from mod_perl 2!"); return 1; } 1; __END__ ``` -------------------------------- ### Extending the write() Method - Step 2 Source: https://github.com/cxn03651/write_xlsx/blob/main/docs/examples.md Example of extending the write() method. This is the second step, adding custom logic. ```ruby require 'write_xlsx' # Define a custom writer class that inherits from WriteXLSX. class MyWriter < WriteXLSX def write_row(row, col, data, format=nil) # Add custom logic: prepend 'Custom: ' to each string element. processed_data = data.map do |item| item.is_a?(String) ? "Custom: #{item}" : item end super(row, col, processed_data, format) end end workbook = MyWriter.new('write_handler2.xlsx') worksheet = workbook.add_worksheet worksheet.write_row('A1', ['Header 1', 'Header 2']) worksheet.write_row('A2', ['Data 1', 'Data 2']) workbook.close ``` -------------------------------- ### Create a Doughnut Chart with Custom Colors Source: https://github.com/cxn03651/write_xlsx/blob/main/docs/examples.md This example demonstrates creating a Doughnut chart and setting custom colors for each segment using the 'points' option in add_series. ```ruby chart2 = workbook.add_chart(type: 'doughnut', embedded: 1) # Configure the series and add user defined segment colours. chart2.add_series( name: 'Doughnut sales data', categories: '=Sheet1!$A$2:$A$4', values: '=Sheet1!$B$2:$B$4', points: [ { fill: { color: '#FA58D0' } }, { fill: { color: '#61210B' } }, { fill: { color: '#F5F6CE' } } ] ) # Add a title. chart2.set_title(name: 'Doughnut Chart with user defined colors') # Insert the chart into the worksheet (with an offset). worksheet.insert_chart( 'C18', chart2, x_offset: 25, y_offset: 10 ) ``` -------------------------------- ### Simple Cell Merging in Excel Source: https://github.com/cxn03651/write_xlsx/blob/main/docs/examples.md A basic example demonstrating how to merge cells. ```ruby require 'write_xlsx' workbook = WriteXLSX.new('merge1.xlsx') worksheet = workbook.add_worksheet # Merge cells. The arguments are: # row_start, col_start, row_end, col_end worksheet.merge_range('B2:D2', 'Merged Cells') workbook.close ``` -------------------------------- ### Simple mod_perl 1 Program Source: https://github.com/cxn03651/write_xlsx/blob/main/docs/examples.md A basic example of a mod_perl 1 program. ```perl use strict; use warnings; package MyHandler; sub handler { my $r = shift; $r->content_type('text/plain'); $r->print("Hello from mod_perl 1!"); return Apache2::Const::OK; } 1; __END__ ``` -------------------------------- ### Create Doughnut Chart with WriteXLSX Source: https://github.com/cxn03651/write_xlsx/blob/main/docs/doughnut.md This example demonstrates creating a doughnut chart. It requires the 'write_xlsx' gem. Ensure data ranges are correctly specified for chart series. ```ruby require 'write_xlsx' workbook = WriteXLSX.new('chart_doughnut.xlsx') worksheet = workbook.add_worksheet bold = workbook.add_format(bold: 1) # Add the worksheet data that the charts will refer to. headings = [ 'Category', 'Values' ] data = [ [ 'Glazed', 'Chocolate', 'Cream' ], [ 50, 35, 15 ] ] worksheet.write('A1', headings, bold) worksheet.write('A2', data) # Create a new chart object. In this case an embedded chart. chart = workbook.add_chart(type: 'doughnut', embedded: 1) # Configure the series. Note the use of the array ref to define ranges: # [ sheetname, row_start, row_end, col_start, col_end ]. chart.add_series( name: 'Doughnut sales data', categories: [ 'Sheet1', 1, 3, 0, 0 ], values: [ 'Sheet1', 1, 3, 1, 1 ] ) # Add a title. chart.set_title(name: 'Popular Doughnut Types') # Set an Excel chart style. Colors with white outline and shadow. chart.set_style(10) # Insert the chart into the worksheet (with an offset). worksheet.insert_chart('C2', chart, 25, 10) workbook.close ``` -------------------------------- ### Strings with Multiple Formats in Excel Source: https://github.com/cxn03651/write_xlsx/blob/main/docs/examples.md Example of creating strings with different formatting applied to parts of the string. ```ruby require 'write_xlsx' workbook = WriteXLSX.new('rich_strings.xlsx') worksheet = workbook.add_worksheet # Define some formats. bold = workbook.add_format(:bold => 1) italic = workbook.add_format(:italic => 1) color = workbook.add_format(:font_color => 'red') # Write a rich string. worksheet.write_rich_string('A1', 'This is ', bold, 'bold', ' and this is ', italic, 'italic', ' and ', color, 'red') workbook.close ``` -------------------------------- ### Add Macros from Existing File in Excel Source: https://github.com/cxn03651/write_xlsx/blob/main/docs/examples.md An example of how to embed macros from an existing `.xlsm` file into a new workbook. ```ruby require 'write_xlsx' workbook = WriteXLSX.new('macros.xlsm') # Add macros from an existing file. workbook.add_macros('source.xlsm') workbook.close ``` -------------------------------- ### Configure Doughnut Chart Hole Size Source: https://github.com/cxn03651/write_xlsx/blob/main/docs/examples.md This example shows how to create a Doughnut chart and then customize its appearance by setting the hole size using set_hole_size. ```ruby # Create an example Doughnut chart like above. chart4 = workbook.add_chart(type: 'doughnut', embedded: 1) # Configure the series. chart4.add_series( name: 'Doughnut sales data', categories: '=Sheet1!$A$2:$A$4', values: '=Sheet1!$B$2:$B$4' ) # Add a title. chart4.set_title(name: 'Doughnut Chart with user defined hole size') # Change the hole size. chart4.set_hole_size(33) # Insert the chart into the worksheet (with an offset). worksheet.insert_chart( 'C50', chart4, x_offset: 25, y_offset: 10 ) ``` -------------------------------- ### Create a Simple Bar Chart Source: https://github.com/cxn03651/write_xlsx/blob/main/docs/bar.md Use this snippet to generate a basic Excel file with a bar chart. Ensure the WriteXLSX gem is installed. ```ruby require 'write_xlsx' workbook = WriteXLSX.new('chart.xlsx') worksheet = workbook.add_worksheet chart = workbook.add_chart(type: 'bar') # Configure the chart. chart.add_series( categories: '=Sheet1!$A$2:$A$7', values: '=Sheet1!$B$2:$B$7' ) # Add the worksheet data the chart refers to. data = [ [ 'Category', 2, 3, 4, 5, 6, 7 ], [ 'Value', 1, 4, 5, 2, 1, 5 ] ] worksheet.write('A1', data) workbook.close ``` -------------------------------- ### Demonstrate WriteXLSX Features Source: https://github.com/cxn03651/write_xlsx/blob/main/docs/examples.md Showcases various features of the WriteXLSX gem, including formatting, text, numbers, and formulas across multiple worksheets. Requires the gem to be installed. ```ruby #!/usr/bin/env ruby # -*- coding: utf-8 -*- require 'write_xlsx' workbook = WriteXLSX.new('demo.xlsx') worksheet = workbook.add_worksheet('Demo') worksheet2 = workbook.add_worksheet('Another sheet') worksheet3 = workbook.add_worksheet('And another') bold = workbook.add_format(bold: 1) ####################################################################### # # Write a general heading # worksheet.set_column('A:A', 36, bold) worksheet.set_column('B:B', 20) worksheet.set_row(0, 40) heading = workbook.add_format( bold: 1, color: 'blue', size: 16, merge: 1, align: 'vcenter' ) headings = ['Features of WriteXLSX', ''] worksheet.write_row('A1', headings, heading) ####################################################################### # # Some text examples # text_format = workbook.add_format( bold: 1, italic: 1, color: 'red', size: 18, font: 'Lucida Calligraphy' ) worksheet.write('A2', "Text") worksheet.write('B2', "Hello Excel") worksheet.write('A3', "Formatted text") worksheet.write('B3', "Hello Excel", text_format) worksheet.write('A4', "Unicode text") worksheet.write('B4', "А Б В Г Д") ####################################################################### # # Some numeric examples # num1_format = workbook.add_format(num_format: '$#,##0.00') num2_format = workbook.add_format(num_format: ' d mmmm yyy') worksheet.write('A5', "Numbers") worksheet.write('B5', 1234.56) worksheet.write('A6', "Formatted numbers") worksheet.write('B6', 1234.56, num1_format) worksheet.write('A7', "Formatted numbers") worksheet.write('B7', 37257, num2_format) ####################################################################### # # Formulae ``` -------------------------------- ### Write Basic Text and Numbers to XLSX Source: https://github.com/cxn03651/write_xlsx/blob/main/docs/examples.md Demonstrates writing simple text, numbers, and formulas to an Excel file using WriteXLSX. Ensure the gem is installed. ```ruby #!/usr/bin/env ruby # -*- coding: utf-8 -*- ####################################################################### # # A simple example of how to use the Excel::Writer::XLSX module to # write text and numbers to an Excel xlsx file. # # reverse(c), March 2001, John McNamara, jmcnamara@cpan.org # convert to ruby by Hideo NAKAMURA, nakamura.hideo@gmail.com # require 'write_xlsx' # Create a new workbook called simple.xls and add a worksheet workbook = WriteXLSX.new('a_simple.xlsx') worksheet = workbook.add_worksheet # The general syntax is write(row, column, token). Note that row and # column are zero indexed # # Write some text worksheet.write(0, 0, "Hi Excel!") # Write some numbers worksheet.write(2, 0, 3) # Writes 3 worksheet.write(3, 0, 3.00000) # Writes 3 worksheet.write(4, 0, 3.00001) # Writes 3.00001 worksheet.write(5, 0, 3.14159) # TeX revision no.? # Write some formulas worksheet.write(7, 0, '=A3 + A6') worksheet.write(8, 0, '=IF(A5>3,"Yes", "No")') # Write a hyperlink worksheet.write(10, 0, 'http://www.ruby-lang.org/') workbook.close ``` -------------------------------- ### Create a Complete Stock Chart Example Source: https://github.com/cxn03651/write_xlsx/blob/main/docs/stock.md Demonstrates creating a stock chart with data, formatting, series, and axis labels. Includes writing date and numeric data to the worksheet and inserting the chart. ```ruby require 'write_xlsx' workbook = WriteXLSX.new('chart_stock.xlsx') worksheet = workbook.add_worksheet bold = workbook.add_format(bold: 1) date_format = workbook.add_format(num_format: 'dd/mm/yyyy') chart = workbook.add_chart(type: 'stock', embedded: 1) # Add the worksheet data the charts will refer to. headings = [ 'Date', 'High', 'Low', 'Close' ] data = [ [ '2007-01-01T', '2007-01-02T', '2007-01-03T', '2007-01-04T', '2007-01-05T' ], [ 27.2, 25.03, 19.05, 20.34, 18.5 ], [ 23.49, 19.55, 15.12, 17.84, 16.34 ], [ 25.45, 23.05, 17.32, 20.45, 17.34 ] ] worksheet.write('A1', headings, bold) (0..4).each do |row| worksheet.write_date_time(row+1, 0, data[0][row], date_format ) worksheet.write( row+1, 1, data[1][row] ) worksheet.write( row+1, 2, data[2][row] ) worksheet.write( row+1, 3, data[3][row] ) end worksheet.set_column('A:D', 11) # Add a series for each of the High-Low-Close columns. chart.add_series( categories: '=Sheet1!$A$2:$A$6', values: '=Sheet1!$B$2:$B$6' ) chart.add_series( categories: '=Sheet1!$A$2:$A$6', values: '=Sheet1!$C$2:$C$6' ) chart.add_series( categories: '=Sheet1!$A$2:$A$6', values: '=Sheet1!$D$2:$D$6' ) # Add a chart title and some axis labels. chart.set_title (name: 'High-Low-Close') chart.set_x_axis(name: 'Date') chart.set_y_axis(name: 'Share price') worksheet.insert_chart('E9', chart) workbook.close ``` -------------------------------- ### Basic Excel Formulas Source: https://github.com/cxn03651/write_xlsx/blob/main/docs/formulas_and_functions.md Examples of basic Excel formulas including arithmetic operations and function calls. Formulas must start with an equals sign and use uppercase for cell references and function names. ```excel '=A1+B1' ``` ```excel '=AVERAGE(1, 2, 3)' ``` -------------------------------- ### Create Format with Font Properties Hash Source: https://github.com/cxn03651/write_xlsx/blob/main/docs/cell_formatting.md Example of creating a format object using a hash containing font properties. This allows for modular definition of formatting styles. ```python font = { font: 'Calibri', size: 12, color: 'blue', bold: 1 } format1 = workbook.add_format(font) ``` -------------------------------- ### Create Introduction Worksheet Source: https://github.com/cxn03651/write_xlsx/blob/main/docs/examples.md Generates an 'Introduction' worksheet with custom formatting for text, including bold, size, color, and alignment. It also creates formatted hyperlinks to other sections. ```ruby def intro(workbook, _center, _heading, _colors) worksheet = workbook.add_worksheet('Introduction') worksheet.set_column(0, 0, 60) format = workbook.add_format format.set_bold format.set_size(14) format.set_color('blue') format.set_align('center') format2 = workbook.add_format format2.set_bold format2.set_color('blue') format3 = workbook.add_format( color: 'blue', underline: 1 ) worksheet.write(2, 0, 'This workbook demonstrates some of', format) worksheet.write(3, 0, 'the formatting options provided by', format) worksheet.write(4, 0, 'the Excel::Writer::XLSX module.', format) worksheet.write('A7', 'Sections:', format2) worksheet.write('A8', "internal:Fonts!A1", 'Fonts', format3) worksheet.write('A9', "internal:'Named colors'!A1", 'Named colors', format3) worksheet.write( 'A10', "internal:'Standard colors'!A1", 'Standard colors', format3 ) worksheet.write( 'A11', "internal:'Numeric formats'!A1", 'Numeric formats', format3 ) worksheet.write('A12', "internal:Borders!A1", 'Borders', format3) worksheet.write('A13', "internal:Patterns!A1", 'Patterns', format3) worksheet.write('A14', "internal:Alignment!A1", 'Alignment', format3) worksheet.write('A15', "internal:Miscellaneous!A1", 'Miscellaneous', format3) end ``` -------------------------------- ### Display Input Message on Cell Selection Source: https://github.com/cxn03651/write_xlsx/blob/main/docs/data_validation.md This example configures data validation for integers within a range and also displays a custom input title and message when the cell is selected. This helps guide the user on expected input. ```python worksheet.data_validation('A7', { "validate": 'integer', "criteria": 'between', "minimum": 1, "maximum": 100, "input_title": 'Enter an integer:', "input_message": 'between 1 and 100', }) ``` -------------------------------- ### Set Starting Page Number Source: https://github.com/cxn03651/write_xlsx/blob/main/docs/page_set_up.md Specify the number of the first page when the worksheet is printed. The default starting page is 1. ```python worksheet.set_start_page(2) ``` -------------------------------- ### Initialize Workbook and Define Formats Source: https://github.com/cxn03651/write_xlsx/blob/main/docs/examples.md Sets up a new Excel workbook and defines common formatting objects like centered and bold headings. Requires the 'write_xlsx' library. ```ruby require 'write_xlsx' workbook = WriteXLSX.new('formats.xlsx') # Some common formats center = workbook.add_format(align: 'center') heading = workbook.add_format(align: 'center', bold: 1) ``` -------------------------------- ### Configure Page Setup and Printing Options Source: https://context7.com/cxn03651/write_xlsx/llms.txt Sets up page layout, margins, headers, footers, print titles, print area, scaling, and page breaks for printing an Excel worksheet. Ensure the WriteXLSX gem is required. ```ruby require 'write_xlsx' workbook = WriteXLSX.new('print_setup.xlsx') worksheet = workbook.add_worksheet # Page orientation and size worksheet.set_landscape # worksheet.set_portrait # Default worksheet.set_paper(9) # A4 paper size # Set margins (inches) worksheet.set_margins(0.75, 0.75, 1.0, 1.0) # left, right, top, bottom # Headers and footers worksheet.set_header('&LCompany Name&C&A&RPage &P of &N') worksheet.set_footer('&L&D &T&R&F') # &L=left, &C=center, &R=right, &A=sheet name # &P=page number, &N=total pages, &D=date, &T=time, &F=filename # Print titles (repeat rows/columns on each page) worksheet.repeat_rows(0, 1) # Repeat rows 1-2 worksheet.repeat_columns(0, 0) # Repeat column A # Print area worksheet.print_area('A1:H50') # Fit to page worksheet.fit_to_pages(1, 0) # 1 page wide, unlimited tall # Other print options worksheet.center_horizontally worksheet.center_vertically worksheet.hide_gridlines(0) # 0=visible, 1=screen only, 2=hidden worksheet.print_row_col_headers # Print row/column headers # Page breaks worksheet.set_h_pagebreaks([20, 40, 60]) # Horizontal breaks worksheet.set_v_pagebreaks([5]) # Vertical breaks # Zoom and scaling worksheet.set_zoom(75) # Screen zoom worksheet.set_print_scale(90) # Print scale (%) workbook.close ``` -------------------------------- ### Create a Simple Line Chart Source: https://github.com/cxn03651/write_xlsx/blob/main/docs/line.md Demonstrates the basic steps to create an Excel file with a Line chart. Requires the 'write_xlsx' gem. ```ruby require 'write_xlsx' workbook = WriteXLSX.new('chart.xlsx') worksheet = workbook.add_worksheet chart = workbook.add_chart(type: 'line') # Configure the chart. chart.add_series( :categories: '=Sheet1!$A$2:$A$7', :values: '=Sheet1!$B$2:$B$7' ) # Add the worksheet data the chart refers to. data = [ [ 'Category', 2, 3, 4, 5, 6, 7 ], [ 'Value', 1, 4, 5, 2, 1, 5 ] ] worksheet.write('A1', data) workbook.close ``` -------------------------------- ### Set Bold and Color using Format Methods Source: https://github.com/cxn03651/write_xlsx/blob/main/docs/cell_formatting.md Demonstrates setting font properties like bold and color using individual format methods. Ensure the format object is created before calling these methods. ```python format = workbook.add_format format.set_bold format.set_color('red') ``` -------------------------------- ### Create a Simple Column Chart Source: https://github.com/cxn03651/write_xlsx/blob/main/docs/column.md Demonstrates the basic steps to create an Excel file with a simple column chart using WriteXLSX. Requires the 'write_xlsx' gem. ```ruby require 'write_xlsx' workbook = WriteXLSX.new('chart.xlsx') worksheet = workbook.add_worksheet chart = workbook.add_chart(type: 'column') # Configure the chart. chart.add_series( categories: '=Sheet1!$A$2:$A$7', values: '=Sheet1!$B$2:$B$7' ) # Add the worksheet data the chart refers to. data = [ [ 'Category', 2, 3, 4, 5, 6, 7 ], [ 'Value', 1, 4, 5, 2, 1, 5 ] ] worksheet.write('A1', data) workbook.close ```