Ruby - Cannot save formatted spreadsheet locally

50 Views Asked by At

I'm having an issue when trying to save a formatted spreadsheet locally to my computer.

The purpose of the program is to consolidate units to reorder based on a master spreadsheet and save it locally. The program should then attach the formatted spreadsheet to an email and send it.

The program seems to function without using AXLSX as far as sending the email goes, but when creating the method to save the spreadsheet using this gem, I keep getting the same error over and over stating "unknown regexp option: n (ArgumentError)"

Any help is appreciated as this is a really frustrating issue. I was initially able to insert the formatted data to a google sheets file, but then I ran into a bunch of issues trying to save the sheets file locally or as a temp file so I wanted to try going this route instead.

Here is the code for reference:

require 'bundler'
require 'roo'
require 'mail'
require 'date'
require 'axlsx'
Bundler.require

begin
def read_spreadsheet(file_path)
  spreadsheet = Roo::Excelx.new(file_path)
  header = spreadsheet.row(2)
  data = []
  (3..spreadsheet.last_row).each do |i|
    row = Hash[[header, spreadsheet.row(i)].transpose]
    data << row
  end
  data
end

def format_data_plain_text(data)
  formatted_data = ""

  data.each do |row|
    unless row['REORDER QTY'].to_i.zero? || row['AC SKU'].to_s.empty?
      sku = row['AC SKU'].to_s
      quantity = row['REORDER QTY'].to_s

      formatted_data += "#{sku}\t#{quantity}\n"
    end
  end
  formatted_data.to_s
end

def save_to_excel(formatted_data, local_file_path)
  Axlsx::Package.new do |p|
    p.workbook.add_worksheet(name: 'Reorder Details') do |sheet|
      sheet.add_row(['SKU', 'QUANTITY'])

      data_lines = formatted_data.split("\n")
      data_lines.each do |line|
        columns = line.split("\t")
        sheet.add_row(columns)
      end
    end
    p.serialize(local_file_path)
  end
end

def process_order(file_path)
  order_details = read_spreadsheet(file_path)
  formatted_data = format_data_plain_text(order_details)

  local_file_path = "new\spreadsheet\output.xlsx"
  save_to_excel(formatted_data, local_file_path)
  send_email(file_path)
end

def send_email(local_file_path)
    # Configure your email settings
    options = {
      address: 'smtp.gmail.com',
      port: 587,
      user_name: '[email protected]',
      password: 'password',
      domain: 'domain.com',
      authentication: 'plain',
      enable_starttls_auto: true
    }
    Mail.defaults do
      delivery_method :smtp, options
    end

    order_details = read_spreadsheet(file_path)
    formatted_data = format_data_plain_text(order_details)

    #Specify the email content
    mail = Mail.new do
      from    '[email protected]'
      to      '[email protected]'
      subject "Order Details:"
      body    "Hey Tim. Hope you're doing well! I just need to place an order for the following items. This will be using Net Terms.\n\nPlease let me know if you need anything else from me! Have a great week!"
      add_file local_file_path
    end
    # Send the email
    mail.deliver!
end

file_path = 'path\to\original\spreadsheet\012224 COUNT.xlsx'
process_order(file_path)

end

I tried using Axlsx to save the spreadsheet as well as using google-api to save to google sheets. I was able to update the google sheets file successfully using google-api, but then I couldn't get it to save locally to my computer, so I figured trying to work locally would be a more simple solution.

0

There are 0 best solutions below